From: Ben Beltran Date: Sun, 13 Nov 2016 05:23:46 +0000 (-0600) Subject: Add basic option parsing X-Git-Tag: 0.1.0^2~4^2~3 X-Git-Url: https://git.r.bdr.sh/rbdr/lyricli/commitdiff_plain/026a2f69776d0a8c9e15e560bef4a8a01f510aa0?ds=inline Add basic option parsing --- diff --git a/README.md b/README.md index 6713a7d..71d7259 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ -#Lyricli (lrc) +# Lyricli (lrc) A command line tool to show the lyrics of your current song + +## Building + +Run `swift build` + +## Running tests + +Run `swift test` diff --git a/Sources/main.swift b/Sources/main.swift index f7cf60e..61f4f71 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -1 +1,40 @@ -print("Hello, world!") +import CommandLineKit +import Foundation + +/// Sets up and returns a new options parser +/// +/// - Returns: A new OptionParser instance +func createParser() -> ([String:Option], CommandLineKit) { + + let parser = CommandLineKit() + var flags: [String:Option] = [:] + + flags["help"] = BoolOption(shortFlag: "h", longFlag: "help", helpMessage: "Prints a help message.") + + parser.addOptions(Array(flags.values)) + + return (flags, parser) +} + +func main() { + + let (flags, parser) = createParser() + + do { + try parser.parse() + } + catch { + parser.printUsage(error) + exit(EX_USAGE) + } + + if let helpFlag = flags["help"] as? BoolOption { + if helpFlag.value == true { + parser.printUsage() + exit(0) + } + } + +} + +main()