argparse ~update-codecov
Parser for command line arguments
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
Parser for command line arguments
argparse
is a flexible utility for D programming language to parse command line arguments.
[!WARNING] :warning: Please be aware that current HEAD contains breaking changes comparing to 1.*
:warning: This includes changes in documentation - documentation for 1.* version is here
Changes since 1.* version
<details> <summary>Changelog</summary>
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.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 arguments
For 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.
Enhancements and bug fixes
- 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
.
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 build
and 4x better fordub test
. - New documentation </details>
Features
- 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.
Documentation
Please find up-to-date documentation here.
- Registered by Andrey Zherikov
- ~update-codecov released 16 hours ago
- andrey-zherikov/argparse
- BSL-1.0
- Copyright © 2021, Andrey Zherikov
- Authors:
- Dependencies:
- none
- Versions:
-
1.4.0 2024-Oct-31 1.3.0 2023-May-10 1.2.0 2022-Sep-16 1.1.1 2022-Aug-29 1.1.0 2022-Aug-24 - Download Stats:
-
-
0 downloads today
-
5 downloads this week
-
47 downloads this month
-
2434 downloads total
-
- Score:
- 2.2
- Short URL:
- argparse.dub.pm