]> git.r.bdr.sh - rbdr/lyricli/blob - Sources/main.swift
42d485588fabfcb72c064c0f8748811d9e7fe334
[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 parser.formatOutput = {parseString, type in
25
26 var formattedString: String
27
28 switch(type) {
29 case .About:
30 formattedString = "\(parseString) [<artist_name> <song_name>]"
31 break
32 default:
33 formattedString = parseString
34 }
35
36 return parser.defaultFormat(formattedString, type: type)
37 }
38
39 return (flags, parser)
40 }
41
42 func main() {
43
44 let (flags, parser) = createParser()
45
46 do {
47 try parser.parse()
48 }
49 catch {
50 parser.printUsage(error)
51 exit(EX_USAGE)
52 }
53
54 if let helpFlag = flags["help"] as? BoolOption {
55 if helpFlag.value == true {
56 parser.printUsage()
57 exit(0)
58 }
59 }
60
61 if let versionFlag = flags["version"] as? BoolOption {
62 if versionFlag.value == true {
63 print(Lyricli.version)
64 exit(0)
65 }
66 }
67
68 if let listSourcesFlag = flags["listSources"] as? BoolOption {
69 if listSourcesFlag.value == true {
70 Lyricli.printSources()
71 exit(0)
72 }
73 }
74
75 if let enableSourceFlag = flags["enableSource"] as? StringOption {
76 if let source = enableSourceFlag.value {
77 Lyricli.enableSource(source)
78 exit(0)
79 }
80 }
81
82 if let disableSourceFlag = flags["disableSource"] as? StringOption {
83 if let source = disableSourceFlag.value {
84 Lyricli.disableSource(source)
85 exit(0)
86 }
87 }
88
89 if let resetSourceFlag = flags["resetSource"] as? StringOption {
90 if let source = resetSourceFlag.value {
91 Lyricli.resetSource(source)
92 exit(0)
93 }
94 }
95
96 if let titleFlag = flags["title"] as? BoolOption {
97 if titleFlag.value == true {
98 Lyricli.showTitle = true
99 }
100 }
101
102 // Remove any flags so anyone after this gets the unprocessed values
103 let programName: [String] = [CommandLine.arguments[0]]
104 CommandLine.arguments = programName + parser.unparsedArguments
105
106 Lyricli.printLyrics()
107 }
108
109 main()