import ArgumentParser import Foundation import ParcelIPC struct SetupCommand: AsyncParsableCommand { @OptionGroup var output: CLIOptions static let configuration = CommandConfiguration( commandName: "setup", abstract: String( localized: "List, save, open, or delete named setups.", bundle: .module), subcommands: [ SetupListCommand.self, SetupSaveCommand.self, SetupNamedCommand.self, SetupNamedCommand.self, ], defaultSubcommand: SetupListCommand.self) } struct SetupListCommand: CLICommand { static let configuration = CommandConfiguration( commandName: "list", abstract: String(localized: "List saved setups.", bundle: .module)) @OptionGroup var output: CLIOptions var invocation: CLIInvocation { .request(command: "query", args: [SetupIPC.queryName], query: .setups, json: output.json) } } struct SetupSaveCommand: CLICommand { static let configuration = CommandConfiguration( commandName: "save", abstract: String(localized: "Save the current arrangement as a named setup.", bundle: .module)) @OptionGroup var output: CLIOptions @Argument( help: ArgumentHelp( String(localized: "Setup name. Quote names containing spaces.", bundle: .module), valueName: "name")) var name: String @Flag( help: ArgumentHelp( String( localized: "Do not hide other applications when this setup opens.", bundle: .module))) var noHide = false mutating func validate() throws { try CLIParsing.requireName(name) } var invocation: CLIInvocation { get throws { let request = try SetupIPC.Mutation.save.request(name: name, noHide: noHide) return .request(command: request.command, args: request.args, query: nil, json: output.json) } } } protocol SetupMutationShortcut { static var mutation: SetupIPC.Mutation { get } static var abstract: String { get } } struct SetupNamedCommand: CLICommand { static var configuration: CommandConfiguration { CommandConfiguration( commandName: String(Action.mutation.rawValue.dropFirst("setup-".count)), abstract: Action.abstract) } @OptionGroup var output: CLIOptions @Argument( help: ArgumentHelp( String(localized: "Setup name. Quote names containing spaces.", bundle: .module), valueName: "name")) var name: String mutating func validate() throws { try CLIParsing.requireName(name) } var invocation: CLIInvocation { get throws { let request = try Action.mutation.request(name: name) return .request(command: request.command, args: request.args, query: nil, json: output.json) } } } enum SetupOpen: SetupMutationShortcut { static let mutation = SetupIPC.Mutation.open static var abstract: String { String(localized: "Open a saved setup.", bundle: .module) } } enum SetupDelete: SetupMutationShortcut { static let mutation = SetupIPC.Mutation.delete static var abstract: String { String(localized: "Delete a saved setup.", bundle: .module) } }