From 026a2f69776d0a8c9e15e560bef4a8a01f510aa0 Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Sat, 12 Nov 2016 23:23:46 -0600 Subject: Add basic option parsing --- README.md | 10 +++++++++- Sources/main.swift | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) 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() -- cgit