]> git.r.bdr.sh - rbdr/lyricli/blob - Sources/main.swift
e4b2760e873888695e8174b42db9c84b77ca82e3
[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 flags["enableSource"] = StringOption(shortFlag: "e", longFlag: "enable-source", helpMessage: "Enables a source")
16 flags["disableSource"] = StringOption(shortFlag: "d", longFlag: "disable-source", helpMessage: "Disables a source")
17 flags["resetSource"] = StringOption(shortFlag: "r", longFlag: "reset-source", helpMessage: "Resets a source")
18 flags["listSources"] = BoolOption(shortFlag: "l", longFlag: "list-sources", helpMessage: "Lists all sources")
19
20 flags["title"] = BoolOption(shortFlag: "t", longFlag: "title", helpMessage: "Shows title of song if true")
21
22 parser.addOptions(Array(flags.values))
23
24 return (flags, parser)
25 }
26
27 func main() {
28
29 let (flags, parser) = createParser()
30
31 do {
32 try parser.parse()
33 }
34 catch {
35 parser.printUsage(error)
36 exit(EX_USAGE)
37 }
38
39 if let helpFlag = flags["help"] as? BoolOption {
40 if helpFlag.value == true {
41 parser.printUsage()
42 exit(0)
43 }
44 }
45
46 if let versionFlag = flags["version"] as? BoolOption {
47 if versionFlag.value == true {
48 print(Lyricli.version)
49 exit(0)
50 }
51 }
52
53 if let listSourcesFlag = flags["listSources"] as? BoolOption {
54 if listSourcesFlag.value == true {
55 Lyricli.printSources()
56 exit(0)
57 }
58 }
59
60 if let enableSourceFlag = flags["enableSource"] as? StringOption {
61 if let source = enableSourceFlag.value {
62 Lyricli.enableSource(source)
63 exit(0)
64 }
65 }
66
67 if let disableSourceFlag = flags["disableSource"] as? StringOption {
68 if let source = disableSourceFlag.value {
69 Lyricli.disableSource(source)
70 exit(0)
71 }
72 }
73
74 if let resetSourceFlag = flags["resetSource"] as? StringOption {
75 if let source = resetSourceFlag.value {
76 Lyricli.resetSource(source)
77 exit(0)
78 }
79 }
80
81 if let titleFlag = flags["title"] as? BoolOption {
82 if titleFlag.value == true {
83 Lyricli.showTitle = true
84 }
85 }
86
87 // Remove any flags so anyone after this gets the unprocessed values
88 let programName: [String] = [CommandLine.arguments[0]]
89 CommandLine.arguments = programName + parser.unparsedArguments
90
91 Lyricli.printLyrics()
92 }
93
94 main()