soma-io  5.0.5
carto::OptionsParser Class Reference

Commandline options parser. More...

#include <soma-io/getopt/getopt.h>

Inheritance diagram for carto::OptionsParser:
Collaboration diagram for carto::OptionsParser:

Public Types

typedef std::vector< std::string > CommandLineArguments
 

Public Member Functions

 OptionsParser (int argc, const char **argv)
 
 ~OptionsParser ()
 
template<typename T >
void addOption (T &param, const std::string &token, const std::string &description, bool optional=false)
 Adds a single parameter to be parsed on the commandline. More...
 
template<typename T >
void addOptionSeries (T &param, const std::string &token, const std::string &description, unsigned minnum=0, unsigned maxnum=0)
 Adds a series of parameters to be parsed on the commandline. More...
 
void alias (const std::string &to, const std::string &from)
 Gives an alternative name (token) to to the parameter of token from. More...
 
void parse ()
 Parses the commandline arguments (argc, argv) and fills in the registered parameters variables. More...
 
void check ()
 Check all needed parameters are filled in, and series numbers are OK. More...
 

Protected Types

typedef std::vector< OptionBase *> OptionList
 

Protected Member Functions

void pushOption (OptionBase *)
 

Protected Attributes

CommandLineArguments cla
 
OptionList options
 
int insertIndex
 

Detailed Description

Commandline options parser.

Usage: instantiate a parser, add parameters using addOption(), then call parse() and check().

You generally do not use OptionsParser directly, but rather a subclass of it: CartoApplication, or AimsApplication in aims commands.

example:

std::string input, output;
int param = 10;
OptionsParser app( argc, argv );
app.addOption( input, "-i", "input file" );
app.addOption( output, "-o", "output file" );
app.addOption( param, "-p", "an int parameter (default value: 10)", true );
try
{
app.parse();
app.check();
}
catch( std::exception & e )
{
std::cerr << e.what();
throw;
}

Definition at line 470 of file getopt.h.

Member Typedef Documentation

◆ CommandLineArguments

typedef std::vector< std::string > carto::OptionsParser::CommandLineArguments

Definition at line 473 of file getopt.h.

◆ OptionList

typedef std::vector< OptionBase * > carto::OptionsParser::OptionList
protected

Definition at line 478 of file getopt.h.

Constructor & Destructor Documentation

◆ OptionsParser()

carto::OptionsParser::OptionsParser ( int  argc,
const char **  argv 
)

◆ ~OptionsParser()

carto::OptionsParser::~OptionsParser ( )

Member Function Documentation

◆ addOption()

template<typename T >
void OptionsParser::addOption ( T &  param,
const std::string &  token,
const std::string &  description,
bool  optional = false 
)
inline

Adds a single parameter to be parsed on the commandline.

A single parameter can only appear once in a commandline: for instance if the "-i" parameter is a single option, you cannot call the commanline with the arguments "-i file1 -i file2" (this will raise an exception).

Parameters
paramthe parameter variable (it will be filled automatically when parse() is called)
tokenoption switch used to identify this parameter on the commandline ("-i" or "-o" for instance). Any string is accepted so they are not necessarily one-character switches, and they even do not need to start by a '-', althrough it is strongly recommended for clarity.
descriptionthe help string for this option (displayed when invoking the commandline with the "--help" option)
optionalif true, the parameter is not mandatory, and if it is not provided on the commandline, the corresponding variable will be left untouched. So optional parameters variables must be initialized with a default value.

Definition at line 550 of file getopt.h.

◆ addOptionSeries()

template<typename T >
void OptionsParser::addOptionSeries ( T &  param,
const std::string &  token,
const std::string &  description,
unsigned  minnum = 0,
unsigned  maxnum = 0 
)
inline

Adds a series of parameters to be parsed on the commandline.

A series of parameters is a STL sequence of parameters of the same type, all given by the same commandline option switch: for instance if "-i" is a series of parameters of type string, the commandline will accept the arguments "-i file1 -i file2"

Parameters
paramthe parameter variable (it will be filled automatically when parse() is called). It must be a STL-compatible sequence (like a vector or a list) with iterators (begin(), end(), rbegin() methods), a value_type type, and a push_back() insertion method.
tokenoption switch used to identify this parameter on the commandline
descriptionthe help string for this option (displayed when invoking the commandline with the "--help" option)
minnumminimum admissible number of occurrences of this parameter: 0 means it it optional
maxnummaximum admissible number of occurrences of this parameter, the special value 0 means unlimited

Definition at line 558 of file getopt.h.

◆ alias()

void carto::OptionsParser::alias ( const std::string &  to,
const std::string &  from 
)

Gives an alternative name (token) to to the parameter of token from.

◆ check()

void carto::OptionsParser::check ( )

Check all needed parameters are filled in, and series numbers are OK.

This method must be called after parse().

◆ parse()

void carto::OptionsParser::parse ( )

Parses the commandline arguments (argc, argv) and fills in the registered parameters variables.

This must be done after all addOption() / addOptionSeries() calls.

◆ pushOption()

void carto::OptionsParser::pushOption ( OptionBase )
protected

Member Data Documentation

◆ cla

CommandLineArguments carto::OptionsParser::cla
protected

Definition at line 476 of file getopt.h.

◆ insertIndex

int carto::OptionsParser::insertIndex
protected

Definition at line 481 of file getopt.h.

◆ options

OptionList carto::OptionsParser::options
protected

Definition at line 479 of file getopt.h.


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