]> git.r.bdr.sh - rbdr/lyricli/blame_incremental - Sources/main.swift
Add linting to travis
[rbdr/lyricli] / Sources / main.swift
... / ...
CommitLineData
1import CommandLineKit
2import Foundation
3
4func 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
37/// Sets up and returns a new options parser
38///
39/// - Returns: A Dictionary of Options, and a new CommandLineKit instance
40private func createParser() -> ([String:Option], CommandLineKit) {
41 let parser = CommandLineKit()
42 var flags: [String:Option] = [:]
43
44 flags["help"] = BoolOption(shortFlag: "h", longFlag: "help", helpMessage: "Prints a help message.")
45 flags["version"] = BoolOption(shortFlag: "v", longFlag: "version", helpMessage: "Prints the version.")
46
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
54 parser.addOptions(Array(flags.values))
55
56 parser.formatOutput = {parseString, type in
57
58 var formattedString: String
59
60 switch type {
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
71 return (flags, parser)
72}
73
74// Handle the Help flag
75
76private func checkHelpFlag(_ flag: Option?, withParser parser: CommandLineKit) {
77 if let helpFlag = flag as? BoolOption {
78 if helpFlag.value {
79 parser.printUsage()
80 exit(0)
81 }
82 }
83}
84
85// Handle the version flag
86
87private func checkVersionFlag(_ flag: Option?, withParser parser: CommandLineKit) {
88 if let versionFlag = flag as? BoolOption {
89 if versionFlag.value {
90 print(Lyricli.version)
91 exit(0)
92 }
93 }
94}
95
96// Handle the list sources flag
97
98private func checkListSourcesFlag(_ flag: Option?, withParser parser: CommandLineKit) {
99 if let listSourcesFlag = flag as? BoolOption {
100 if listSourcesFlag.value {
101 Lyricli.printSources()
102 exit(0)
103 }
104 }
105}
106
107// Handle the title flag
108
109private 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
118
119private func checkEnableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
120 if let enableSourceFlag = flag as? StringOption {
121 if let source = enableSourceFlag.value {
122 Lyricli.enableSource(source)
123 exit(0)
124 }
125 }
126}
127
128// Handle the disable source flag
129
130private func checkDisableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
131 if let disableSourceFlag = flag as? StringOption {
132 if let source = disableSourceFlag.value {
133 Lyricli.disableSource(source)
134 exit(0)
135 }
136 }
137}
138
139// Handle the reset source flag
140
141private func checkResetSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
142 if let resetSourceFlag = flag as? StringOption {
143 if let source = resetSourceFlag.value {
144 Lyricli.resetSource(source)
145 exit(0)
146 }
147 }
148}
149
150main()