aboutsummaryrefslogtreecommitdiff
path: root/Sources/main.swift
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2017-05-18 23:39:25 -0500
committerBen Beltran <ben@nsovocal.com>2017-05-18 23:39:25 -0500
commit1263f62c5c6379f11e19eb184ffedf5889390b70 (patch)
treef594221e77f5a6163ed96ad1c56556240ec8ea86 /Sources/main.swift
parent6608973cc785af2363b71299e03407e5dd436f54 (diff)
Fix linter warnings
Diffstat (limited to 'Sources/main.swift')
-rw-r--r--Sources/main.swift111
1 files changed, 76 insertions, 35 deletions
diff --git a/Sources/main.swift b/Sources/main.swift
index 42d4855..5c39cb5 100644
--- a/Sources/main.swift
+++ b/Sources/main.swift
@@ -1,11 +1,43 @@
import CommandLineKit
import Foundation
+func main() {
+ let (flags, parser) = createParser()
+
+ do {
+ try parser.parse()
+ } catch {
+ parser.printUsage(error)
+ exit(EX_USAGE)
+ }
+
+ // Boolean Options
+
+ checkHelpFlag(flags["help"], withParser: parser)
+ checkVersionFlag(flags["version"], withParser: parser)
+ checkListSourcesFlag(flags["listSources"], withParser: parser)
+ checkTitleFlag(flags["title"], withParser: parser)
+
+ // String Options
+
+ checkEnableSourceFlag(flags["enableSource"], withParser: parser)
+ checkDisableSourceFlag(flags["disableSource"], withParser: parser)
+ checkResetSourceFlag(flags["resetSource"], withParser: parser)
+
+ // Remove any flags so anyone after this gets the unprocessed values
+
+ let programName: [String] = [CommandLine.arguments[0]]
+ CommandLine.arguments = programName + parser.unparsedArguments
+
+ // Run Lyricli
+
+ Lyricli.printLyrics()
+}
+
/// Sets up and returns a new options parser
///
/// - Returns: A Dictionary of Options, and a new CommandLineKit instance
-func createParser() -> ([String:Option], CommandLineKit) {
-
+private func createParser() -> ([String:Option], CommandLineKit) {
let parser = CommandLineKit()
var flags: [String:Option] = [:]
@@ -25,7 +57,7 @@ func createParser() -> ([String:Option], CommandLineKit) {
var formattedString: String
- switch(type) {
+ switch type {
case .About:
formattedString = "\(parseString) [<artist_name> <song_name>]"
break
@@ -39,71 +71,80 @@ func createParser() -> ([String:Option], CommandLineKit) {
return (flags, parser)
}
-func main() {
-
- let (flags, parser) = createParser()
+// Handle the Help flag
- do {
- try parser.parse()
- }
- catch {
- parser.printUsage(error)
- exit(EX_USAGE)
- }
-
- if let helpFlag = flags["help"] as? BoolOption {
- if helpFlag.value == true {
+private func checkHelpFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+ if let helpFlag = flag as? BoolOption {
+ if helpFlag.value {
parser.printUsage()
exit(0)
}
}
+}
- if let versionFlag = flags["version"] as? BoolOption {
- if versionFlag.value == true {
+// Handle the version flag
+
+private func checkVersionFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+ if let versionFlag = flag as? BoolOption {
+ if versionFlag.value {
print(Lyricli.version)
exit(0)
}
}
+}
- if let listSourcesFlag = flags["listSources"] as? BoolOption {
- if listSourcesFlag.value == true {
+// Handle the list sources flag
+
+private func checkListSourcesFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+ if let listSourcesFlag = flag as? BoolOption {
+ if listSourcesFlag.value {
Lyricli.printSources()
exit(0)
}
}
+}
+
+// Handle the title flag
+
+private func checkTitleFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+ if let titleFlag = flag as? BoolOption {
+ if titleFlag.value {
+ Lyricli.showTitle = true
+ }
+ }
+}
+
+// Handle the enable source flag
- if let enableSourceFlag = flags["enableSource"] as? StringOption {
+private func checkEnableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+ if let enableSourceFlag = flag as? StringOption {
if let source = enableSourceFlag.value {
Lyricli.enableSource(source)
exit(0)
}
}
+}
+
+// Handle the disable source flag
- if let disableSourceFlag = flags["disableSource"] as? StringOption {
+private func checkDisableSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+ if let disableSourceFlag = flag as? StringOption {
if let source = disableSourceFlag.value {
Lyricli.disableSource(source)
exit(0)
}
}
+}
+
+// Handle the reset source flag
- if let resetSourceFlag = flags["resetSource"] as? StringOption {
+private func checkResetSourceFlag(_ flag: Option?, withParser parser: CommandLineKit) {
+ if let resetSourceFlag = flag as? StringOption {
if let source = resetSourceFlag.value {
Lyricli.resetSource(source)
exit(0)
}
}
-
- if let titleFlag = flags["title"] as? BoolOption {
- if titleFlag.value == true {
- Lyricli.showTitle = true
- }
- }
-
- // Remove any flags so anyone after this gets the unprocessed values
- let programName: [String] = [CommandLine.arguments[0]]
- CommandLine.arguments = programName + parser.unparsedArguments
-
- Lyricli.printLyrics()
}
main()