![]() |
cppassist
1.0.0.f4fab4f962ff
C++ sanctuary for small but powerful and frequently required, stand alone features.
|
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 > | |
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... | |
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.
cppassist::ArgumentParser::ArgumentParser | ( | ) |
Constructor.
cppassist::ArgumentParser::~ArgumentParser | ( | ) |
Destructor.
void cppassist::ArgumentParser::parse | ( | int | argc, |
char * | argv[] | ||
) |
Parse command line.
[in] | argc | Number of arguments |
[in] | argv | List of arguments |
const std::map<std::string, std::string>& cppassist::ArgumentParser::options | ( | ) | const |
Get options.
true
if set. bool cppassist::ArgumentParser::isSet | ( | const std::string & | option | ) | const |
Check if option is set.
[in] | option | Name of option (e.g., "-v") |
true
if option is set, else false
std::string cppassist::ArgumentParser::value | ( | const std::string & | option, |
const std::string & | defaultValue = "" |
||
) | const |
Get option value.
[in] | option | Name of option (e.g., "--v") |
[in] | defaultValue | Default value returned if option is not set |
const std::string& cppassist::ArgumentParser::value | ( | const std::string & | option, |
std::string & | defaultValue | ||
) | const |
Get option value.
[in] | option | Name of option (e.g., "--v") |
[in] | defaultValue | Default value returned if option is not set |
T cppassist::ArgumentParser::value | ( | const std::string & | option, |
const T & | defaultValue = T() |
||
) | const |
Get option value converted to specific data type.
[in] | option | Name of option (e.g., "--v") |
[in] | defaultValue | Default value returned if option is not set |
const std::vector<std::string>& cppassist::ArgumentParser::params | ( | ) | const |
Get additional parameters.
void cppassist::ArgumentParser::print | ( | ) | const |
Print command line parameters to console.