aboutsummaryrefslogtreecommitdiff
path: root/Sources/main.swift
blob: e4b2760e873888695e8174b42db9c84b77ca82e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import CommandLineKit
import Foundation

/// Sets up and returns a new options parser
/// 
/// - Returns: A Dictionary of Options, and a new CommandLineKit 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.")
    flags["version"] = BoolOption(shortFlag: "v", longFlag: "version", helpMessage: "Prints the version.")

    flags["enableSource"] = StringOption(shortFlag: "e", longFlag: "enable-source", helpMessage: "Enables a source")
    flags["disableSource"] = StringOption(shortFlag: "d", longFlag: "disable-source", helpMessage: "Disables a source")
    flags["resetSource"] = StringOption(shortFlag: "r", longFlag: "reset-source", helpMessage: "Resets a source")
    flags["listSources"] = BoolOption(shortFlag: "l", longFlag: "list-sources", helpMessage: "Lists all sources")

    flags["title"] = BoolOption(shortFlag: "t", longFlag: "title", helpMessage: "Shows title of song if true")

    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)
        }
    }

    if let versionFlag = flags["version"] as? BoolOption {
        if versionFlag.value == true {
            print(Lyricli.version)
            exit(0)
        }
    }

    if let listSourcesFlag = flags["listSources"] as? BoolOption {
        if listSourcesFlag.value == true {
            Lyricli.printSources()
            exit(0)
        }
    }

    if let enableSourceFlag = flags["enableSource"] as? StringOption {
        if let source = enableSourceFlag.value {
            Lyricli.enableSource(source)
            exit(0)
        }
    }

    if let disableSourceFlag = flags["disableSource"] as? StringOption {
        if let source = disableSourceFlag.value {
            Lyricli.disableSource(source)
            exit(0)
        }
    }

    if let resetSourceFlag = flags["resetSource"] 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()