import Darwin import ArgumentParser @main struct LyricliCommand: ParsableCommand { // Positional Arguments @Argument var artist: String? @Argument var trackName: String? // Flags @Flag(name: .shortAndLong, help: "Prints the version.") var version = false @Flag(name: [.long, .customShort("t")], help: "Shows title of track if true") var showTitle = false @Flag(name: .shortAndLong, help: "Lists all sources") var listSources = false // Named Arguments @Option(name: .shortAndLong, help: ArgumentHelp("Enables a source", valueName: "source")) var enableSource: String? @Option(name: .shortAndLong, help: ArgumentHelp("Disables a source", valueName: "source")) var disableSource: String? @Option(name: .shortAndLong, help: ArgumentHelp("Resets a source", valueName: "source")) var resetSource: String? mutating func run() throws { // Handle the version flag if version { print(Lyricli.version) Darwin.exit(0) } // Handle the list sources flag if listSources { Lyricli.printSources() Darwin.exit(0) } // Handle the enable source option if let source = enableSource { do { try Lyricli.enableSource(source) } catch let error { handleErrorAndQuit(error) } Darwin.exit(0) } // Handle the disable source option if let source = disableSource { do { try Lyricli.disableSource(source) } catch let error { handleErrorAndQuit(error) } Darwin.exit(0) } // Handle the reset source flag if let source = resetSource { do { try Lyricli.resetSource(source) } catch let error { handleErrorAndQuit(error) } Darwin.exit(0) } Lyricli.showTitle = showTitle if let artist { let currentTrack: Track if let trackName { currentTrack = Track(withName: trackName, andArtist: artist) } else { currentTrack = Track(withName: "", andArtist: artist) } Lyricli.printLyrics(currentTrack) Darwin.exit(0) } Lyricli.printLyrics() } private func handleErrorAndQuit(_ error: Error) { fputs(error.localizedDescription, stderr) Darwin.exit(1) } }