4 /// Sets up and returns a new options parser
6 /// - Returns: A Dictionary of Options, and a new CommandLineKit instance
7 func createParser() -> ([String:Option], CommandLineKit) {
9 let parser = CommandLineKit()
10 var flags: [String:Option] = [:]
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.")
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")
20 flags["title"] = BoolOption(shortFlag: "t", longFlag: "title", helpMessage: "Shows title of song if true")
22 parser.addOptions(Array(flags.values))
24 parser.formatOutput = {parseString, type in
26 var formattedString: String
30 formattedString = "\(parseString) [<artist_name> <song_name>]"
33 formattedString = parseString
36 return parser.defaultFormat(formattedString, type: type)
39 return (flags, parser)
44 let (flags, parser) = createParser()
50 parser.printUsage(error)
54 if let helpFlag = flags["help"] as? BoolOption {
55 if helpFlag.value == true {
61 if let versionFlag = flags["version"] as? BoolOption {
62 if versionFlag.value == true {
63 print(Lyricli.version)
68 if let listSourcesFlag = flags["listSources"] as? BoolOption {
69 if listSourcesFlag.value == true {
70 Lyricli.printSources()
75 if let enableSourceFlag = flags["enableSource"] as? StringOption {
76 if let source = enableSourceFlag.value {
77 Lyricli.enableSource(source)
82 if let disableSourceFlag = flags["disableSource"] as? StringOption {
83 if let source = disableSourceFlag.value {
84 Lyricli.disableSource(source)
89 if let resetSourceFlag = flags["resetSource"] as? StringOption {
90 if let source = resetSourceFlag.value {
91 Lyricli.resetSource(source)
96 if let titleFlag = flags["title"] as? BoolOption {
97 if titleFlag.value == true {
98 Lyricli.showTitle = true
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
106 Lyricli.printLyrics()