5 let (flags, parser) = createParser()
10 parser.printUsage(error)
16 checkHelpFlag(flags["help"], withParser: parser)
17 checkVersionFlag(flags["version"], withParser: parser)
18 checkListSourcesFlag(flags["listSources"], withParser: parser)
19 checkTitleFlag(flags["title"], withParser: parser)
23 checkEnableSourceFlag(flags["enableSource"], withParser: parser)
24 checkDisableSourceFlag(flags["disableSource"], withParser: parser)
25 checkResetSourceFlag(flags["resetSource"], withParser: parser)
27 // Remove any flags so anyone after this gets the unprocessed values
29 let programName: [String] = [CommandLine.arguments[0]]
30 CommandLine.arguments = programName + parser.unparsedArguments
37 /// Sets up and returns a new options parser
39 /// - Returns: A Dictionary of Options, and a new CommandLineKit instance
40 private func createParser() -> ([String:Option], CommandLineKit) {
41 let parser = CommandLineKit()
42 var flags: [String:Option] = [:]
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.")
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")
52 flags["title"] = BoolOption(shortFlag: "t", longFlag: "title", helpMessage: "Shows title of song if true")
54 parser.addOptions(Array(flags.values))
56 parser.formatOutput = {parseString, type in
58 var formattedString: String
62 formattedString = "\(parseString) [<artist_name> <song_name>]"
65 formattedString = parseString
68 return parser.defaultFormat(formattedString, type: type)
71 return (flags, parser)
74 // Handle the Help flag
76 private func checkHelpFlag(_ flag: Option?, withParser parser: CommandLineKit) {
77 if let helpFlag = flag as? BoolOption {
85 // Handle the version flag
87 private func checkVersionFlag(_ flag: Option?, withParser parser: CommandLineKit) {
88 if let versionFlag = flag as? BoolOption {
89 if versionFlag.value {
90 print(Lyricli.version)
96 // Handle the list sources flag
98 private func checkListSourcesFlag(_ flag: Option?, withParser parser: CommandLineKit) {
99 if let listSourcesFlag = flag as? BoolOption {
100 if listSourcesFlag.value {
101 Lyricli.printSources()
107 // Handle the title flag
109 private func checkTitleFlag(_ flag: Option?, withParser parser: CommandLineKit) {
110 if let titleFlag = flag as? BoolOption {
112 Lyricli.showTitle = true
117 // Handle the enable source flag
119 private func checkEnableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
120 if let enableSourceFlag = flag as? StringOption {
121 if let source = enableSourceFlag.value {
122 Lyricli.enableSource(source)
128 // Handle the disable source flag
130 private func checkDisableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
131 if let disableSourceFlag = flag as? StringOption {
132 if let source = disableSourceFlag.value {
133 Lyricli.disableSource(source)
139 // Handle the reset source flag
141 private func checkResetSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
142 if let resetSourceFlag = flag as? StringOption {
143 if let source = resetSourceFlag.value {
144 Lyricli.resetSource(source)