Added valueForKeys for CliOption
[][2]
Create and parse command line options for your cli application. Help documentation is auto created for you.
Implement the CliRunnable protocol and provide some CLIOptions.
CliOption(keys:["-o", "--option"], description:"Some Option", requiresValue:false)
This gives your command line application the ability to add some options when you execute your application.
app -o or app --option
app -o
app --option
You can set the requiresValue property to true or false to specify if the option requires a value or not
app -o some-value or app -o
app -o some-value
Set the action to be performed when the option is triggered
public var action: ((CliOption) throws -> Void)?
You can add sub options in the requiredArguments and optionalArguments arrays to create a command and additional options for the command
var command = CliOption(keys:["custom-command"], description:"Custom Command", requiresValue:false) let option = CliOption(keys:["-o", "--option"], description:"Some Option", requiresValue:false) command.add(argument: option, required: false)
app custom-command --option
A command can either have optionalArguments or requiredArguments
let secondaryOption = CliOption(keys:["-a", "--alternate-option"], description:"Alternate Option", requiresValue:true) command.add(argument: option, required: true)
app custom-command --alternate-option my-required-value
Help commands are automatically created from the CliOptions and CliOptionGroups
app help or app --help or app -h or no options at all app
app help
app --help
app -h
app
App Description app COMMAND [OPTIONS] Custom Commands: custom-command Do something custom
app custom-command help
app custom-command [OPTIONS] Custom Command -o, --option Some Option -a, --alternate-option Alternate Option
if you want to group related commands together for the printed help, use the CliOptionGroup
var psCommand = CliOption(keys:["ps"], description:"Process Status", requiresValue:false) var lsCommand = CliOption(keys:["ls"], description:"List Directory Contents", requiresValue:false) CliOptionGroup(description:"BSD General Commands:", options: [psCommand, lsCommand]) var sshCommand = CliOption(keys:["ssh"], description:"OpenSSH SSH client (remote login program)") var scpCommand = CliOption(keys:["sap"], description:"Secure copy (remote file copy program)") CliOptionGroup(description:"SSH Commands:", options: [sshCommand, scpCommand])
App Description app COMMAND [OPTIONS] BSD General Commands: ps Process Status ls List Directory Contents SSH Commands: ssh OpenSSH SSH client (remote login program) scp Secure copy (remote file copy program)
struct App : CliRunnable { var description: String? = "App Description\n" let appUsage = "app COMMAND [OPTIONS]\n" let customCommandUsage = "app custom-command [OPTIONS]\n" //prepare your commands and options var command = CliOption(keys:["custom-command"], description:"Do something custom", requiresValue:false) let option = CliOption(keys:["-o", "--option"], description:"Some Option", requiresValue:false) let secondaryOption = CliOption(keys:["-a", "--alternate-option"], description:"Alternate Option", requiresValue:true) var group = CliOptionGroup(description:"Commands Group:") public init(){ //add your options and to groups command.add(argument: option) command.add(argument: secondaryOption, required: true) group.options.append(command) cliOptionGroups = [group] } public var cliOptionGroups: [CliOptionGroup] public func usage(option: CliOption?) -> String? { return option != nil ? customCommandUsage : appUsage } }
[2]: //dnrops/CliRunnable/tree/master/ https:/swift.org”Swift”
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
CliRunnable
Create and parse command line options for your cli application. Help documentation is auto created for you.
CliOption
Implement the CliRunnable protocol and provide some CLIOptions.
This gives your command line application the ability to add some options when you execute your application.
app -o
orapp --option
CliOption: requiresValue
You can set the requiresValue property to true or false to specify if the option requires a value or not
app -o some-value
orapp -o
CliOption: action
Set the action to be performed when the option is triggered
CliOption as a Command
You can add sub options in the requiredArguments and optionalArguments arrays to create a command and additional options for the command
app custom-command --option
CliOption: optionalArguments, requiredArguments
A command can either have optionalArguments or requiredArguments
app custom-command --alternate-option my-required-value
Printing Help
Help commands are automatically created from the CliOptions and CliOptionGroups
Main Application Help
app help
orapp --help
orapp -h
or no options at allapp
Command Specific Help
app custom-command help
CliOptionGroup
if you want to group related commands together for the printed help, use the CliOptionGroup
app --help
Example CliRunnable struct
[2]: //dnrops/CliRunnable/tree/master/ https:/swift.org”Swift”