#include <argparse.h>
|
template<typename T > |
T | Arg (std::string_view name, std::string_view description) |
|
template<typename T > |
T | Kwarg (std::string_view keys, T &&default_value, std::string_view description) |
|
bool | Flag (std::string_view name, bool default_value, std::string_view description) |
|
virtual std::string_view | description () const |
|
Base Args class.
Subclass this class and use the Arg()
, Kwarg()
, and Flag()
methods to declare and initialize the fields of the subclass.
◆ Arg()
template<typename T >
T ctrl_utils::Args::Arg |
( |
std::string_view |
name, |
|
|
std::string_view |
description |
|
) |
| |
|
inlineprotected |
Declares a positional argument.
Prints an error if the argument cannot be parsed.
- Parameters
-
name | Name of the argument (only used to generate the help string). |
description | Description of the argument. |
- Returns
- The parsed positional argument.
◆ description()
virtual std::string_view ctrl_utils::Args::description |
( |
| ) |
const |
|
inlineprotectedvirtual |
Optional app description string.
◆ Flag()
bool ctrl_utils::Args::Flag |
( |
std::string_view |
name, |
|
|
bool |
default_value, |
|
|
std::string_view |
description |
|
) |
| |
|
inlineprotected |
Declares a flag argument.
Positive flags use the form --flag
, and negative flags use the form --no-flag
. Prints an error if the argument cannot be parsed.
- Parameters
-
name | Name of the flag (e.g. "flag"). |
default_value | Default value of the flag. |
description | Description of the argument. |
- Returns
- The parsed flag argument.
◆ help_string()
const std::string& ctrl_utils::Args::help_string |
( |
| ) |
const |
|
inline |
Help string generated from the defined arguments.
◆ Kwarg()
template<typename T >
T ctrl_utils::Args::Kwarg |
( |
std::string_view |
keys, |
|
|
T && |
default_value, |
|
|
std::string_view |
description |
|
) |
| |
|
inlineprotected |
Declares a keyword argument.
Single-character keywords use the form -k
, and multi-character keywords use the form --keyword
. Prints an error if the argument cannot be parsed.
- Parameters
-
keys | Comma-separated string of keywords (e.g. "k,keyword"). |
default_value | Default value of the argument. |
description | Description of the argument. |
- Returns
- The parsed keyword argument.
◆ operator<<
std::ostream& operator<< |
( |
std::ostream & |
os, |
|
|
const Args & |
args |
|
) |
| |
|
friend |
Prints the parsed fields of the Args object.
◆ ParseArgs
template<typename Derived >
Parses the arguments.
Example:
std::string name = Arg<std::string>("name", "Your name.");
int num_repeat = Kwarg<int>("n,num-repeat", 1,
"Number of times to repeat the greeting");
bool print_name = Flag<bool>("print-name", true, "Print your name.");
};
int main(int argc, char* argv[]) {
if (!args) return 1;
const std::string entity = args->print_name ? args->name : "world";
for (int i = 0; i < args->num_repeat; i++) {
std::cout << "Hello " << entity << "!" << std::endl;
}
return 0;
}
Definition: argparse.h:83
Definition: optional.h:30
Definition: ctrl_utils.cc:18
- Returns
- A child class of ctrl_utils::Args with its fields populated with the command line arguments, or an empty optional if the parsing fails.
The documentation for this class was generated from the following file:
- /home/runner/work/ctrl-utils/ctrl-utils/include/ctrl_utils/argparse.h