EventAnalysis
1.0.0
|
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 |
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").
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.
shortInd | Single-character indicator (including the trailing -). |
longInd | Multi-character indicator (including the trailing –). |
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".
shortInd | Single-character indicator (including the trailing -). |
longInd | Multi-character indicator (including the trailing –). |
mandatory | If true, ProcessCommandLine will return an error if the option is not found in the command line. |
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"
shortInd | Single-character indicator (including the trailing -). |
longInd | Multi-character indicator (including the trailing –).. |
mandatory | If true, ProcessCommandLine will return an error if the option is not found in the command line. |
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.
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.
indicator | the indicator of the desired flag option (either in the short or long format). |
optionNo | The desired occurrence of the given option (0,...,GetNMultiOptions() - 1). |
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.
indicator | the indicator of the desired option (either in the short or long format). |
iOpt | The desired occurrence of the given option (0,...,GetNOptions() - 1). |
int EA::CommandLineParser::GetNMultiOptions | ( | const std::string & | indicator | ) |
Returns the number of times the given multioption has been found in command line.
indicator | The indicator of the desired multioption (either in the short or long format) |
int EA::CommandLineParser::GetNOptions | ( | const std::string & | indicator | ) |
Returns the number of times the given option has been found in command line.
indicator | The indicator of the desired option (either in the short or long format) |
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.
indicator | the indicator of the desired option (either in the short or long format). |
optionNo | The desired occurrence of the given option. |
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.
argc | Number of command line arguments. |
argv | Vector of command line arguments. |
|
private |
|
private |
|
private |
|
private |