4 // Entry point of the application. This is the main executable
6 let (flags, parser) = createParser()
11 parser.printUsage(error)
17 checkHelpFlag(flags["help"], withParser: parser)
18 checkVersionFlag(flags["version"], withParser: parser)
19 checkListSourcesFlag(flags["listSources"], withParser: parser)
20 checkTitleFlag(flags["title"], withParser: parser)
24 checkEnableSourceFlag(flags["enableSource"], withParser: parser)
25 checkDisableSourceFlag(flags["disableSource"], withParser: parser)
26 checkResetSourceFlag(flags["resetSource"], withParser: parser)
28 // Remove any flags so anyone after this gets the unprocessed values
30 let programName: [String] = [CommandLine.arguments[0]]
31 CommandLine.arguments = programName + parser.unparsedArguments
38 /// Sets up and returns a new options parser
40 /// - Returns: A Dictionary of Options, and a new CommandLineKit instance
41 private func createParser() -> ([String:Option], CommandLineKit) {
42 let parser = CommandLineKit()
43 var flags: [String:Option] = [:]
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.")
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")
53 flags["title"] = BoolOption(shortFlag: "t", longFlag: "title", helpMessage: "Shows title of song if true")
55 parser.addOptions(Array(flags.values))
57 parser.formatOutput = {parseString, type in
59 var formattedString: String
63 formattedString = "\(parseString) [<artist_name> <song_name>]"
66 formattedString = parseString
69 return parser.defaultFormat(formattedString, type: type)
72 return (flags, parser)
75 // Handle the Help flag
77 private func checkHelpFlag(_ flag: Option?, withParser parser: CommandLineKit) {
78 if let helpFlag = flag as? BoolOption {
86 // Handle the version flag
88 private func checkVersionFlag(_ flag: Option?, withParser parser: CommandLineKit) {
89 if let versionFlag = flag as? BoolOption {
90 if versionFlag.value {
91 print(Lyricli.version)
97 // Handle the list sources flag
99 private func checkListSourcesFlag(_ flag: Option?, withParser parser: CommandLineKit) {
100 if let listSourcesFlag = flag as? BoolOption {
101 if listSourcesFlag.value {
102 Lyricli.printSources()
108 // Handle the title flag
110 private func checkTitleFlag(_ flag: Option?, withParser parser: CommandLineKit) {
111 if let titleFlag = flag as? BoolOption {
113 Lyricli.showTitle = true
118 // Handle the enable source flag
120 private func checkEnableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
121 if let enableSourceFlag = flag as? StringOption {
122 if let source = enableSourceFlag.value {
123 Lyricli.enableSource(source)
129 // Handle the disable source flag
131 private func checkDisableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
132 if let disableSourceFlag = flag as? StringOption {
133 if let source = disableSourceFlag.value {
134 Lyricli.disableSource(source)
140 // Handle the reset source flag
142 private func checkResetSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
143 if let resetSourceFlag = flag as? StringOption {
144 if let source = resetSourceFlag.value {
145 Lyricli.resetSource(source)