Option++
2.0
C++ library for reading command-line options
|
Describes a valid program command-line option. More...
#include <option.hpp>
Public Types | |
enum | arg_type { string_arg, int_arg, uint_arg, double_arg } |
Holds the possible argument types. | |
Public Member Functions | |
option () noexcept | |
Default constructor. More... | |
option (char short_name) | |
Construct an option with a given short name. More... | |
option (const std::string &long_name, char short_name='\0', const std::string &description="", const std::string &arg_name="", bool arg_required=false) | |
Construct an option with given information. More... | |
option & | name (const std::string &long_name, char short_name='\0') |
Sets the long and short name for the option. More... | |
std::string | name () const noexcept |
Returns a name for the option. More... | |
option & | long_name (const std::string &name) |
Set the option's long name. More... | |
const std::string & | long_name () const noexcept |
Retrieve the option's long name. More... | |
option & | short_name (char name) noexcept |
Set the option's short name. More... | |
char | short_name () const noexcept |
Retrieve the option's short name. More... | |
option & | argument (const std::string &name, bool required=true) |
Set the option's argument information. More... | |
const std::string & | argument_name () const noexcept |
Retrieve the option's argument name. More... | |
bool | is_argument_required () const noexcept |
Return true if the argument is mandatory. More... | |
arg_type | argument_type () const noexcept |
Return the type of argument. More... | |
option & | bind_bool (bool *var) noexcept |
Designates a location to store whether the option was set. More... | |
option & | bind_string (std::string *var) noexcept |
Designates that the option should take a string argument which should be stored in *var . More... | |
option & | bind_int (int *var) noexcept |
Designates that the option should take an integer argument which should be stored in *var . More... | |
option & | bind_uint (unsigned int *var) noexcept |
Designates that the option should take an unsigned integer argument which should be stored in *var . More... | |
option & | bind_double (double *var) noexcept |
Designates that the option should take a double-precision floating point argument which should be stored in *var . More... | |
bool | has_bound_argument_variable () const noexcept |
Returns true if a variable has been bound to the option's argument. More... | |
void | write_bool (bool value) const noexcept |
Writes to the bound boolean variable that was specified in bind_bool . More... | |
void | write_string (const std::string &value) const |
Writes to the bound string variable that was specified in bind_string . More... | |
void | write_int (int value) const |
Writes to the bound integer variable that was specified in bind_int . More... | |
void | write_uint (unsigned int value) const |
Writes to the bound unsigned integer variable that was specified in bind_uint . More... | |
void | write_double (double value) const |
Writes to the bound double variable that was specified in bind_double . More... | |
option & | description (const std::string &desc) |
Set the option description. More... | |
const std::string & | description () const noexcept |
Retrieve the option description. More... | |
Describes a valid program command-line option.
A option
can hold information about a program option that the user can pass through the command line. Among this information, the most important are the long name and the short name.
If an option has a long name, then the user can specify that option using a double-hyphen. For example, --version
would specify an option with a long name of version
.
If an option has a short name, then the user can specify the option with a single hyphen. For example -v
would specify an option with a short name of v
. Short names are always a single character, and can be combined in a single command-line argument. For example, the command-line argument -xvf
specifies three different options: one with a short name of x
, another with short name v
, and another with f
.
Program options can accept arguments. These can be mandatory or optional. The argument name (set in the name
parameter of the argument
method) is used in the help text and usage string.
A description and a group name can be set as well. These are used in generating the program help text.
Note that many of the methods in the class return a reference to the current instance. This allows for convenient chaining. For example, to create an option with a long name of help
, a short name of ?
, and an appropriate description, one could write:
|
inlinenoexcept |
Default constructor.
Constructs an option without initial information.
|
inline |
Construct an option with a given short name.
Other fields are left empty.
short_name | The short name for the option. |
optionpp::option::option | ( | const std::string & | long_name, |
char | short_name = '\0' , |
||
const std::string & | description = "" , |
||
const std::string & | arg_name = "" , |
||
bool | arg_required = false |
||
) |
Construct an option with given information.
The argument, description, and group fields are left empty.
long_name | The long name for the option. |
short_name | The short name for the option. |
description | Description of the option. |
arg_name | Name of the option's argument, if any (usually uppercase). |
arg_required | Set to true if argument is mandatory. |
option & optionpp::option::argument | ( | const std::string & | name, |
bool | required = true |
||
) |
Set the option's argument information.
The name is used in the program help text and usage string.
name | Name of the argument (usually all uppercase). |
required | True if the option is mandatory, false if it is optional. |
|
inlinenoexcept |
Retrieve the option's argument name.
This is the name that is used in the help text.
|
inlinenoexcept |
Return the type of argument.
|
noexcept |
Designates a location to store whether the option was set.
If the option is encountered on the command line, the bound value is set to true. Otherwise, it is set to false.
var | Address of boolean to set. |
|
noexcept |
Designates that the option should take a double-precision floating point argument which should be stored in *var
.
If a non-numeric argument is given, then the parser
will throw a parse_error
exception.
var | Address of double to receive argument value. |
|
noexcept |
Designates that the option should take an integer argument which should be stored in *var
.
If a non-integer argument is given, then the parser
will throw a parse_error
exception.
var | Address of integer to receive argument value. |
|
noexcept |
Designates that the option should take a string argument which should be stored in *var
.
var | Address of string to receive argument value. |
|
noexcept |
Designates that the option should take an unsigned integer argument which should be stored in *var
.
If a non-integer argument is given, or if a negative integer is encountered, then the parser
will throw a parse_error
exception.
var | Address of unsigned int to receive argument value. |
|
inlinenoexcept |
Retrieve the option description.
|
inline |
Set the option description.
This description is used in generating the program help text.
desc | Description of the option. |
|
inlinenoexcept |
Returns true if a variable has been bound to the option's argument.
Note that binding a boolean value to the option does not affect the return value of this method.
|
inlinenoexcept |
Return true if the argument is mandatory.
|
inlinenoexcept |
Retrieve the option's long name.
|
inline |
Set the option's long name.
name | The long name to use. |
|
inlinenoexcept |
Returns a name for the option.
This will be the long name if it exists, otherwise the short name is used. If neither name is set, returns an empty string.
|
inline |
Sets the long and short name for the option.
long_name | Long name form for the option. |
short_name | Single-character short option name. |
|
inlinenoexcept |
Retrieve the option's short name.
|
inlinenoexcept |
Set the option's short name.
name | The character to use as the short name. |
|
noexcept |
Writes to the bound boolean variable that was specified in bind_bool
.
It is safe to call this function even when no boolean variable is currently bound. Doing so is effectively a no-op.
value | Value to write to the bound bool variable. |
void optionpp::option::write_double | ( | double | value | ) | const |
Writes to the bound double variable that was specified in bind_double
.
This method should not be called unless a double variable was previously bound. You can use the argument_type
method to check what type of argument the option expects.
type_error | If no double variable was bound. |
value | Value to write to the bound double variable. |
void optionpp::option::write_int | ( | int | value | ) | const |
Writes to the bound integer variable that was specified in bind_int
.
This method should not be called unless an int variable was previously bound. You can use the argument_type
method to check what type of argument the option expects.
type_error | If no int variable was bound. |
value | Value to write to the bound int variable. |
void optionpp::option::write_string | ( | const std::string & | value | ) | const |
Writes to the bound string variable that was specified in bind_string
.
This method should not be called unless a string variable was previously bound. You can use the argument_type
method to check what type of argument the option expects.
type_error | If no string variable was bound. |
value | Value to write to the bound string variable. |
void optionpp::option::write_uint | ( | unsigned int | value | ) | const |
Writes to the bound unsigned integer variable that was specified in bind_uint
.
This method should not be called unless an unsigned int variable was previously bound. You can use the argument_type
method to check what type of argument the option expects.
type_error | If no unsigned int variable was bound. |
value | Value to write to the bound unsigned int variable. |