Option++  2.0
C++ library for reading command-line options
Public Types | Public Member Functions | List of all members
optionpp::option_group Class Reference

Holds a group of program options. More...

#include <option_group.hpp>

Public Types

using value_type = option
 Type used to hold option information.
 
using container_type = std::vector< option >
 Type of container used to hold the options.
 
using size_type = container_type::size_type
 Type used to represent the size of the container.
 
using iterator = container_type::iterator
 Plain iterator type.
 
using const_iterator = container_type::const_iterator
 Constant iterator type.
 
using reverse_iterator = container_type::reverse_iterator
 Reverse iterator type.
 
using const_reverse_iterator = container_type::const_reverse_iterator
 Constant reverse iterator type.
 

Public Member Functions

 option_group () noexcept
 Default constructor.
 
 option_group (const std::string &name)
 Construct empty named group. More...
 
template<typename InputIt >
 option_group (const std::string &name, InputIt first, InputIt last)
 Construct from a sequence. More...
 
const std::string & name () const noexcept
 Returns the name of the group. More...
 
optionadd_option (const option &opt=option{})
 Add a program option to the group. More...
 
optionadd_option (const std::string &long_name, char short_name='\0', const std::string &description="", const std::string &arg_name="", bool arg_required=false)
 Construct and add a program option to the group. More...
 
size_type size () const noexcept
 Return the number of options in the group. More...
 
bool empty () const noexcept
 Return whether the container is empty. More...
 
iterator begin () noexcept
 Return an iterator to the first option in the group. More...
 
const_iterator begin () const noexcept
 Return a const_iterator to the first option. More...
 
iterator end () noexcept
 Return an iterator to one beyond the last option in the group. More...
 
const_iterator end () const noexcept
 Return a const_iterator to the end of the container. More...
 
const_iterator cbegin () const noexcept
 Return a const_iterator to the first option. More...
 
const_iterator cend () const noexcept
 Return a const_iterator to the end of the container. More...
 
reverse_iterator rbegin () noexcept
 Return a reverse_iterator to the beginning. More...
 
const_reverse_iterator rbegin () const noexcept
 Return a const_reverse_iterator to the beginning. More...
 
reverse_iterator rend () noexcept
 Return a reverse_iterator to the end. More...
 
const_reverse_iterator rend () const noexcept
 Return a const_reverse_iterator to the end. More...
 
const_reverse_iterator crbegin () const noexcept
 Return a const_reverse_iterator to the beginning. More...
 
const_reverse_iterator crend () const noexcept
 Return a const_reverse_iterator to the end. More...
 
iterator find (const std::string &long_name)
 Search for an option in the group. More...
 
const_iterator find (const std::string &long_name) const
 Search for an option in the group. More...
 
iterator find (char short_name)
 Search for an option in the group. More...
 
const_iterator find (char short_name) const
 Search for an option in the group. More...
 
void sort ()
 Sorts the options by name. More...
 
optionoperator[] (const std::string long_name)
 Subscript operator. More...
 
optionoperator[] (char short_name)
 Subscript operator. More...
 

Detailed Description

Holds a group of program options.

Groups can be used to keep options organized when displaying the application's help message.

Constructor & Destructor Documentation

◆ option_group() [1/2]

optionpp::option_group::option_group ( const std::string &  name)
inline

Construct empty named group.

Parameters
nameName of the group.

◆ option_group() [2/2]

template<typename InputIt >
optionpp::option_group::option_group ( const std::string &  name,
InputIt  first,
InputIt  last 
)
inline

Construct from a sequence.

Each option in the sequence specifies a program option that can be given on the command line.

Template Parameters
InputItThe iterator type (usually deduced).
Parameters
nameGroup name.
firstThe iterator pointing to the start of the sequence.
lastThe iterator pointing to one past the end of the sequence.

Member Function Documentation

◆ add_option() [1/2]

option& optionpp::option_group::add_option ( const option opt = option{})
inline

Add a program option to the group.

Note that the method returns a reference to the instance of the option that was inserted to allow chaining. For example:

group.add_option()
.long_name("verbose")
.short_name('v')
.description("Show verbose output.");
Parameters
optThe option to add.
Returns
Reference to the inserted option, for chaining.

◆ add_option() [2/2]

option & optionpp::option_group::add_option ( const std::string &  long_name,
char  short_name = '\0',
const std::string &  description = "",
const std::string &  arg_name = "",
bool  arg_required = false 
)

Construct and add a program option to the group.

Parameters
long_nameLong name for the option.
short_nameShort name for the option.
descriptionDescription of the option.
arg_nameName of the option's argument.
arg_requiredShould be true if the option's argument is mandatory.
Returns
Reference to the inserted option, for chaining.

◆ begin() [1/2]

const_iterator optionpp::option_group::begin ( ) const
inlinenoexcept

