All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
gears::optparse::option Struct Reference

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.
 
optionoperator= (const option &other)
 Copy assignment.
 
 option (option &&)=default
 Move constructor.
 
optionoperator= (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...
 

Detailed Description

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.

Member Function Documentation

template<typename T >
const T& gears::optparse::option::get ( ) const
inline

Retrieves the internal value of the option. If the option has no value then an exception is thrown.

Exceptions
std::runtime_errorThrown if the option has not been parsed.
std::invalid_argumentThrown for invalid casting or if the option has no value.
Returns
The internal value.

Definition at line 172 of file option.hpp.

template<typename T >
const T& gears::optparse::option::get_or ( const typename std::remove_reference< T >::type &  def) const
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.

Returns
The internal value or a reasonable default.

Definition at line 193 of file option.hpp.

bool gears::optparse::option::is ( char  arg) const
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:

optparse::option x = { "help", 'h', "show message" };
x.is("help"); // true
x.is('h'); // true
x.is("stuff"); // false
Parameters
argLong or short option to check for.
Returns
true if the option can be represented by arg.

Definition at line 153 of file option.hpp.

std::string gears::optparse::option::metavar ( ) const
inlinenoexcept

Retrieves the internal value's metavar member. If takes_value returns false, then an empty string is returned.

Returns
The internal value's metavar member.

Definition at line 215 of file option.hpp.

size_t gears::optparse::option::nargs ( ) const
inlinenoexcept
Returns
Returns the number of arguments expected by the value. If no value is expected, i.e. takes_value is false, then 0 is returned.

Definition at line 128 of file option.hpp.

Friends And Related Function Documentation

enum trait : char
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.