ctrl-utils
Classes | Public Member Functions | Protected Member Functions | Friends | List of all members
ctrl_utils::Args Class Reference

#include <argparse.h>

Public Member Functions

const std::string & help_string () const
 

Protected Member Functions

template<typename T >
Arg (std::string_view name, std::string_view description)
 
template<typename 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
 

Friends

std::ostream & operator<< (std::ostream &os, const Args &args)
 
template<typename Derived >
std::optional< Derived > ParseArgs (int, char *[])
 

Detailed Description

Base Args class.

Subclass this class and use the Arg(), Kwarg(), and Flag() methods to declare and initialize the fields of the subclass.

Member Function Documentation

◆ 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
nameName of the argument (only used to generate the help string).
descriptionDescription 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
nameName of the flag (e.g. "flag").
default_valueDefault value of the flag.
descriptionDescription 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
keysComma-separated string of keywords (e.g. "k,keyword").
default_valueDefault value of the argument.
descriptionDescription of the argument.
Returns
The parsed keyword argument.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Args args 
)
friend

Prints the parsed fields of the Args object.

◆ ParseArgs

template<typename Derived >
std::optional<Derived> ParseArgs ( int  argc,
char *  argv[] 
)
friend

Parses the arguments.

Example:

struct Args : ctrl_utils::Args {
// Mandatory constructor boilerplate.
explicit Args(ctrl_utils::Args&& args)
: ctrl_utils::Args(std::move(args)) {}
// Create a positional string argument.
// E.g. `./hello joe`
std::string name = Arg<std::string>("name", "Your name.");
// Create an optional int argument.
// E.g. `./hello joe --num-repeat 5` or `./hello joe -n 5`.
int num_repeat = Kwarg<int>("n,num-repeat", 1,
"Number of times to repeat the greeting");
// Create a flag argument.
// E.g. `./bin joe -n 5 --print-name` or `./bin joe -n 5 --no-print-name`.
bool print_name = Flag<bool>("print-name", true, "Print your name.");
};
int main(int argc, char* argv[]) {
// Parse the arguments into an Args object.
std::optional<Args> args = ctrl_utils::ParseArgs<Args>(argc, argv);
// If the optional is empty, the args could not be parsed.
if (!args) return 1;
// Access the parsed args as normal fields in the struct.
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
Definition: chrono.h:15
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: