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