]>
Commit | Line | Data |
---|---|---|
026a2f69 BB |
1 | import CommandLineKit |
2 | import Foundation | |
3 | ||
4 | /// Sets up and returns a new options parser | |
5 | /// | |
194a3581 | 6 | /// - Returns: A Dictionary of Options, and a new CommandLineKit instance |
026a2f69 BB |
7 | func createParser() -> ([String:Option], CommandLineKit) { |
8 | ||
9 | let parser = CommandLineKit() | |
10 | var flags: [String:Option] = [:] | |
11 | ||
12 | flags["help"] = BoolOption(shortFlag: "h", longFlag: "help", helpMessage: "Prints a help message.") | |
194a3581 | 13 | flags["version"] = BoolOption(shortFlag: "v", longFlag: "version", helpMessage: "Prints the version.") |
026a2f69 BB |
14 | |
15 | parser.addOptions(Array(flags.values)) | |
16 | ||
17 | return (flags, parser) | |
18 | } | |
19 | ||
20 | func main() { | |
21 | ||
22 | let (flags, parser) = createParser() | |
23 | ||
24 | do { | |
25 | try parser.parse() | |
26 | } | |
27 | catch { | |
28 | parser.printUsage(error) | |
29 | exit(EX_USAGE) | |
30 | } | |
31 | ||
32 | if let helpFlag = flags["help"] as? BoolOption { | |
33 | if helpFlag.value == true { | |
34 | parser.printUsage() | |
35 | exit(0) | |
36 | } | |
37 | } | |
38 | ||
194a3581 BB |
39 | if let versionFlag = flags["version"] as? BoolOption { |
40 | if versionFlag.value == true { | |
41 | print(Lyricli.version) | |
42 | exit(0) | |
43 | } | |
44 | } | |
45 | ||
026a2f69 BB |
46 | } |
47 | ||
48 | main() |