Command-line parser with flags, options, positionals, subcommands and generated help.
More...
|
| | ArgParser (std::string programName="", std::string description="") |
| | Creates a parser; programName defaults to the executable name.
|
| |
| Argument & | addFlag (std::vector< std::string > flags, std::string description) |
| | Adds a flag without value; short flags can be bundled, like -abc.
|
| |
| Argument & | addOption (std::vector< std::string > flags, std::string description) |
| | Adds an option taking a value: --name value, --name=value, -n value or -n5.
|
| |
| Argument & | addPositional (std::string name, std::string description) |
| | Adds a positional argument; positionals are filled in declaration order.
|
| |
| ArgParser & | addSubcommand (std::string name, std::string description) |
| | Adds a subcommand with its own arguments, and returns its parser.
|
| |
| void | setVersion (std::string version) |
| | Enables --version, which prints the program name and version.
|
| |
| void | setEpilog (std::string text) |
| | Text printed at the end of the help.
|
| |
| bool | loadConfig (const std::filesystem::path &file) |
| | Loads fallback values from a file of name = value lines, name being a flag without dashes.
|
| |
| ParseResult | parse (int argc, const char *const *argv) |
| | Parses the arguments of main.
|
| |
| ParseResult | parse (const std::vector< std::string > &args) |
| | Parses args, without the program name.
|
| |
| bool | isSet (std::string_view name) const |
| | Shortcut for Argument::isSet(); name is a flag (-v, --verbose) or a name (verbose).
|
| |
| std::size_t | count (std::string_view name) const |
| | Shortcut for Argument::count().
|
| |
| template<typename T = std::string> |
| std::optional< T > | get (std::string_view name) const |
| | Shortcut for Argument::get().
|
| |
| template<typename T = std::string> |
| std::vector< T > | getAll (std::string_view name) const |
| | Shortcut for Argument::getAll().
|
| |
| const std::string & | activeSubcommand () const noexcept |
| | Name of the subcommand given on the command line, or an empty string.
|
| |
| const ArgParser * | activeSubparser () const |
| | Parser of the active subcommand, or nullptr.
|
| |
| std::string | help () const |
| | The generated help text.
|
| |
| void | printHelp () const |
| | Prints help() to stdout.
|
| |
Command-line parser with flags, options, positionals, subcommands and generated help.
-h/--help and --version are built in. Values not given on the command line fall back on their environment variable, then the config file, then their default value.
parser.addFlag({"-v", "--verbose"}, "Print more details");
parser.addOption({"-n", "--count"}, "How many times").integer().defaultValue(1);
if (auto result = parser.parse(argc, argv); !result) {
result.print();
return result.exitCode();
}
int count = parser.get<
int>(
"--count").value_or(1);
Command-line parser with flags, options, positionals, subcommands and generated help.
Definition ArgParser.hpp:34
std::size_t count(std::string_view name) const
Shortcut for Argument::count().
Definition ArgParser.hpp:69
- Examples
- parser.cpp, and quickstart.cpp.