]>
Commit | Line | Data |
---|---|---|
026a2f69 BB |
1 | import CommandLineKit |
2 | import Foundation | |
3 | ||
1263f62c BB |
4 | func main() { |
5 | let (flags, parser) = createParser() | |
6 | ||
7 | do { | |
8 | try parser.parse() | |
9 | } catch { | |
10 | parser.printUsage(error) | |
11 | exit(EX_USAGE) | |
12 | } | |
13 | ||
14 | // Boolean Options | |
15 | ||
16 | checkHelpFlag(flags["help"], withParser: parser) | |
17 | checkVersionFlag(flags["version"], withParser: parser) | |
18 | checkListSourcesFlag(flags["listSources"], withParser: parser) | |
19 | checkTitleFlag(flags["title"], withParser: parser) | |
20 | ||
21 | // String Options | |
22 | ||
23 | checkEnableSourceFlag(flags["enableSource"], withParser: parser) | |
24 | checkDisableSourceFlag(flags["disableSource"], withParser: parser) | |
25 | checkResetSourceFlag(flags["resetSource"], withParser: parser) | |
26 | ||
27 | // Remove any flags so anyone after this gets the unprocessed values | |
28 | ||
29 | let programName: [String] = [CommandLine.arguments[0]] | |
30 | CommandLine.arguments = programName + parser.unparsedArguments | |
31 | ||
32 | // Run Lyricli | |
33 | ||
34 | Lyricli.printLyrics() | |
35 | } | |
36 | ||
026a2f69 BB |
37 | /// Sets up and returns a new options parser |
38 | /// | |
194a3581 | 39 | /// - Returns: A Dictionary of Options, and a new CommandLineKit instance |
1263f62c | 40 | private func createParser() -> ([String:Option], CommandLineKit) { |
026a2f69 BB |
41 | let parser = CommandLineKit() |
42 | var flags: [String:Option] = [:] | |
43 | ||
44 | flags["help"] = BoolOption(shortFlag: "h", longFlag: "help", helpMessage: "Prints a help message.") | |
194a3581 | 45 | flags["version"] = BoolOption(shortFlag: "v", longFlag: "version", helpMessage: "Prints the version.") |
026a2f69 | 46 | |
0b3e11a8 BB |
47 | flags["enableSource"] = StringOption(shortFlag: "e", longFlag: "enable-source", helpMessage: "Enables a source") |
48 | flags["disableSource"] = StringOption(shortFlag: "d", longFlag: "disable-source", helpMessage: "Disables a source") | |
49 | flags["resetSource"] = StringOption(shortFlag: "r", longFlag: "reset-source", helpMessage: "Resets a source") | |
50 | flags["listSources"] = BoolOption(shortFlag: "l", longFlag: "list-sources", helpMessage: "Lists all sources") | |
51 | ||
52 | flags["title"] = BoolOption(shortFlag: "t", longFlag: "title", helpMessage: "Shows title of song if true") | |
53 | ||
026a2f69 BB |
54 | parser.addOptions(Array(flags.values)) |
55 | ||
47f1de79 BB |
56 | parser.formatOutput = {parseString, type in |
57 | ||
58 | var formattedString: String | |
59 | ||
1263f62c | 60 | switch type { |
47f1de79 BB |
61 | case .About: |
62 | formattedString = "\(parseString) [<artist_name> <song_name>]" | |
63 | break | |
64 | default: | |
65 | formattedString = parseString | |
66 | } | |
67 | ||
68 | return parser.defaultFormat(formattedString, type: type) | |
69 | } | |
70 | ||
026a2f69 BB |
71 | return (flags, parser) |
72 | } | |
73 | ||
1263f62c | 74 | // Handle the Help flag |
026a2f69 | 75 | |
1263f62c BB |
76 | private func checkHelpFlag(_ flag: Option?, withParser parser: CommandLineKit) { |
77 | if let helpFlag = flag as? BoolOption { | |
78 | if helpFlag.value { | |
0b3e11a8 BB |
79 | parser.printUsage() |
80 | exit(0) | |
81 | } | |
026a2f69 | 82 | } |
1263f62c | 83 | } |
026a2f69 | 84 | |
1263f62c BB |
85 | // Handle the version flag |
86 | ||
87 | private func checkVersionFlag(_ flag: Option?, withParser parser: CommandLineKit) { | |
88 | if let versionFlag = flag as? BoolOption { | |
89 | if versionFlag.value { | |
0b3e11a8 BB |
90 | print(Lyricli.version) |
91 | exit(0) | |
92 | } | |
93 | } | |
1263f62c | 94 | } |
0b3e11a8 | 95 | |
1263f62c BB |
96 | // Handle the list sources flag |
97 | ||
98 | private func checkListSourcesFlag(_ flag: Option?, withParser parser: CommandLineKit) { | |
99 | if let listSourcesFlag = flag as? BoolOption { | |
100 | if listSourcesFlag.value { | |
0b3e11a8 BB |
101 | Lyricli.printSources() |
102 | exit(0) | |
103 | } | |
104 | } | |
1263f62c BB |
105 | } |
106 | ||
107 | // Handle the title flag | |
108 | ||
109 | private func checkTitleFlag(_ flag: Option?, withParser parser: CommandLineKit) { | |
110 | if let titleFlag = flag as? BoolOption { | |
111 | if titleFlag.value { | |
112 | Lyricli.showTitle = true | |
113 | } | |
114 | } | |
115 | } | |
116 | ||
117 | // Handle the enable source flag | |
0b3e11a8 | 118 | |
1263f62c BB |
119 | private func checkEnableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) { |
120 | if let enableSourceFlag = flag as? StringOption { | |
0b3e11a8 BB |
121 | if let source = enableSourceFlag.value { |
122 | Lyricli.enableSource(source) | |
123 | exit(0) | |
124 | } | |
125 | } | |
1263f62c BB |
126 | } |
127 | ||
128 | // Handle the disable source flag | |
0b3e11a8 | 129 | |
1263f62c BB |
130 | private func checkDisableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) { |
131 | if let disableSourceFlag = flag as? StringOption { | |
0b3e11a8 BB |
132 | if let source = disableSourceFlag.value { |
133 | Lyricli.disableSource(source) | |
134 | exit(0) | |
135 | } | |
136 | } | |
1263f62c BB |
137 | } |
138 | ||
139 | // Handle the reset source flag | |
0b3e11a8 | 140 | |
1263f62c BB |
141 | private func checkResetSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) { |
142 | if let resetSourceFlag = flag as? StringOption { | |
0b3e11a8 BB |
143 | if let source = resetSourceFlag.value { |
144 | Lyricli.resetSource(source) | |
145 | exit(0) | |
146 | } | |
147 | } | |
026a2f69 BB |
148 | } |
149 | ||
150 | main() |