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