Argparse Versions Save

Argument Parser for Modern C++

v3.0

6 months ago

Features / Enhancements

  • Added support for mutually_exclusive_arguments #301, README
  • Added C++20 module #290
  • Added built-in choices argument support. #277, README
  • Added support for binary notation, e.g., 0b101 #306
  • Added is_subcommand_used overload that accepts a subcommand parser #233
  • Added exit_on_default_arguments parameter to ArgumentParser #264
  • A (hide-from-help) suppress flag for subcommands #273
  • Allowed check to see if ArgumentParser has parsed values https://github.com/p-ranav/argparse/pull/218
  • Implemented column-aligned multi-line help message for arguments #259

Fixes

  • Marked ArgumentParser copy and move constructors as deleted #304
  • Fixed error text when multiple positional arguments are expected https://github.com/p-ranav/argparse/pull/209
  • Fixed std::string_view being identified as a container https://github.com/p-ranav/argparse/pull/229
  • Fixed crash with char[] default values #253
  • Resolves a std::numeric_limits<std::size_t>::max)()} error #263
  • Fixed issue #248: Align multiline help messages #268
  • Various maintenance fixes #254

Other

  • Added clang-tidy to PRs https://github.com/p-ranav/argparse/pull/215
  • No install when used as third party #231
  • Install cmake export file in CMAKE_INSTALL_DIR #255
  • CMakefile: Use -Wpedantic, -Werror and -Wextra for compilation in gcc. Fixed warnings #292
  • Updates to README cmake FetchContent section #293
  • More descriptive parse_number errors #297

v2.9

1 year ago

Thanks @ndevenish and @skrobinson

  • Added support for parse_known_args #201
  • Added -Wsign-conversion to the tests build and removed implicit conversion warnings #202
  • Allow --option=value form of arguments #203
  • Added -Wshadow and -Wconversion to CXX_FLAGS and fixed warnings #204
  • Added prefix_chars and assign_chars support for better option-value syntax #205
  • Improved help, metavar, subcommands, and samples #206

v2.8

1 year ago

Subcommands

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.

v2.7

1 year ago
  • Added empty line above epilog #186
  • Update CI hosts and fix clang-cl build #189
  • Remove unnecessary back_inserter #191
  • Use references for any_cast #192

v2.6

1 year ago

Merged pull request https://github.com/p-ranav/argparse/pull/125 - Improve nargs

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.

v2.3

2 years ago

v2.2

2 years ago
  • Simplify parsing numeric arguments with .scan #65
  • Get arguments in optional<T> with .present<T>() #68
  • Simplify a few internals #71
  • Add the option to add text before and after help output #72
  • Avoid use of cmd.exe in Travis #77
  • Fix incorrect message when mUsedName is empty #79
  • Make ArgumentParser::add_*() functions working on the parser itself chainable #82
  • CMakeLists.txt : add export #86
  • Fix help if required and def-value. Fixes #89. #90
  • Show default value for arg in help message #92
  • nicer usage text for required arg #93
  • Qualify iterator functions #97
  • misc clean ups #98
  • Add Argument.append method to allow repeated argument use #99
  • Add ArgumentParser.is_used to discern user-supplied values from defaults #100
  • Allow user to limit version argument to --version #103
  • Added packaging using CPack and generation of pkg-config files. #107
  • Const-correct ArgumentParser #108
  • Fix std::min conflict with min/max definitions from windows.h #109
  • Replace size_t to std::size_t. #110
  • Some cleanup in CPack packaging #115
  • Document and use Argument.scan where possible #121

v2.1

2 years ago
  • Simplify parsing numeric arguments with .scan #65
  • Get arguments in optional<T> with .present<T>() #68
  • Simplify a few internals #71
  • Add the option to add text before and after help output #72
  • Avoid use of cmd.exe in Travis #77
  • Fix incorrect message when mUsedName is empty #79
  • Make ArgumentParser::add_*() functions working on the parser itself chainable #82
  • CMakeLists.txt : add export #86
  • Fix help if required and def-value. Fixes #89. #90
  • Show default value for arg in help message #92
  • nicer usage text for required arg #93
  • Qualify iterator functions #97
  • misc clean ups #98
  • Add Argument.append method to allow repeated argument use #99
  • Add ArgumentParser.is_used to discern user-supplied values from defaults #100
  • Allow user to limit version argument to --version #103
  • Added packaging using CPack and generation of pkg-config files. #107
  • Const-correct ArgumentParser #108
  • Fix std::min conflict with min/max definitions from windows.h #109
  • Replace size_t to std::size_t. #110
  • Some cleanup in CPack packaging #115
  • Document and use Argument.scan where possible #121