Releases: andrey-zherikov/argparse
v2.0.2
v2.0.1
v2.0.0
Changes since previous major version
Breaking changes
-
Changes in
Config:-
Custom error handler function (
Config.errorHandler) now receives message text with ANSI styling if styling is enabled. One can useargparse.ansi.getUnstyledTextfunction to remove any styling - this function returns a range of unstyledstringobjects which can be used as is orjoin'ed into a string if needed:message.getUnstyledText.join. -
Config.addHelpis renamed toConfig.addHelpArgument. -
Config.arraySepis renamed toConfig.valueSep. -
Config.caseSensitiveis replaced withConfig.caseSensitiveShortName,Config.caseSensitiveLongNameandConfig.caseSensitiveSubCommand.
There is also a "new"Config.caseSensitivefunction/property helper that sets all above settings to a specific value. -
Config.endOfArgsis renamed toConfig.endOfNamedArgs. -
Config.helpStyleis renamed toConfig.styling. -
Config.namedArgCharis replaced withConfig.shortNamePrefixandConfig.longNamePrefix. -
Config.stylingModeremains unchanged even if it was set toautodetect. One should checkansiStylingArgument.stdoutStyling
oransiStylingArgument.stderrStylingto determine whether styling should be enabled.
-
-
Style.namedArgumentNameis renamed toStyle.argumentName. -
Underlying type of
ansiStylingArgumentargument is changed. It supports detection and tracking of styling for stdout and stderr
separately: useansiStylingArgument.stdoutStylingandansiStylingArgument.stderrStylingfor this.
Also it can be cast to boolean which is an equivalent toansiStylingArgument.stdoutStyling.So if you use it:
static auto color = ansiStylingArgument;
then you should replace
if(args.color == Config.StylingMode.on)
with
if(args.color) // or if(args.color.stdoutStyling) // or if(ansiStylingArgument.stdoutStyling)
-
@SubCommandsUDA is removed. One should useSubCommandtemplate instead ofSumType.
All calls tostd.sumtype.matchshould be replaced withmatchCmd.Simply replace
@SubCommands SumType!(CMD1, CMD2, Default!CMD3) cmd; ... cmd.match!...;
with
SubCommand!(CMD1, CMD2, Default!CMD3) cmd; ... cmd.matchCmd!...;
-
@TrailingArgumentsUDA 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@PositionalArgumentinstead of@TrailingArguments. -
Functions for parsing customization (
PreValidation,Parse,ValidationandAction) 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'; })
-
HideFromHelpis renamed toHiddenand now also hides an argument from shell completion. -
AllowedValuesnow accepts values as run-time parameters, not as template parameters.For example, replace this
.AllowedValues!(["value1", "value2", value3"])
with
.AllowedValues("value1", "value2", value3")
-
AllowNoValuenow accepts a value as run-time parameter, not as template parameter.For example, replace this
.AllowNoValue!"myvalue"
with
.AllowNoValue("myvalue") -
RequireNoValueis renamed toForceNoValueand now accepts a value as run-time parameter, not as template parameter.For example, replace this
.RequireNoValue!"myvalue"
with
.ForceNoValue("myvalue") -
ArgumentValueis renamed toAllowedValues.For example, replace this
.ArgumentValue("value1", "value2")
with
.AllowedValues("value1", "value2")
-
parseArgstemplate functions that receivednewMaintemplate argument was removed. One should use eithermaintemplate mixin
or non-templatedResult parseArgs(ref COMMAND receiver, string[] args)function. -
Removed
Result.Error(T...)(string msg, T extraArgs)function. Users should explicitly specify result code using
Result.Error(T...)(int resultCode, string msg, T extraArgs). -
Dropped support for DMD-2.099.
Enhancements and bug fixes
-
Parsing procedure follows POSIX.1-2024 meaning that
argparsenow
allows at most one value per appearance of named argument in command line. This means thatprog --param value1 value2
is not working anymore by default ---parammust be repeated:prog --param value1 --param value2.
However,prog --param value1,value2still works.To make
argparse2.* behave like 1.*, one should setConfig.variadicNamedArgumentto true.
See documentation for details. -
Fix for
Command()UDA:ArrayIndexErroris not thrown anymore. -
Error messages are printed with
Config.stylingand now have the same styling as help text. -
New
errorMessagePrefixmember inConfig.stylingthat 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.shortNamePrefix(dash-by default) orConfig.longNamePrefix(double-dash--by default).
-
Functions for parsing customization (
PreValidation,Parse,ValidationandAction) can now returnResultthroughResult.SuccessorResult.Errorand provide error message if needed. -
Fixes for bundling of single-letter arguments.
For example, the following cases are supported forbool 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
-
Values of multi-value positional argument can now be interleaved with named arguments.
For example, the following is the same whenarg1andarg2are values for singlestring[] argspositional argument:--flag arg1 arg2arg1 --flag arg2arg1 arg2 --flag
-
Long and short names of arguments are now separated:
- Short names are single-character names by default. This can be overridden by explicitly specifying short and long names in
NamedArgumentUDA. - Short names can be specified with short prefix only (e.g.
-). - Long names can be specified with long prefix only (e.g.
--).
- Short names are single-character names by default. This can be overridden by explicitly specifying short and long names in
-
Removed support for delegate in
Config.errorHandler,Description,ShortDescription,UsageandEpilogbecause of compiler'sclosures are not yet supported in CTFE. -
Added new
Config.assignKeyValueCharparameter to customize assign character inkey=valuesyntax for arguments with associative array type. -
Added new
Config.errorExitCodeparameter to customize exit code in case of parsing error. -
Added support of
@PositionalArgumentwithout explicit position. In this case positions are determined in the order of declarations of members. -
Added support for environment fallback, so adding
EnvFallback("VAR")to an argument would automatically populate the argument with the content
of theVARenvironment variable if nothing is provided on the command line. -
Fix for empty argument values: they are now non-null empty strings (
""instead ofstring.initbefore). -
Added support for classes.
Other changes
- 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 buildand 4x better fordub test. - New documentation
v2.0.0-rc2
Bug fixes
- Fix for 'main' template.
v2.0.0-rc1
Breaking changes
-
Changes in
Config:-
Custom error handler function (
Config.errorHandler) now receives message text with ANSI styling if styling is enabled. One can useargparse.ansi.getUnstyledTextfunction to remove any styling - this function returns a range of unstyledstringobjects which can be used as is orjoin'ed into a string if needed:message.getUnstyledText.join. -
Config.addHelpis renamed toConfig.addHelpArgument. -
Config.arraySepis renamed toConfig.valueSep. -
Config.caseSensitiveis replaced withConfig.caseSensitiveShortName,Config.caseSensitiveLongNameandConfig.caseSensitiveSubCommand.
There is also a "new"Config.caseSensitivefunction/property helper that sets all above settings to a specific value. -
Config.endOfArgsis renamed toConfig.endOfNamedArgs. -
Config.helpStyleis renamed toConfig.styling. -
Config.namedArgCharis replaced withConfig.shortNamePrefixandConfig.longNamePrefix.
-
-
Style.namedArgumentNameis renamed toStyle.argumentName. -
Underlying type of
ansiStylingArgumentargument 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) -
@SubCommandsUDA is removed. One should useSubCommandtemplate instead ofSumTypeSimply replace
@SubCommands SumType!(CMD1, CMD2, Default!CMD3) cmd;
with
SubCommand!(CMD1, CMD2, Default!CMD3) cmd;
-
@TrailingArgumentsUDA 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@PositionalArgumentinstead of@TrailingArguments. -
Functions for parsing customization (
PreValidation,Parse,ValidationandAction) 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'; })
-
HideFromHelpis renamed toHiddenand now also hides an argument from shell completion. -
AllowedValuesnow accepts values as run-time parameters, not as template parameters.For example, replace this
.AllowedValues!(["value1", "value2", value3"])
with
.AllowedValues("value1", "value2", value3")
-
AllowNoValuenow accepts a value as run-time parameter, not as template parameter.For example, replace this
.AllowNoValue!"myvalue"
with
.AllowNoValue("myvalue") -
RequireNoValueis renamed toForceNoValueand now accepts a value as run-time parameter, not as template parameter.For example, replace this
.RequireNoValue!"myvalue"
with
.ForceNoValue("myvalue") -
ArgumentValueis renamed toAllowedValues.For example, replace this
.ArgumentValue("value1", "value2")
with
.AllowedValues("value1", "value2")
-
parseArgstemplate functions that receivednewMaintemplate argument was removed. One should use eithermaintemplate mixin
or non-templatedResult parseArgs(ref COMMAND receiver, string[] args)function. -
Dropped support for DMD-2.099.
Enhancements and bug fixes
-
Parsing procedure follows POSIX.1-2024 meaning that
argparsenow
allows at most one value per appearance of named argument in command line. This means thatprog --param value1 value2
is not working anymore by default ---parammust be repeated:prog --param value1 --param value2.
However,prog --param value1,value2still works.To make
argparse2.* behave like 1.*, one should setConfig.variadicNamedArgumentto true.
See documentation for details. -
Fix for
Command()UDA:ArrayIndexErroris not thrown anymore. -
Error messages are printed with
Config.stylingand now have the same styling as help text. -
New
errorMessagePrefixmember inConfig.stylingthat 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.shortNamePrefix(dash-by default) orConfig.longNamePrefix(double-dash--by default).
-
Functions for parsing customization (
PreValidation,Parse,ValidationandAction) can now returnResultthroughResult.SuccessorResult.Errorand provide error message if needed. -
Fixes for bundling of single-letter arguments.
For example, the following cases are supported forbool 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,UsageandEpilogbecause of compiler'sclosures are not yet supported in CTFE. -
Long and short names of arguments are now separated:
- Short names are single-character names by default. This can be overridden by explicitly specifying short and long names in
NamedArgumentUDA. - Short names can be specified with short prefix only (e.g.
-). - Long names can be specified with long prefix only (e.g.
--).
- Short names are single-character names by default. This can be overridden by explicitly specifying short and long names in
-
Added new
Config.assignKeyValueCharparameter to customize assign character inkey=valuesyntax for arguments with associative array type. -
Added support of
@PositionalArgumentwithout explicit position. In this case positions are determined in the order of declarations of members.
Other changes
- 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 buildand 4x better fordub test. - New documentation
v1.4.1
v1.4.0
Enhancements
CLI.maincan now accept struct with disabled copy constructor.- Main function passed to
CLI.maincan accept parameter by reference. - Boolean values (
yes,y,true,no,nandfalse) are now parsed case-insensitively. - New checks:
- Argument name can't start with
Config.namedArgChar(dash-by default). - For positional arguments within a command:
- There should be no gaps in indexes, e.g.
0,1,3,4indexes are not allowed because2is missed. - Indexes should be unique, e.g.
0,1,2,3,2indexes are not allowed because2is repeated. - Number of values should be fixed (i.e. minimum number of values should be the same as maximum number of values) unless it's the last positional argument.
- Required positional arguments must go before optional positional arguments.
- Optional positional arguments are not allowed if command has default subcommand.
- There should be no gaps in indexes, e.g.
- Argument name can't start with
v1.3.0
Enhancements
- You can now customize what values are accepted in command line for
enumtypes:struct T { enum Fruit { apple, @ArgumentValue("no-apple","noapple") noapple }; Fruit a; } assert(CLI!T.parseArgs!((T t) { assert(t == T(T.Fruit.apple)); })(["-a","apple"]) == 0); assert(CLI!T.parseArgs!((T t) { assert(t == T(T.Fruit.noapple)); })(["-a=no-apple"]) == 0); assert(CLI!T.parseArgs!((T t) { assert(t == T(T.Fruit.noapple)); })(["-a","noapple"]) == 0);
Other changes
- Code has been widely refactored to have more clear responsibility separation between components
- New testing mode in CI:
-preview=in
v1.2.0
Enhancements
- Add support of custom types:
struct Value { string a; } struct T { @(NamedArgument.Parse!((string s) { return Value(s); })) Value s; }
Bug fixes
-
Fix for the case when main program is attributed with
Commandwithout name:@(Command.Description("Description of main program")) struct Program { ... }
-
Ignore arguments with long invocation text during indent calculation for help text:
Optional arguments: -x X 1 x --yyyyyyyyyyyy YYYYYYYYYYYY 12 ys -h, --help Show this help message and exit
v1.1.1
Bug fixes
- Fix for positional argument name where
"name"was ignored:@PositionalArgument(0, "name") string param