A simple command-line argument parser.
More...
#include <simple_arg.hpp>
|
| struct | Opt |
| | An option (named or positional). More...
|
|
| | SimpleArg (std::string p) |
| | Construct a new Simple Arg object.
|
| template<typename T> |
| void | add (char s, std::string l, std::string d, bool req=false, T def=T{}, std::vector< std::string > choices={}) |
| | Add a named argument.
|
| template<typename T> |
| void | addPositional (std::string l, std::string d, std::vector< std::string > choices={}) |
| | Add a positional argument.
|
| bool | parse (int argc, char **argv) |
| | Parse the command-line arguments.
|
| template<typename T> |
| T | get (const std::string &name) const |
| | Get the value of a named argument.
|
| bool | help () const |
| | Check if help was requested.
|
| bool | errors () const |
| | Check if there were any errors during parsing.
|
| std::string | errorMsg () const |
| | Get the error message.
|
| template<typename T> |
| T | getPositional (size_t idx) const |
| | Get the Positional object.
|
| void | printHelp (std::ostream &os=std::cout) const |
| | Print the help message.
|
|
|
std::string | prog |
| | The name of the program (usually argv[0]).
|
|
std::vector< Opt > | opts |
| | The list of named options.
|
|
std::vector< Opt > | positionals |
| | The list of positional options.
|
|
std::map< std::string, std::string > | vals |
| | The parsed values.
|
|
std::map< std::string, bool > | flags |
| | The parsed flags.
|
|
std::vector< std::string > | errs |
| | The list of errors encountered during parsing.
|
|
|
bool | store (const Opt &o, const std::string &key, const std::string &val) |
A simple command-line argument parser.
◆ SimpleArg()
| SimpleArg::SimpleArg |
( |
std::string | p | ) |
|
|
inline |
Construct a new Simple Arg object.
- Parameters
-
| p | Name of the program (usually argv[0]). |
◆ add()
template<typename T>
| void SimpleArg::add |
( |
char | s, |
|
|
std::string | l, |
|
|
std::string | d, |
|
|
bool | req = false, |
|
|
T | def = T{}, |
|
|
std::vector< std::string > | choices = {} ) |
|
inline |
Add a named argument.
- Template Parameters
-
| T | Type of the argument. Only bool, int, float, and std::string are supported. |
- Parameters
-
| s | The short name (e.g. 'h' for -h). |
| l | The long name (e.g. "help" for –help). |
| d | Description of the argument. |
| req | Whether the argument is required (true) or optional (false). |
| def | Default value (only if req is false). |
| choices | Allowed choices (only for enum types). |
◆ addPositional()
template<typename T>
| void SimpleArg::addPositional |
( |
std::string | l, |
|
|
std::string | d, |
|
|
std::vector< std::string > | choices = {} ) |
|
inline |
Add a positional argument.
- Template Parameters
-
| T | Type of the argument. Only int, float, and std::string are supported. |
- Parameters
-
| l | The name of the positional argument (for help messages). |
| d | Description of the argument. |
| choices | Allowed choices (only for enum types). |
◆ errorMsg()
| std::string SimpleArg::errorMsg |
( |
| ) |
const |
|
inline |
Get the error message.
- Returns
- std::string The error message, or an empty string if there were no errors.
◆ errors()
| bool SimpleArg::errors |
( |
| ) |
const |
|
inline |
Check if there were any errors during parsing.
- Returns
- true if there were errors, false otherwise.
◆ get()
template<typename T>
| T SimpleArg::get |
( |
const std::string & | name | ) |
const |
|
inline |
Get the value of a named argument.
- Template Parameters
-
| T | Type of the argument. Only bool, int, float, and std::string are supported. |
- Parameters
-
| name | The long name of the argument (e.g. "help" for –help). |
- Returns
- T The value of the argument.
◆ getPositional()
template<typename T>
| T SimpleArg::getPositional |
( |
size_t | idx | ) |
const |
|
inline |
Get the Positional object.
- Template Parameters
-
| T | Type of the positional argument. Only int, float, and std::string are supported. |
- Parameters
-
- Returns
- T The value of the positional argument at index idx.
◆ help()
| bool SimpleArg::help |
( |
| ) |
const |
|
inline |
Check if help was requested.
- Returns
- true if help was requested, false otherwise.
◆ parse()
| bool SimpleArg::parse |
( |
int | argc, |
|
|
char ** | argv ) |
|
inline |
Parse the command-line arguments.
- Parameters
-
| argc | The number of arguments (from main). |
| argv | The argument values (from main). |
- Returns
- true if parsing was successful, false otherwise.
◆ printHelp()
| void SimpleArg::printHelp |
( |
std::ostream & | os = std::cout | ) |
const |
|
inline |
Print the help message.
- Parameters
-
| os | The output stream to print to (default: std::cout). |
The documentation for this struct was generated from the following file: