EventAnalysis  1.3.0
Classes | Public Member Functions | Private Attributes | List of all members
EA::CommandLineParser Class Reference

Parser for command line options. More...

#include <CommandLineParser.h>

Classes

struct  FlagStruct
 
struct  MultiOptionStruct
 
struct  OptionStruct
 

Public Member Functions

void AddOption (const std::string &shortInd, const std::string &longInd, bool mandatory=false)
 Add and option with a single value. More...
 
void AddMultiOption (const std::string &shortInd, const std::string &longInd, bool mandatory=false)
 Add and option with multiple values. More...
 
void AddFlag (const std::string &shortInd, const std::string &longInd)
 Add a flag option. More...
 
bool ProcessCommandLine (int argc, const char *const *const argv)
 Process the command line. More...
 
int GetNOptions (const std::string &indicator)
 Returns the number of times the given option has been found in command line. More...
 
int GetNMultiOptions (const std::string &indicator)
 Returns the number of times the given multioption has been found in command line. More...
 
const std::string & GetOption (const std::string &indicator, int optionNo=0)
 Returns the value of the mandatory option. More...
 
const std::vector< std::string > & GetMultiOption (const std::string &indicator, int optionNo=0)
 Returns a vector of values of the mandatory multioption. More...
 
bool GetFlag (const std::string &indicator)
 Checks if a flag has been defined. More...
 
const std::vector< std::string > & GetArguments ()
 Getter method for argument list. More...
 

Private Attributes

std::vector< OptionStruct_options
 
std::vector< MultiOptionStruct_multiOptions
 
std::vector< FlagStruct_flags
 
std::vector< std::string > _arguments
 

Detailed Description

Parser for command line options.

This class handles command line to extract options, flags and arguments. An option is an indicator-value pair like "-o value" or "--option value"; the value can be a list of comma-separated values, e.g. "-o value1,value2,value3". A flag is an option without a value, e.g. an indicator like "-h" or "--help", and is treated like a boolean. An argument is anything on the command line which is not preceded by an indicator. As an example, the following command line:

$ program arg1 -p opt1 -h –file file1 –data data1,data2,data3 arg2 –verbose

defines two single-value option ("-p opt1" and "--file file1"), a multi-value option ("--data data1,data2,data3"), two flags ("-h" and "--verbose") and three arguments ("program", "arg1" and "arg2").

Member Function Documentation

◆ AddFlag()

void EA::CommandLineParser::AddFlag ( const std::string &  shortInd,
const std::string &  longInd 
)

Add a flag option.

Defines an option which is a flag, i.e. that has no associated value. As an example, a "--help" flag.

Parameters
shortIndSingle-character indicator (including the trailing -).
longIndMulti-character indicator (including the trailing –).

◆ AddMultiOption()

void EA::CommandLineParser::AddMultiOption ( const std::string &  shortInd,
const std::string &  longInd,
bool  mandatory = false 
)

Add and option with multiple values.

Defines an option taking a vector of values, e.g. "-f file1,file2,file3".

Parameters
shortIndSingle-character indicator (including the trailing -).
longIndMulti-character indicator (including the trailing –).
mandatoryIf true, ProcessCommandLine will return an error if the option is not found in the command line.

◆ AddOption()

void EA::CommandLineParser::AddOption ( const std::string &  shortInd,
const std::string &  longInd,
bool  mandatory = false 
)

Add and option with a single value.

Defines an option taking a single value, e.g. "-n 5" or "--number 5"

Parameters
shortIndSingle-character indicator (including the trailing -).
longIndMulti-character indicator (including the trailing –)..
mandatoryIf true, ProcessCommandLine will return an error if the option is not found in the command line.

◆ GetArguments()

const std::vector< std::string > & EA::CommandLineParser::GetArguments ( )

Getter method for argument list.

All the arguments are grouped and returned by this method. Arguments are ordered as in the command line, i.e. the first one will be the name of the executable.

Returns
A vector of arguments expressed as strings.

◆ GetFlag()

bool EA::CommandLineParser::GetFlag ( const std::string &  indicator)

Checks if a flag has been defined.

If a command line flag is defined as "--help" then calling this method with indicator argument set to "--help" will return true.

Parameters
indicatorthe indicator of the desired flag option (either in the short or long format).
optionNoThe desired occurrence of the given option (0,...,GetNMultiOptions() - 1).
Returns
True if the flag has been defined.

◆ GetMultiOption()

const std::vector< std::string > & EA::CommandLineParser::GetMultiOption ( const std::string &  indicator,
int  optionNo = 0 
)

Returns a vector of values of the mandatory multioption.

If a command line multioption is defined as "-f file1,file2,file3" then calling this method with indicator argument set to "-f" will return a vector with three string elements "file1", "file2" and "file3". If the multioption appears more than once in the command line, the optionNo argument can be used to select the desired occurrence. An empty string is returned if #optionNo is out-of-bound.

Parameters
indicatorthe indicator of the desired option (either in the short or long format).
iOptThe desired occurrence of the given option (0,...,GetNOptions() - 1).
Returns
A vector with the values of the desired option.

◆ GetNMultiOptions()

int EA::CommandLineParser::GetNMultiOptions ( const std::string &  indicator)

Returns the number of times the given multioption has been found in command line.

Parameters
indicatorThe indicator of the desired multioption (either in the short or long format)
Returns
The number of times the given multioption is present in command line.

◆ GetNOptions()

int EA::CommandLineParser::GetNOptions ( const std::string &  indicator)

Returns the number of times the given option has been found in command line.

Parameters
indicatorThe indicator of the desired option (either in the short or long format)
Returns
The number of times the given option is present in command line.

◆ GetOption()

const std::string & EA::CommandLineParser::GetOption ( const std::string &  indicator,
int  optionNo = 0 
)

Returns the value of the mandatory option.

If a command line option is defined as "-f file" then calling this method with indicator argument set to "-f" will return "file". If the option appears more than once in the command line, the optionNo argument can be used to select the desired occurrence. An empty string is returned if #optionNo is out-of-bound.

Parameters
indicatorthe indicator of the desired option (either in the short or long format).
optionNoThe desired occurrence of the given option.
Returns
The desired option.

◆ ProcessCommandLine()

bool EA::CommandLineParser::ProcessCommandLine ( int  argc,
const char *const *const  argv 
)

Process the command line.

This method scans the command line for options, flags and arguments. The argc and argv options of this method should be set to be equal to the main() options.

Parameters
argcNumber of command line arguments.
argvVector of command line arguments.
Returns
true if all the mandatory options and flags have been found

Member Data Documentation

◆ _arguments

std::vector<std::string> EA::CommandLineParser::_arguments
private

◆ _flags

std::vector<FlagStruct> EA::CommandLineParser::_flags
private

◆ _multiOptions

std::vector<MultiOptionStruct> EA::CommandLineParser::_multiOptions
private

◆ _options

std::vector<OptionStruct> EA::CommandLineParser::_options
private

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