PIDUINO
Loading...
Searching...
No Matches
Piduino::OptionParser

OptionParser manages all Options. More...

#include <popl.h>

Collaboration diagram for Piduino::OptionParser:

Public Member Functions

 OptionParser (std::string description="")
 OptionParser implementation /////////////////////////////////.
 
virtual ~OptionParser ()=default
 Destructor.
 
template<typename T , Attribute attribute, typename... Ts>
std::shared_ptr< T > add (Ts &&... params)
 
template<typename T , typename... Ts>
std::shared_ptr< T > add (Ts &&... params)
 
void parse (int argc, const char *const argv[])
 
std::string help (const Attribute &max_attribute=Attribute::optional) const
 
std::string description () const
 
const std::vector< Option_ptr > & options () const
 
const std::vector< std::string > & non_option_args () const
 
const std::vector< std::string > & unknown_options () const
 
template<typename T >
std::shared_ptr< T > get_option (const std::string &long_name) const
 
template<typename T >
std::shared_ptr< T > get_option (char short_name) const
 

Protected Member Functions

Option_ptr find_option (const std::string &long_name) const
 
Option_ptr find_option (char short_name) const
 

Protected Attributes

std::vector< Option_ptroptions_
 
std::string description_
 
std::vector< std::string > non_option_args_
 
std::vector< std::string > unknown_options_
 

Detailed Description

OptionParser manages all Options.

OptionParser manages all Options Add Options (Option_Type = Value<T>, Implicit<T> or Switch) with "add<Option_Type>(option params)" Call "parse(argc, argv)" to trigger parsing of the options and to fill "non_option_args" and "unknown_options"

Definition at line 293 of file popl.h.

Constructor & Destructor Documentation

◆ OptionParser()

Piduino::OptionParser::OptionParser ( std::string  description = "")
inlineexplicit

OptionParser implementation /////////////////////////////////.

Construct the OptionParser

Parameters
descriptionused for the help message

Definition at line 782 of file popl.h.

◆ ~OptionParser()

virtual Piduino::OptionParser::~OptionParser ( )
virtualdefault

Destructor.

Member Function Documentation

◆ add() [1/2]

template<typename T , Attribute attribute, typename... Ts>
std::shared_ptr< T > Piduino::OptionParser::add ( Ts &&...  params)
inline

Add an Option e.g. 'add<Value<int>>("i", "int", "description for the -i option")'

Parameters
Tthe option type (Value, Switch, Implicit)
attributethe Option's attribute (inactive, hidden, required, optional, ...)
Tsthe Option's parameter

Definition at line 793 of file popl.h.

References options_.

◆ add() [2/2]

template<typename T , typename... Ts>
std::shared_ptr< T > Piduino::OptionParser::add ( Ts &&...  params)
inline

Add an Option e.g. 'add<Value<int>>("i", "int", "description for the -i option")'

Parameters
Tthe option type (Value, Switch, Implicit)
Tsthe Option's parameter

Definition at line 787 of file popl.h.

◆ description()

std::string Piduino::OptionParser::description ( ) const
inline

Get the OptionParser's description

Returns
the description as given during construction

Definition at line 814 of file popl.h.

References description_.

Referenced by Piduino::ConsoleOptionPrinter::print(), and Piduino::GroffOptionPrinter::print().

◆ find_option() [1/2]

Option_ptr Piduino::OptionParser::find_option ( char  short_name) const
inlineprotected

Definition at line 843 of file popl.h.

References options_, and Piduino::short_name.

◆ find_option() [2/2]

Option_ptr Piduino::OptionParser::find_option ( const std::string &  long_name) const
inlineprotected

Definition at line 834 of file popl.h.

References Piduino::long_name, and options_.

Referenced by get_option(), get_option(), and parse().

◆ get_option() [1/2]

template<typename T >
std::shared_ptr< T > Piduino::OptionParser::get_option ( char  short_name) const
inline

Get an Option by it's short name

Parameters
theOption's short name
Returns
a pointer of type "Value, Switch, Implicit" to the Option or nullptr

Definition at line 867 of file popl.h.

References find_option(), and Piduino::short_name.

Here is the call graph for this function:

◆ get_option() [2/2]

template<typename T >
std::shared_ptr< T > Piduino::OptionParser::get_option ( const std::string &  long_name) const
inline

Get an Option by it's long name

Parameters
theOption's long name
Returns
a pointer of type "Value, Switch, Implicit" to the Option or nullptr

Definition at line 853 of file popl.h.

References find_option(), and Piduino::long_name.

Here is the call graph for this function:

◆ help()

std::string Piduino::OptionParser::help ( const Attribute max_attribute = Attribute::optional) const
inline

Produce a help message

Parameters
max_attributeshow options up to this level (optional, advanced, expert)
Returns
the help message

Definition at line 983 of file popl.h.

References Piduino::ConsoleOptionPrinter::print().

Here is the call graph for this function:

◆ non_option_args()

const std::vector< std::string > & Piduino::OptionParser::non_option_args ( ) const
inline

Get command line arguments without option e.g. "-i 5 hello" => hello e.g. "-i 5 -- from here non option args" => "from", "here", "non", "option", "args"

Returns
vector to "stand-alone" command line arguments

Definition at line 824 of file popl.h.

References non_option_args_.

◆ options()

const std::vector< Option_ptr > & Piduino::OptionParser::options ( ) const
inline

Get all options that where added with "add"

Returns
a vector of the contained Options

Definition at line 819 of file popl.h.

References options_.

Referenced by Piduino::ConsoleOptionPrinter::print(), and Piduino::GroffOptionPrinter::print().

◆ parse()

void Piduino::OptionParser::parse ( int  argc,
const char *const  argv[] 
)
inline

Parse the command line into the added Options

Parameters
argccommand line argument count
argvcommand line arguments

from here on only non opt args

long option arg

short option arg

use the rest of the current argument as optarg

or the next arg

use the rest of the current argument as optarg

Definition at line 880 of file popl.h.

References find_option(), Piduino::inactive, Piduino::long_name, Piduino::invalid_option::missing_option, Piduino::no, non_option_args_, Piduino::optional, options_, Piduino::required, Piduino::short_name, and unknown_options_.

Here is the call graph for this function:

◆ unknown_options()

const std::vector< std::string > & Piduino::OptionParser::unknown_options ( ) const
inline

Get unknown command options e.g. '–some_unknown_option="hello"'

Returns
vector to "stand-alone" command line arguments

Definition at line 829 of file popl.h.

References unknown_options_.

Member Data Documentation

◆ description_

std::string Piduino::OptionParser::description_
protected

Definition at line 358 of file popl.h.

Referenced by description().

◆ non_option_args_

std::vector<std::string> Piduino::OptionParser::non_option_args_
protected

Definition at line 359 of file popl.h.

Referenced by non_option_args(), and parse().

◆ options_

std::vector<Option_ptr> Piduino::OptionParser::options_
protected

Definition at line 357 of file popl.h.

Referenced by add(), find_option(), find_option(), options(), and parse().

◆ unknown_options_

std::vector<std::string> Piduino::OptionParser::unknown_options_
protected

Definition at line 360 of file popl.h.

Referenced by parse(), and unknown_options().