Calling the parser
argparse
provides CLI
template to call the parser covering different use cases. It has the following signatures:
template CLI(Config config, COMMAND)
– this is main template that provides multiple API (see below) for all supported use cases.template CLI(Config config, COMMANDS...)
– convenience wrapper of the previous template that providesmain
template mixin only for the simplest use case with subcommands. See Subcommands section for details.alias CLI(COMMANDS...) = CLI!(Config.init, COMMANDS)
– alias provided for convenience that allows using defaultConfig
, i.e.,config = Config.init
.
Wrapper for main
function
The recommended and most convenient way to use argparse
is through CLI!(...).main(alias newMain)
mixin template. It declares the standard main
function that parses command line arguments and calls provided newMain
function with an object that contains parsed arguments.
newMain
function must satisfy these requirements:
It must accept
COMMAND
type as a first parameter ifCLI
template is used with oneCOMMAND
.It must accept all
COMMANDS
types as a first parameter ifCLI
template is used with multipleCOMMANDS...
.argparse
usesstd.sumtype.match
for matching. Possible implementation of suchnewMain
function would be a function that is overridden for every command type fromCOMMANDS
. Another example would be a lambda that does compile-time checking of the type of the first parameter (see examples below for details).Optionally
newMain
function can take astring[]
parameter as a second argument. Providing such a function will mean thatargparse
will parse known arguments only and all unknown ones will be passed into the second parameter ofnewMain
function. IfnewMain
function doesn’t have such parameter, thenargparse
will error out if there is an unknown argument provided in command line.Optionally
newMain
can return a result that can be cast toint
. In this case, this result will be returned from standardmain
function.
Usage examples:
Call parser with custom main
function
In the case when wrapping of standard main
function does not fit the needs (e.g., some initialization has to be done before parsing the command line), argparse
offers CLI!(...).parseArgs
function:
int parseArgs(alias newMain)(string[] args, COMMAND initialValue = COMMAND.init)
Parameters:
newMain
– function that’s called with object of typeCOMMAND
as a first parameter that is filled with the data parsed from command line; optionally it can takestring[]
as a second parameter which will contain unknown arguments (see Wrapper for main function section for details).args
– raw command line arguments (excludingargv[0]
– first command line argument inmain
function).initialValue
– initial value for the object passed tonewMain
function.
Return value:
If there is an error happened during the parsing, then non-zero value is returned. In case of no error, if newMain
function returns a value that can be cast to int
, then this value is returned, or 0
otherwise.
Usage example:
Low-level calling of parser
For the cases when providing newMain
function is not possible or feasible, parseArgs
function can accept a reference to an object that receives the values of command line arguments:
Result parseArgs(ref COMMAND receiver, string[] args)
Parameters:
receiver
– object that is populated with parsed values.args
– raw command line arguments (excludingargv[0]
– first command line argument inmain
function).
Return value:
An object that can be cast to bool
to check whether the parsing was successful or not.
Usage example:
Partial argument parsing
Sometimes a program may only parse a few of the command line arguments and process the remaining arguments in some different way. In these cases, CLI!(...).parseKnownArgs
function can be used. It works much like CLI!(...).parseArgs
except that it does not produce an error when unknown arguments are present. It has the following signatures:
Result parseKnownArgs(ref COMMAND receiver, string[] args, out string[] unrecognizedArgs)
Parameters:
receiver
– the object that’s populated with parsed values.args
– raw command line arguments (excludingargv[0]
– first command line argument inmain
function).unrecognizedArgs
– raw command line arguments that were not parsed.
Return value:
An object that can be cast to
bool
to check whether the parsing was successful or not.Result parseKnownArgs(ref COMMAND receiver, ref string[] args)
Parameters:
receiver
– the object that’s populated with parsed values.args
– raw command line arguments that are modified to have parsed arguments removed (excludingargv[0]
– first command line argument inmain
function).
Return value:
An object that can be cast to
bool
to check whether the parsing was successful or not.
Usage example: