cppassist  1.0.0.f4fab4f962ff
C++ sanctuary for small but powerful and frequently required, stand alone features.
Public Member Functions | List of all members
cppassist::ArgumentParser Class Reference

Command line arguments parser. More...

#include <cppassist/include/cppassist/cmdline/ArgumentParser.h>

Public Member Functions

 ArgumentParser ()
 Constructor. More...
 
 ~ArgumentParser ()
 Destructor. More...
 
void parse (int argc, char *argv[])
 Parse command line. More...
 
const std::map< std::string, std::string > & options () const
 Get options. More...
 
bool isSet (const std::string &option) const
 Check if option is set. More...
 
std::string value (const std::string &option, const std::string &defaultValue="") const
 Get option value. More...
 
const std::string & value (const std::string &option, std::string &defaultValue) const
 Get option value. More...
 
template<typename T >
value (const std::string &option, const T &defaultValue=T()) const
 Get option value converted to specific data type. More...
 
const std::vector< std::string > & params () const
 Get additional parameters. More...
 
void print () const
 Print command line parameters to console. More...
 

Detailed Description

Command line arguments parser.

Can be used to parse the arguments from the command line. The following command line syntax is assumed:

<executable> [--<option> <value>]* [-<option>]* [<param>]*

<executable>: The first argument is assumed to be the current executable name.

--<option>: Options with two slashes (--) must be followed by a value.

-<option>: Options with a single slash (-) do not have a value. If present, their value is set to true.

<param>: All other arguments found in the command line are assumed to be additional parameters and are added to the parameter list.

Constructor & Destructor Documentation

◆ ArgumentParser()

cppassist::ArgumentParser::ArgumentParser ( )

Constructor.

◆ ~ArgumentParser()

cppassist::ArgumentParser::~ArgumentParser ( )

Destructor.

Member Function Documentation

◆ parse()

void cppassist::ArgumentParser::parse ( int  argc,
char *  argv[] 
)

Parse command line.

Parameters
[in]argcNumber of arguments
[in]argvList of arguments

◆ options()

const std::map<std::string, std::string>& cppassist::ArgumentParser::options ( ) const

Get options.

Returns
Options (key->value)
Remarks
Options without values (single "-") have the value true if set.

◆ isSet()

bool cppassist::ArgumentParser::isSet ( const std::string &  option) const

Check if option is set.

Parameters
[in]optionName of option (e.g., "-v")
Returns
true if option is set, else false

◆ value() [1/3]

std::string cppassist::ArgumentParser::value ( const std::string &  option,
const std::string &  defaultValue = "" 
) const

Get option value.

Parameters
[in]optionName of option (e.g., "--v")
[in]defaultValueDefault value returned if option is not set
Returns
Value of the option or defaultValue if the option is not set

◆ value() [2/3]

const std::string& cppassist::ArgumentParser::value ( const std::string &  option,
std::string &  defaultValue 
) const

Get option value.

Parameters
[in]optionName of option (e.g., "--v")
[in]defaultValueDefault value returned if option is not set
Returns
Value of the option or defaultValue if the option is not set

◆ value() [3/3]

template<typename T >
T cppassist::ArgumentParser::value ( const std::string &  option,
const T &  defaultValue = T() 
) const

Get option value converted to specific data type.

Parameters
[in]optionName of option (e.g., "--v")
[in]defaultValueDefault value returned if option is not set
Returns
Value of the option
Remarks
Uses string::fromString() to convert to specified type

◆ params()

const std::vector<std::string>& cppassist::ArgumentParser::params ( ) const

Get additional parameters.

Returns
Additional parameters

◆ print()

void cppassist::ArgumentParser::print ( ) const

Print command line parameters to console.


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