]> git.r.bdr.sh - rbdr/lyricli/blame - Sources/main.swift
Simulate lyrics engine
[rbdr/lyricli] / Sources / main.swift
CommitLineData
026a2f69
BB
1import CommandLineKit
2import Foundation
3
4/// Sets up and returns a new options parser
5///
194a3581 6/// - Returns: A Dictionary of Options, and a new CommandLineKit instance
026a2f69
BB
7func 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.")
194a3581 13 flags["version"] = BoolOption(shortFlag: "v", longFlag: "version", helpMessage: "Prints the version.")
026a2f69 14
0b3e11a8
BB
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
026a2f69
BB
22 parser.addOptions(Array(flags.values))
23
24 return (flags, parser)
25}
26
27func main() {
28
29 let (flags, parser) = createParser()
30
31 do {
32 try parser.parse()
33 }
34 catch {
35 parser.printUsage(error)
36 exit(EX_USAGE)
37 }
38
39 if let helpFlag = flags["help"] as? BoolOption {
40 if helpFlag.value == true {
0b3e11a8
BB
41 parser.printUsage()
42 exit(0)
43 }
026a2f69
BB
44 }
45
194a3581
BB
46 if let versionFlag = flags["version"] as? BoolOption {
47 if versionFlag.value == true {
0b3e11a8
BB
48 print(Lyricli.version)
49 exit(0)
50 }
51 }
52
53 if let listSourcesFlag = flags["listSources"] as? BoolOption {
54 if listSourcesFlag.value == true {
55 Lyricli.printSources()
56 exit(0)
57 }
58 }
59
60 if let enableSourceFlag = flags["enableSource"] as? StringOption {
61 if let source = enableSourceFlag.value {
62 Lyricli.enableSource(source)
63 exit(0)
64 }
65 }
66
67 if let disableSourceFlag = flags["disableSource"] as? StringOption {
68 if let source = disableSourceFlag.value {
69 Lyricli.disableSource(source)
70 exit(0)
71 }
72 }
73
74 if let resetSourceFlag = flags["resetSource"] as? StringOption {
75 if let source = resetSourceFlag.value {
76 Lyricli.resetSource(source)
77 exit(0)
78 }
79 }
80
81 if let titleFlag = flags["title"] as? BoolOption {
82 if titleFlag.value == true {
a968bff7 83 Lyricli.showTitle = true
0b3e11a8 84 }
194a3581
BB
85 }
86
4425e900
BB
87 // Remove any flags so anyone after this gets the unprocessed values
88 let programName: [String] = [CommandLine.arguments[0]]
89 CommandLine.arguments = programName + parser.unparsedArguments
90
0b3e11a8 91 Lyricli.printLyrics()
026a2f69
BB
92}
93
94main()