argparse
is a flexible utility for D programming language to parse command line arguments.
Warning
Changelog
-
Changes in
Config
:-
Custom error handler function (
Config.errorHandler
) now receives message text with ANSI styling if styling is enabled. One can useargparse.ansi.getUnstyledText
function to remove any styling - this function returns a range of unstyledstring
objects which can be used as is orjoin
'ed into a string if needed:message.getUnstyledText.join
. -
Config.namedArgChar
is renamed toConfig.namedArgPrefix
. -
Config.endOfArgs
is renamed toConfig.endOfNamedArgs
. -
Config.helpStyle
is renamed toConfig.styling
. -
Config.addHelp
is renamed toConfig.addHelpArgument
. -
Config.arraySep
is renamed toConfig.valueSep
.
-
-
Style.namedArgumentName
is renamed toStyle.argumentName
. -
Underlying type of
ansiStylingArgument
argument is changed. It can now be directly cast to boolean instead comparing againstConfig.StylingMode
.So if you use it:
static auto color = ansiStylingArgument;
then you should replace
if(args.color == Config.StylingMode.on)
with
if(args.color)
-
@SubCommands
UDA is removed. One should useSubCommand
template instead ofSumType
Simply replace
@SubCommands SumType!(CMD1, CMD2, Default!CMD3) cmd;
with
SubCommand!(CMD1, CMD2, Default!CMD3) cmd;
-
@TrailingArguments
UDA is removed: all command line parameters that appear after double-dash--
are considered as positional arguments. So if those parameters are to be parsed, use@PositionalArgument
instead of@TrailingArguments
. -
Functions for parsing customization (
PreValidation
,Parse
,Validation
andAction
) now accept functions as runtime parameters instead of template argumentsFor example, replace this
.Parse !((string s) { return cast(char) s[1]; }) .Validation!((char v) { return v >= '0' && v <= '9'; })
with
.Parse ((string s) { return cast(char) s[1]; }) .Validation((char v) { return v >= '0' && v <= '9'; })
-
HideFromHelp
is renamed toHidden
and now also hides an argument from shell completion. -
Dropped support for DMD-2.099.
- Fix for
Command()
UDA:ArrayIndexError
is not thrown anymore. - Error messages are printed with
Config.styling
and now have the same styling as help text. - New
errorMessagePrefix
member inConfig.styling
that determines the style of "Error:" prefix in error messages. This prefix is printed in red by default. - New checks:
- Argument is not allowed to be in multiple argument groups.
- Subcommand name can't start with
Config.namedArgPrefix
(dash-
by default).
- Functions for parsing customization (
PreValidation
,Parse
,Validation
andAction
) can now returnResult
throughResult.Success
orResult.Error
and provide error message if needed. - Fixes for bundling of single-letter arguments.
For example, the following cases are supported for
bool b; string s;
arguments:./prog -b -s=abc
./prog -b -s abc
./prog -b -sabc
./prog -bsabc
./prog -bs=abc
- Fixes for parsing of multiple values. Only these formats are supported:
./prog --arg value1 value2 value3
./prog --arg=value1,value2,value3
- Removed support for delegate in
Config.errorHandler
,Description
,ShortDescription
,Usage
andEpilog
because of compiler'sclosures are not yet supported in CTFE
.
- Removed dependency on
std.regex
. - New code base: library implementation is almost fully rewritten (public API was not changed in this effort). Unnecessary templates were replaced with regular functions. As a result, compilation time and memory usage were improved: 2x better for
dub build
and 4x better fordub test
. - New documentation
- Positional arguments:
- Automatic type conversion of the value.
- Required by default, can be marked as optional.
- Named arguments:
- Multiple names are supported, including short (
-v
) and long (--verbose
) ones. - Case-sensitive/-insensitive parsing.
- Bundling of short names (
-vvv
is same as-v -v -v
). - Equals sign is accepted (
-v=debug
,--verbose=debug
). - Automatic type conversion of the value.
- Optional by default, can be marked as required.
- Multiple names are supported, including short (
- Support different types of destination data member:
- Scalar (e.g.,
int
,float
,bool
). - String arguments.
- Enum arguments.
- Array arguments.
- Hash (associative array) arguments.
- Callbacks.
- Scalar (e.g.,
- Different workflows are supported:
- Mixin to inject standard
main
function. - Parsing of known arguments only (returning not recognized ones).
- Enforcing that there are no unknown arguments provided.
- Mixin to inject standard
- Shell completion.
- Options terminator (e.g., parsing up to
--
leaving any argument specified after it). - Arguments groups.
- Subcommands.
- Fully customizable parsing:
- Raw (
string
) data validation (i.e., before parsing). - Custom conversion of argument value (
string
-> anydestination type
). - Validation of parsed data (i.e., after conversion to
destination type
). - Custom action on parsed data (doing something different from storing the parsed value in a member of destination object).
- Raw (
- ANSI colors and styles.
- Built-in reporting of error happened during argument parsing.
- Built-in help generation.
Please find up-to-date documentation here.