CLI API
CLI is a template that provides entry-point functions to call argparse.
Here are the signatures that CLI template has:
The second template with multiple COMMANDS... has only main function which wraps all COMMANDS inside internal struct with only data member of type SubCommand!COMMANDS and calls CLI(Config config, CMD).main with that.
There is also an alias that uses default Config.init to simplify default behavior:
Public members
parseKnownArgs
CLI.parseKnownArgs is a function that parses only known arguments from the command line.
All arguments that were not recognized during parsing are returned to a caller.
Signature
Parameters
receiverObject that receives parsed command line arguments.
argsCommand line arguments to parse (excluding
argv[0]– first command line argument inmainfunction).unrecognizedArgsCommand line arguments that were not parsed.
Notes
The second signature (without
unrecognizedArgsparameter) returns not parsed arguments throughargsreference parameter.
Return value
Result object that can be cast to bool to check whether the parsing was successful or not. Successful parsing for parseKnownArgs function means that there are no error during parsing of known arguments. This means that having unrecognized arguments in a command line is not an error.
parseArgs
CLI.parseArgs is a function that parses command line arguments and validates that there are no unknown ones.
Signature
Parameters
receiverObject that receives parsed command line arguments.
argsCommand line arguments to parse (excluding
argv[0]– first command line argument inmainfunction).
Return value
In case of parsing error -
Result.exitCode(1by default).In case of success -
0.
complete
CLI.complete is a function that performs shell completion for command line arguments.
Signature
Parameters
argsCommand line arguments (excluding
argv[0]– first command line argument inmainfunction).
Notes
This function provides completion for the last argument in the command line:
If the last entry in command line is an empty string (
"") then it provides all available argument names prepended withConfig.shortNamePrefixorConfig.longNamePrefix.If the last entry in command line contains characters then
completeprovides completion only with those arguments that have names starting with specified characters.
Return value
0in case of successful parsing.Non-zero otherwise.
mainComplete
CLI.mainComplete is a mixin template that provides global main function which calls CLI.complete.
Signature
Notes
Ingested main function is a simple wrapper of CLI.complete function that removes argv[0] from command line.
Return value
Value returned from CLI.complete function.
main
CLI.main is a mixin template that does one of these:
If
argparse_completionversion is defined then it instantiatesCLI.mainCompletetemplate mixin.Otherwise it provides global
mainfunction that callsCLI.parseArgsfunction.
Signature
Parameters
newMainFunction that is called after successful command line parsing. See
newMainfor details.
Notes
newMainparameter is not used in case ifargparse_completionversion is defined.
Return value
See CLI.mainComplete and CLI.parseArgs.
newMain parameter
newMain parameter in CLI API is a substitution for classic main function with the following differences:
Its first parameter has type of a command struct that is passed to
CLIAPI. This parameter is filled with the data parsed from actual command line.... newMain(COMMAND command)It might have optional second parameter of type
string[]that receives unknown command line arguments.... newMain(COMMAND command, string[] unrecognizedArgs)newMaincan optionally returnintvalue. In this case,argparsewill return that value frommainfunction.