Represents a command line option. More...
Public Member Functions | |
option (std::string name, std::string help="", value_type value=constant(true), trait flags=trait::none) | |
Constructs an option from a long name, a description, a value, and trait. | |
option (std::string name, char alias, std::string help="", value_type value=constant(true), trait flags=trait::none) | |
Constructs an option from a long name, short name, a description, a value, and trait. | |
option (char alias, std::string help="", value_type value=constant(true), trait flags=trait::none) | |
Constructs an option from a short name, description, a value, and traits. | |
option (const option &other) | |
Copy constructor. | |
option & | operator= (const option &other) |
Copy assignment. | |
option (option &&)=default | |
Move constructor. | |
option & | operator= (option &&)=default |
Move assignment. | |
~option ()=default | |
Destructor. | |
bool | takes_value () const noexcept |
Checks if the option accepts a value. | |
size_t | nargs () const noexcept |
Returns the number of arguments expected by the value. More... | |
template<typename T > | |
const T & | get () const |
Retrieves the internal value of the option. More... | |
template<typename T > | |
const T & | get_or (const typename std::remove_reference< T >::type &def) const noexcept |
Retrieves the internal value of the option or a reasonable default. More... | |
bool | is_active () const noexcept |
Checks if the option has gone through parsing. | |
std::string | metavar () const noexcept |
Retrieves the internal option's metavar member. More... | |
bool | is (char arg) const noexcept |
Checks if the option is represented by a long or short option. More... | |
bool | is (const std::string &arg) const noexcept |
Public Attributes | |
std::string | name |
The long name of the option, e.g. "help". | |
std::string | help |
The description used for the help output. | |
trait | flags |
The trait flags to modify the option's behaviour. | |
char | alias = '\0' |
The short name of the option, e.g. 'h'. | |
Related Functions | |
(Note that these are not member functions.) | |
enum | trait : char { none = 0, required = 1 << 0, hidden = 1 << 1 } |
List of traits that modify an option's behaviour. More... | |
A class that represents a command line option. A command line option is a command line argument used to specify information to the program.
Definition at line 53 of file option.hpp.
|
inline |
Retrieves the internal value of the option. If the option has no value then an exception is thrown.
std::runtime_error | Thrown if the option has not been parsed. |
std::invalid_argument | Thrown for invalid casting or if the option has no value. |
Definition at line 172 of file option.hpp.
|
inlinenoexcept |
Retrieves the internal value of the option or a reasonable default. If the option has no value then the default is returned. If the type is not a valid cast then the default is returned.
Definition at line 193 of file option.hpp.
|
inlinenoexcept |
Checks if the option is represented by a long or short option. This function is typically used to avoid code repetition when handling predicates dealing with checking for a long name or short name. This is a member function rather than an operator==
or operator!=
to disallow obfuscation and code duplication (e.g. "help" == op
vs op == "help"
).
Example usage:
arg | Long or short option to check for. |
true
if the option can be represented by arg. Definition at line 153 of file option.hpp.
|
inlinenoexcept |
Retrieves the internal value's metavar member. If takes_value returns false, then an empty string is returned.
Definition at line 215 of file option.hpp.
|
inlinenoexcept |
Definition at line 128 of file option.hpp.
|
related |
List of traits that modify an option's behaviour. This allows you to set whether an option should be required or hidden. These are bit flags so they should be used similar to any other type of bit flag such as, e.g., std::ios_base
.
Enumerator | |
---|---|
none |
Represents no traits being set. |
required |
The option is required to appear, or an error is thrown. |
hidden |
The option is suppressed from the default –help output. |
Definition at line 40 of file option.hpp.