Return a const_iterator to the first option.

Returns
A const_iterator pointing to the first option.

◆ begin() [2/2]

iterator optionpp::option_group::begin ( )
inlinenoexcept

Return an iterator to the first option in the group.

Returns
An iterator pointing to the first option.

◆ cbegin()

const_iterator optionpp::option_group::cbegin ( ) const
inlinenoexcept

Return a const_iterator to the first option.

Returns
A const_iterator pointing to the first option.

◆ cend()

const_iterator optionpp::option_group::cend ( ) const
inlinenoexcept

Return a const_iterator to the end of the container.

Returns
A const_iterator pointing to one past the last option.

◆ crbegin()

const_reverse_iterator optionpp::option_group::crbegin ( ) const
inlinenoexcept

Return a const_reverse_iterator to the beginning.

Returns
A const_reverse_iterator pointing to the first option in the reversed sequence.

◆ crend()

const_reverse_iterator optionpp::option_group::crend ( ) const
inlinenoexcept

Return a const_reverse_iterator to the end.

Returns
A const_reverse_iterator pointing to one past the last option in the reversed sequence.

◆ empty()

bool optionpp::option_group::empty ( ) const
inlinenoexcept

Return whether the container is empty.

Returns
True if the option container is empty, false otherwise.

◆ end() [1/2]

const_iterator optionpp::option_group::end ( ) const
inlinenoexcept

Return a const_iterator to the end of the container.

Returns
A const_iterator pointing to one past the last option.

◆ end() [2/2]

iterator optionpp::option_group::end ( )
inlinenoexcept

Return an iterator to one beyond the last option in the group.

Returns
An iterator pointing to one past the last option.

◆ find() [1/4]

auto optionpp::option_group::find ( char  short_name)

Search for an option in the group.

Looks up an option by its short name and returns an iterator pointing to the option instance.

Parameters
short_nameShort name of the option.
Returns
Iterator pointing to the option or end if not found.

◆ find() [2/4]

auto optionpp::option_group::find ( char  short_name) const

Search for an option in the group.

Looks up an option by its short name and returns an iterator pointing to the option instance.

Parameters
short_nameShort name of the option.
Returns
Iterator pointing to the option or end if not found.

◆ find() [3/4]

auto optionpp::option_group::find ( const std::string &  long_name)

Search for an option in the group.

Looks up an option by its long name and returns an iterator pointing to the option instance.

Parameters
long_nameLong name of the option.
Returns
Iterator pointing to the option or end if not found.

◆ find() [4/4]

auto optionpp::option_group::find ( const std::string &  long_name) const

Search for an option in the group.

Looks up an option by its long name and returns an iterator pointing to the option instance.

Parameters
long_nameLong name of the option.
Returns
Iterator pointing to the option or end if not found.

◆ name()

const std::string& optionpp::option_group::name ( ) const
inlinenoexcept

Returns the name of the group.

Returns
Group name.

◆ operator[]() [1/2]

option & optionpp::option_group::operator[] ( char  short_name)

Subscript operator.

Returns the specified option or creates it if it doesn't exist.

Parameters
short_nameShort name for the option.
Returns
The matching option, or a newly created one if it didn't already exist.

◆ operator[]() [2/2]

option & optionpp::option_group::operator[] ( const std::string  long_name)

Subscript operator.

Returns the specified option or creates it if it doesn't exist.

Parameters
long_nameLong name for the option.
Returns
The matching option, or a newly created one if it didn't already exist.

◆ rbegin() [1/2]

const_reverse_iterator optionpp::option_group::rbegin ( ) const
inlinenoexcept

Return a const_reverse_iterator to the beginning.

Returns
A const_reverse_iterator pointing to the first option in the reversed sequence.

◆ rbegin() [2/2]

reverse_iterator optionpp::option_group::rbegin ( )
inlinenoexcept

Return a reverse_iterator to the beginning.

Returns
A reverse_iterator pointing to the first option in the reversed sequence.

◆ rend() [1/2]

const_reverse_iterator optionpp::option_group::rend ( ) const
inlinenoexcept

Return a const_reverse_iterator to the end.

Returns
A const_reverse_iterator pointing to one past the last option in the reversed sequence.

◆ rend() [2/2]

reverse_iterator optionpp::option_group::rend ( )
inlinenoexcept

Return a reverse_iterator to the end.

Returns
A reverse_iterator pointing to one past the last option in the reversed sequence.

◆ size()

size_type optionpp::option_group::size ( ) const
inlinenoexcept

Return the number of options in the group.

Returns
Size of option container.

◆ sort()

void optionpp::option_group::sort ( )

Sorts the options by name.

By default, options are kept in the same order as they were added. Calling this method will sort them by name (that is, for each option, if both long and short names exist, then the long name is used for comparison).


The documentation for this class was generated from the following files: