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

A container for holding options. More...

Public Member Functions

 option_set (bool help=true)
 Constructs an option_set. More...
 
 option_set (std::initializer_list< option > l)
 Constructs the option_set through std::initializer_list. More...
 
template<typename... Args>
void add (Args &&...args)
 Adds an option to the option_set. More...
 
template<typename Argument >
option_setremove (const Argument &arg)
 Removes an option from the option_set. More...
 
template<typename T , typename Argument >
const T & get (const Argument &arg) const
 Gets a value stored by an option. More...
 
template<typename T , typename Argument >
const T & get_or (const Argument &arg, const typename std::remove_reference< T >::type &def) const noexcept
 Gets a value stored by an option or a reasonable default. More...
 
template<typename Argument >
bool is_active (const Argument &arg) const noexcept
 Checks if an option's value has been parsed. More...
 
size_t size () const noexcept
 Returns the number of elements the option_set has.
 
bool empty () const noexcept
 Checks if the option_set is empty.
 
auto begin () noexcept-> decltype(options.begin())
 Returns an iterator pointing to the first option. More...
 
auto begin () const noexcept-> decltype(options.begin())
 
auto end () noexcept-> decltype(options.end())
 Returns an iterator pointing to one past the end. More...
 
auto end () const noexcept-> decltype(options.end())
 

Detailed Description

A container used for holding options. Order is preserved and duplicates are removed if inserted. Note that this container does not meet most of the requirements imposed by the C++ standard. Rather it is used exclusively for the optparse API. Compatibility with the C++ standard is kept mostly to iterators which allow use of the functions in <algorithm>.

Definition at line 50 of file option_set.hpp.

Constructor & Destructor Documentation

gears::optparse::option_set::option_set ( bool  help = true)
inline

Constructs an option_set with a help option automatically provided if wanted. The help option automatically provided is --help with a short option -h. The description is "shows this message and exits".

Parameters
helpDetermines if help option should be automatically added.

Definition at line 64 of file option_set.hpp.

gears::optparse::option_set::option_set ( std::initializer_list< option l)
inline

Constructs the option_set through std::initializer_list. All duplicates are removed. Only the first duplicate is kept. A duplicate is determined by having the same long name and short-name. A help option is automatically provided. If the help option is unwanted then a call to remove with "help" as a parameter will remove it.

Parameters
lThe std::initializer_list to construct with.

Definition at line 82 of file option_set.hpp.

Member Function Documentation

template<typename... Args>
void gears::optparse::option_set::add ( Args &&...  args)
inline

Adds an option to the option_set. Duplicates are removed. The option is constructed as if calling option(std::forward<Args>(args)...), that is to say that they're constructed in-place.

Parameters
argsArguments to pass to the #option constructor.

Definition at line 106 of file option_set.hpp.

auto gears::optparse::option_set::begin ( ) -> decltype(options.begin())
inlinenoexcept

Returns an iterator pointing to the first option. This member function is defined for iteration purposes and compatibility with <algorithm>. The iterators returned are RandomAccessIterators. Dereferencing this iterator is undefined behaviour if size == 0.

Returns
An iterator pointing to the first option defined.

Definition at line 230 of file option_set.hpp.

auto gears::optparse::option_set::end ( ) -> decltype(options.end())
inlinenoexcept

Returns an iterator pointing to one past the end. This member function is similar to begin with the same rules. It's undefined behaviour to dereference this iterator at all, regardless of the return of size.

Returns
An iterator pointing to one past the end.

Definition at line 249 of file option_set.hpp.

template<typename T , typename Argument >
const T& gears::optparse::option_set::get ( const Argument &  arg) const
inline

Gets a value stored by an option. The option is found as if calling option::is on the arg parameter. If the option is not found an exception is thrown. This function is the equivalent of calling option::get.

Exceptions
std::invalid_argumentThrown if the option is not found.
Parameters
argLong or short option to retrieve value of.
Returns
The internal value stored by an option.

Definition at line 154 of file option_set.hpp.

template<typename T , typename Argument >
const T& gears::optparse::option_set::get_or ( const Argument &  arg,
const typename std::remove_reference< T >::type &  def 
) const
inlinenoexcept

Gets a value stored by an option or a reasonable default. The option is found as if calling option::is on the arg parameter. If the option is not found, then the default is returned.

Parameters
argLong or short option to retrieve value of.
Returns
The internal value stored by an option or a reasonable default.

Definition at line 176 of file option_set.hpp.

template<typename Argument >
bool gears::optparse::option_set::is_active ( const Argument &  arg) const
inlinenoexcept

Checks if an option's value has been parsed. The option is found as if calling option::is on the arg parameter. This function is the equivalent of calling option::is_active.

Parameters
argLong or short option to retrieve value of.
Returns
true if the option's value has been parsed, false otherwise.

Definition at line 198 of file option_set.hpp.

template<typename Argument >
option_set& gears::optparse::option_set::remove ( const Argument &  arg)
inline

Removes an option from the option_set by the argument long or short name. For example, if you want to remove --help then remove("help") would remove it. If the option is not found then this function has no effect.

Parameters
argLong or short name to remove.
Returns
A reference to the option_set

Definition at line 130 of file option_set.hpp.