]> git.r.bdr.sh - rbdr/lyricli/blob - Sources/main.swift
Print the version with -v / --version
[rbdr/lyricli] / Sources / main.swift
1 import CommandLineKit
2 import Foundation
3
4 /// Sets up and returns a new options parser
5 ///
6 /// - Returns: A Dictionary of Options, and a new CommandLineKit instance
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.")
13 flags["version"] = BoolOption(shortFlag: "v", longFlag: "version", helpMessage: "Prints the version.")
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
39 if let versionFlag = flags["version"] as? BoolOption {
40 if versionFlag.value == true {
41 print(Lyricli.version)
42 exit(0)
43 }
44 }
45
46 }
47
48 main()