aboutsummaryrefslogtreecommitdiff
path: root/Sources
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2016-11-12 23:23:46 -0600
committerBen Beltran <ben@nsovocal.com>2016-11-12 23:23:46 -0600
commit026a2f69776d0a8c9e15e560bef4a8a01f510aa0 (patch)
treefeef18ae6adfef33d8d78ad61df47a70c19636b9 /Sources
parentc3180470b5b2549a6fd5833e85a8bcbfad818fb3 (diff)
Add basic option parsing
Diffstat (limited to 'Sources')
-rw-r--r--Sources/main.swift41
1 files changed, 40 insertions, 1 deletions
diff --git a/Sources/main.swift b/Sources/main.swift
index f7cf60e..61f4f71 100644
--- a/Sources/main.swift
+++ b/Sources/main.swift
@@ -1 +1,40 @@
-print("Hello, world!")
+import CommandLineKit
+import Foundation
+
+/// Sets up and returns a new options parser
+///
+/// - Returns: A new OptionParser instance
+func createParser() -> ([String:Option], CommandLineKit) {
+
+ let parser = CommandLineKit()
+ var flags: [String:Option] = [:]
+
+ flags["help"] = BoolOption(shortFlag: "h", longFlag: "help", helpMessage: "Prints a help message.")
+
+ parser.addOptions(Array(flags.values))
+
+ return (flags, parser)
+}
+
+func main() {
+
+ let (flags, parser) = createParser()
+
+ do {
+ try parser.parse()
+ }
+ catch {
+ parser.printUsage(error)
+ exit(EX_USAGE)
+ }
+
+ if let helpFlag = flags["help"] as? BoolOption {
+ if helpFlag.value == true {
+ parser.printUsage()
+ exit(0)
+ }
+ }
+
+}
+
+main()