-fsanitize=unsigned-integer-overflow
happy #328ArgumentParser::add_hidden_alias_for()
method #330Argument::store_into()
functions #331dry_run
argument to Argument::consume()
, and change ArgumentParser
private section to protected #332Argument::hidden()
method to prevent an argument from appearing in usage or help #336Argument::store_into(std::vector<int> &var)
method #3431:*
cardinality followed by another positional argument with 1:1 #344Argument::store_into(std::set<int||string> &var)
method #348do_from_chars()
: initialize variable to fix Coverity Scan warning #353parse_args()
: work around GCC 12 warning bug. #359Published by p-ranav 12 months ago
mutually_exclusive_arguments
#301, README
choices
argument support. #277, README
0b101
#306is_subcommand_used
overload that accepts a subcommand parser #233exit_on_default_arguments
parameter to ArgumentParser #264ArgumentParser
copy and move constructors as deleted #304std::string_view
being identified as a container https://github.com/p-ranav/argparse/pull/229
char[]
default values #253std::numeric_limits<std::size_t>::max)()}
error #263clang-tidy
to PRs https://github.com/p-ranav/argparse/pull/215
-Wpedantic
, -Werror
and -Wextra
for compilation in gcc. Fixed warnings #292Published by p-ranav about 2 years ago
Thanks @ndevenish and @skrobinson
parse_known_args
#201-Wsign-conversion
to the tests build and removed implicit conversion warnings #202--option=value
form of arguments #203-Wshadow
and -Wconversion
to CXX_FLAGS and fixed warnings #204prefix_chars
and assign_chars
support for better option-value syntax #205Published by p-ranav about 2 years ago
Many programs split up their functionality into a number of sub-commands, for example, the git
program can invoke sub-commands like git checkout
, git add
, and git commit
. Splitting up functionality this way can be a particularly good idea when a program performs several different functions which require different kinds of command-line arguments. ArgumentParser
now supports the creation of such sub-commands with add_subparser()
.
#include <argparse/argparse.hpp>
int main(int argc, char *argv[]) {
argparse::ArgumentParser program("git");
// git add subparser
argparse::ArgumentParser add_command("add");
add_command.add_argument("files")
.help("Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files.")
.remaining();
// git commit subparser
argparse::ArgumentParser commit_command("commit");
commit_command.add_argument("-a", "--all")
.help("Tell the command to automatically stage files that have been modified and deleted.")
.default_value(false)
.implicit_value(true);
commit_command.add_argument("-m", "--message")
.help("Use the given <msg> as the commit message.");
// git cat-file subparser
argparse::ArgumentParser catfile_command("cat-file");
catfile_command.add_argument("-t")
.help("Instead of the content, show the object type identified by <object>.");
catfile_command.add_argument("-p")
.help("Pretty-print the contents of <object> based on its type.");
// git submodule subparser
argparse::ArgumentParser submodule_command("submodule");
argparse::ArgumentParser submodule_update_command("update");
submodule_update_command.add_argument("--init")
.default_value(false)
.implicit_value(true);
submodule_update_command.add_argument("--recursive")
.default_value(false)
.implicit_value(true);
submodule_command.add_subparser(submodule_update_command);
// Add the subcommands to the parent parser
program.add_subparser(add_command);
program.add_subparser(commit_command);
program.add_subparser(catfile_command);
program.add_subparser(submodule_command);
// Parse args
try {
program.parse_args(argc, argv);
}
catch (const std::runtime_error& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
std::exit(1);
}
// Use arguments
}
foo@bar:/home/dev/$ ./git --help
Usage: git [options] <command> [<args>]
Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
Subcommands:
add Add file contents to the index
cat-file Provide content or type and size information for repository objects
commit Record changes to the repository
submodule Initialize, update or inspect submodules
foo@bar:/home/dev/$ ./git add --help
Usage: git add [options] files
Add file contents to the index
Positional arguments:
files Files to add content from. Fileglobs (e.g. *.c) can be given to add all matching files.
Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
foo@bar:/home/dev/$ ./git submodule --help
Usage: git submodule [options] <command> [<args>]
Initialize, update or inspect submodules
Optional arguments:
-h --help shows help message and exits [default: false]
-v --version prints version information and exits [default: false]
Subcommands:
update Update the registered submodules to match what the superproject expects
When a help message is requested from a subparser, only the help for that particular parser will be printed. The help message will not include parent parser or sibling parser messages.
Additionally, every parser has a .is_subcommand_used("<command_name>")
member function to check if a subcommand was used.
You can find relevant unit tests here.
Published by p-ranav about 2 years ago
epilog
#186clang-cl
build #189back_inserter
#191any_cast
#192Published by p-ranav over 2 years ago
Thanks @hokacci
You can now make a variable length list of arguments with the .nargs
.
Below are some examples.
program.add_argument("--input_files")
.nargs(1, 3); // This accepts 1 to 3 arguments.
Some useful patterns are defined like "?", "*", "+" of argparse in Python.
program.add_argument("--input_files")
.nargs(argparse::nargs_pattern::any); // "*" in Python. This accepts any number of arguments including 0.
program.add_argument("--input_files")
.nargs(argparse::nargs_pattern::at_least_one); // "+" in Python. This accepts one or more number of arguments.
program.add_argument("--input_files")
.nargs(argparse::nargs_pattern::optional); // "?" in Python. This accepts an argument optionally.
Published by p-ranav over 2 years ago
<utility>
include https://github.com/p-ranav/argparse/commit/95d48506833624e28fa68fdab5cfeeb98b05edff
Published by p-ranav over 2 years ago
Published by p-ranav over 2 years ago
cmake_minimum_required
to 3.12.4
exit()
to std::exit()
https://github.com/p-ranav/argparse/commit/8772b37aabf9c3679c6b346c113fc7b82a247452
Published by p-ranav about 3 years ago
Published by p-ranav almost 5 years ago
Published by p-ranav about 5 years ago
required
arguments: https://github.com/p-ranav/argparse/pull/33 and https://github.com/p-ranav/argparse/pull/34
required
arguments: https://github.com/p-ranav/argparse/issues/35
Published by p-ranav over 5 years ago
Published by p-ranav over 5 years ago
Published by p-ranav over 5 years ago
Published by p-ranav over 5 years ago
Published by p-ranav over 5 years ago