5 struct LyricliCommand: ParsableCommand {
7 // Positional Arguments
8 @Argument var artist: String?
9 @Argument var trackName: String?
12 @Flag(name: .shortAndLong, help: "Prints the version.")
15 @Flag(name: [.long, .customShort("t")], help: "Shows title of track if true")
18 @Flag(name: .shortAndLong, help: "Lists all sources")
19 var listSources = false
22 @Option(name: .shortAndLong, help: ArgumentHelp("Enables a source", valueName: "source"))
23 var enableSource: String?
25 @Option(name: .shortAndLong, help: ArgumentHelp("Disables a source", valueName: "source"))
26 var disableSource: String?
28 @Option(name: .shortAndLong, help: ArgumentHelp("Resets a source", valueName: "source"))
29 var resetSource: String?
31 mutating func run() throws {
33 // Handle the version flag
35 print(Lyricli.version)
39 // Handle the list sources flag
41 Lyricli.printSources()
45 // Handle the enable source option
46 if let source = enableSource {
48 try Lyricli.enableSource(source)
50 handleErrorAndQuit(error)
55 // Handle the disable source option
56 if let source = disableSource {
58 try Lyricli.disableSource(source)
60 handleErrorAndQuit(error)
65 // Handle the reset source flag
66 if let source = resetSource {
68 try Lyricli.resetSource(source)
70 handleErrorAndQuit(error)
75 Lyricli.showTitle = showTitle
78 let currentTrack: Track
80 currentTrack = Track(withName: trackName, andArtist: artist)
82 currentTrack = Track(withName: "", andArtist: artist)
84 Lyricli.printLyrics(currentTrack)
91 private func handleErrorAndQuit(_ error: Error) {
92 fputs(error.localizedDescription, stderr)