PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
OptionParser Class Reference

Parse the options passed to a program. More...

#include <OptionParser.h>

+ Collaboration diagram for OptionParser:

Public Member Functions

void addMode (const std::string &modeName)
 Add a mode in the option. More...
 
template<typename T >
void addOption (const std::string &longOption, const std::string &shortOption, const std::list< T > &defaultValue, const std::string &docString="")
 Add an option in the OptionParser. More...
 
template<typename T >
void addOption (const std::string &longOption, const std::string &shortOption, const std::list< T > &defaultValue, OptionType::OptionType optionType, const std::string &docString="")
 Add an option in the OptionParser. More...
 
template<typename T >
void addOption (const std::string &longOption, const std::string &shortOption, const std::vector< T > &defaultValue, const std::string &docString="")
 Add an option in the OptionParser. More...
 
template<typename T >
void addOption (const std::string &longOption, const std::string &shortOption, const std::vector< T > &defaultValue, OptionType::OptionType optionType, const std::string &docString="")
 Add an option in the OptionParser. More...
 
template<typename T >
void addOption (const std::string &longOption, const std::string &shortOption, const T defaultValue, const std::string &docString="")
 Add an option in the OptionParser. More...
 
template<typename T >
void addOption (const std::string &longOption, const std::string &shortOption, const T defaultValue, OptionType::OptionType optionType, const std::string &docString="")
 Add an option in the OptionParser. More...
 
void addOption (const std::string &longOption, const std::string &shortOption, OptionType::OptionType optionType, bool isRequired, bool isAllowEmpty, const std::string &docString)
 Add an option in the OptionParser. More...
 
void addOption (const std::string &longOption, const std::string &shortOption, OptionType::OptionType optionType, bool isRequired, const std::string &docString)
 Add an option in the OptionParser. More...
 
void closeMode ()
 Close the current mode and go back to be default one. More...
 
OptionModegetDefaultMode ()
 
const OptionModegetDefaultMode () const
 Get default mode. More...
 
OptionModegetMode (const std::string &name)
 Get mode by name. More...
 
const OptionModegetMode (const std::string &name) const
 Get mode by name. More...
 
bool isModeExist (const std::string &name) const
 Check if the given mode name does exist. More...
 
OptionParseroperator= (const OptionParser &other)
 Definition of equal operator of OptionParser. More...
 
 OptionParser (bool enableHelpOption=true, const std::string &programVersion="")
 Default constructeur of OptionParser. More...
 
 OptionParser (const OptionParser &other)
 Copy constructor of OptionParser. More...
 
void parseArgument (int argc, char **argv)
 Parse the arguments passed to the program. More...
 
void print () const
 Print all the options. More...
 
void setExampleLongOption (const std::string &example)
 Set the example usage of the program. More...
 
void setExampleShortOption (const std::string &example)
 Set the example usage of the program. More...
 
virtual ~OptionParser ()
 Destructeur of OptionParser. More...
 

Protected Member Functions

void copyOptionParser (const OptionParser &other)
 Copy function of OptionParser. More...
 

Private Member Functions

bool checkArgument () const
 Check the argument of the parser. More...
 
template<typename T >
void checkOptionType (OptionType::OptionType optionType)
 Check the given option type. More...
 
bool completeOptionValue (std::string &possibleValue, const std::string &cursorOption, const std::string &prevCursorOption) const
 Complete the possible value of an option (FILENAME, DIRECTORY, FILE_OR_DIR) More...
 
const OptionModegetCurrentlyParsedMode () const
 Get the currently parsed OptionMode. More...
 
const OptiongetLongOptionValue (std::string &valueToBeCompleted, const std::string &cursorOption) const
 Get the long option value to be completed. More...
 
OptionModegetParserMode (ArgParser &parser)
 Get a mode if it exist. More...
 
void getPossibleOption (std::string &possibleOption, const std::string &cursorOption) const
 Get the possible options which can be used. More...
 
void getPossibleOtherOption (std::string &possibleOption, const std::string &cursorOption) const
 Get the possible other options which can be used. More...
 
const OptiongetSplitOptionValue (std::string &valueToBeCompleted, const std::string &cursorOption, const std::string &prevCursorOption) const
 Get the split option (without =) value to be completed. More...
 
void initialisationOptionParser ()
 Initialisation function of the class OptionParser. More...
 
void parseArgumentBashCompletion (ArgParser &parser)
 Bash completion argument parsing mode. More...
 
void parseArgumentNormalUse (ArgParser &parser)
 Classical argument parsing mode. More...
 

Private Attributes

size_t p_currentMode
 Index of the current mode in the OptionParser. More...
 
OptionModep_currentParserMode
 Current mode parsed. More...
 
bool p_enableHelpOption
 True to enable automatically the printing of the help option when the program is called with –help or -h. More...
 
std::string p_exempleLongOption
 Usage example with long options. More...
 
std::string p_exempleShortOption
 Usage example with short options. More...
 
std::string p_programVersion
 Program version to be printed on –version or -v option. More...
 
VecMode p_vecMode
 Vector of all the defined mode in the OptionParser. More...
 

Detailed Description

Parse the options passed to a program.

When you define an option, you do not have to pass the '-' of the option name exampe : –longoption or -shortoption you pass only longoption and shortoption

Definition at line 15 of file OptionParser.h.

Constructor & Destructor Documentation

◆ OptionParser() [1/2]

OptionParser::OptionParser ( bool  enableHelpOption = true,
const std::string &  programVersion = "" 
)

Default constructeur of OptionParser.

Parameters
enableHelpOption: True to enable automatically the printing of the help option when the program is called with –help or -h
programVersion: version of the program to be printed on –version or -v options

Definition at line 16 of file OptionParser.cpp.

17  :p_enableHelpOption(enableHelpOption), p_programVersion(programVersion)
18 {
20 }

References initialisationOptionParser().

+ Here is the call graph for this function:

◆ OptionParser() [2/2]

OptionParser::OptionParser ( const OptionParser other)

Copy constructor of OptionParser.

Parameters
other: class to copy

Definition at line 25 of file OptionParser.cpp.

25  {
26  copyOptionParser(other);
27 }

References copyOptionParser().

+ Here is the call graph for this function:

◆ ~OptionParser()

OptionParser::~OptionParser ( )
virtual

Destructeur of OptionParser.

Definition at line 30 of file OptionParser.cpp.

30  {
31 
32 }

Member Function Documentation

◆ addMode()

void OptionParser::addMode ( const std::string &  modeName)

Add a mode in the option.

Parameters
modeName: name of the new mode to be added

Definition at line 56 of file OptionParser.cpp.

56  {
57  OptionMode mode(modeName);
58  mode.setEnableHelpOption(p_enableHelpOption);
59  mode.setProgramVersion(p_programVersion);
60  p_vecMode.push_back(mode);
61  p_currentMode = p_vecMode.size() - 1lu;
62 }

References p_currentMode, p_enableHelpOption, p_programVersion, p_vecMode, OptionMode::setEnableHelpOption(), and OptionMode::setProgramVersion().

+ Here is the call graph for this function:

◆ addOption() [1/8]

template<typename T >
void OptionParser::addOption ( const std::string &  longOption,
const std::string &  shortOption,
const std::list< T > &  defaultValue,
const std::string &  docString = "" 
)

Add an option in the OptionParser.

Parameters
longOption: long option (start with –) as –version
shortOption: short option (start with -) as -v
defaultValue: default value of the option
docString: documentation string of the option The type of the option will be automatically determined with the type of the default value

Definition at line 75 of file OptionParser_impl.h.

75  {
76  OptionType::OptionType optionType = getOptionTypeFromType<T>();
77  OptionValue valueList;
78  valueList.setType(optionType);
79  valueList.setDefaultValue(defaultValue);
80  Option option(longOption, shortOption, valueList, false, docString);
81  p_vecMode.back().addOption(option);
82 }

References p_vecMode, OptionValue::setDefaultValue(), and OptionValue::setType().

+ Here is the call graph for this function:

◆ addOption() [2/8]

template<typename T >
void OptionParser::addOption ( const std::string &  longOption,
const std::string &  shortOption,
const std::list< T > &  defaultValue,
OptionType::OptionType  optionType,
const std::string &  docString = "" 
)

Add an option in the OptionParser.

Parameters
longOption: long option (start with –) as –version
shortOption: short option (start with -) as -v
defaultValue: default value of the option
optionType: type of the data of the option (int, float, double, string, etc)
docString: documentation string of the option The function will check if the given optionType is relevant with the type of the default value

Definition at line 113 of file OptionParser_impl.h.

115 {
116  checkOptionType<T>(optionType);
117  OptionValue valueList;
118  valueList.setDefaultValue(defaultValue);
119  valueList.setType(optionType);
120  Option option(longOption, shortOption, valueList, false, docString);
121  p_vecMode.back().addOption(option);
122 }

References p_vecMode, OptionValue::setDefaultValue(), and OptionValue::setType().

+ Here is the call graph for this function:

◆ addOption() [3/8]

template<typename T >
void OptionParser::addOption ( const std::string &  longOption,
const std::string &  shortOption,
const std::vector< T > &  defaultValue,
const std::string &  docString = "" 
)

Add an option in the OptionParser.

Parameters
longOption: long option (start with –) as –version
shortOption: short option (start with -) as -v
defaultValue: default value of the option
docString: documentation string of the option The type of the option will be automatically determined with the type of the default value

Definition at line 58 of file OptionParser_impl.h.

58  {
59  OptionType::OptionType optionType = getOptionTypeFromType<T>();
60  OptionValue valueVec;
61  valueVec.setType(optionType);
62  valueVec.setDefaultValue(defaultValue);
63  Option option(longOption, shortOption, valueVec, false, docString);
64  p_vecMode.back().addOption(option);
65 }

References p_vecMode, OptionValue::setDefaultValue(), and OptionValue::setType().

+ Here is the call graph for this function:

◆ addOption() [4/8]

template<typename T >
void OptionParser::addOption ( const std::string &  longOption,
const std::string &  shortOption,
const std::vector< T > &  defaultValue,
OptionType::OptionType  optionType,
const std::string &  docString = "" 
)

Add an option in the OptionParser.

Parameters
longOption: long option (start with –) as –version
shortOption: short option (start with -) as -v
defaultValue: default value of the option
optionType: type of the data of the option (int, float, double, string, etc)
docString: documentation string of the option The function will check if the given optionType is relevant with the type of the default value

Definition at line 93 of file OptionParser_impl.h.

95 {
96  checkOptionType<T>(optionType);
97  OptionValue valueVec;
98  valueVec.setDefaultValue(defaultValue);
99  valueVec.setType(optionType);
100  Option option(longOption, shortOption, valueVec, false, docString);
101  p_vecMode.back().addOption(option);
102 }

References p_vecMode, OptionValue::setDefaultValue(), and OptionValue::setType().

+ Here is the call graph for this function:

◆ addOption() [5/8]

template<typename T >
void OptionParser::addOption ( const std::string &  longOption,
const std::string &  shortOption,
const T  defaultValue,
const std::string &  docString = "" 
)

Add an option in the OptionParser.

Parameters
longOption: long option (start with –) as –version
shortOption: short option (start with -) as -v
defaultValue: default value of the option
docString: documentation string of the option The type of the option will be automatically determined with the type of the default value

Definition at line 21 of file OptionParser_impl.h.

21  {
22  OptionType::OptionType optionType = getOptionTypeFromType<T>();
23  OptionValue value;
24  value.setType(optionType);
25  value.setDefaultValue(defaultValue);
26  Option option(longOption, shortOption, value, false, docString);
27  p_vecMode.back().addOption(option);
28 }

References p_vecMode, OptionValue::setDefaultValue(), and OptionValue::setType().

+ Here is the call graph for this function:

◆ addOption() [6/8]

template<typename T >
void OptionParser::addOption ( const std::string &  longOption,
const std::string &  shortOption,
const T  defaultValue,
OptionType::OptionType  optionType,
const std::string &  docString = "" 
)

Add an option in the OptionParser.

Parameters
longOption: long option (start with –) as –version
shortOption: short option (start with -) as -v
defaultValue: default value of the option
optionType: type of the data of the option (int, float, double, string, etc)
docString: documentation string of the option The function will check if the given optionType is relevant with the type of the default value

Definition at line 39 of file OptionParser_impl.h.

41 {
42  checkOptionType<T>(optionType);
43  OptionValue value;
44  value.setType(optionType);
45  value.setDefaultValue(defaultValue);
46  Option option(longOption, shortOption, value, false, docString);
47  p_vecMode.back().addOption(option);
48 }

References p_vecMode, OptionValue::setDefaultValue(), and OptionValue::setType().

+ Here is the call graph for this function:

◆ addOption() [7/8]

void OptionParser::addOption ( const std::string &  longOption,
const std::string &  shortOption,
OptionType::OptionType  optionType,
bool  isRequired,
bool  isAllowEmpty,
const std::string &  docString 
)

Add an option in the OptionParser.

Parameters
longOption: long option (start with –) as –version
shortOption: short option (start with -) as -v
optionType: type of the value to be parsed (INT, BOOL_ CHAR, FLOAT, STRING, FILENAME, DIRECTORY, etc)
isRequired: true if the option is required, false if it is optionnal
isAllowEmpty: the given value can be empty
docString: documentation string of the option

Definition at line 94 of file OptionParser.cpp.

96 {
97  OptionValue value;
98  value.setType(optionType);
99  Option option(longOption, shortOption, value, isRequired, docString);
100  option.setIsAllowEmpty(isAllowEmpty);
101  p_vecMode[p_currentMode].addOption(option);
102 }

References p_currentMode, p_vecMode, Option::setIsAllowEmpty(), and OptionValue::setType().

+ Here is the call graph for this function:

◆ addOption() [8/8]

void OptionParser::addOption ( const std::string &  longOption,
const std::string &  shortOption,
OptionType::OptionType  optionType,
bool  isRequired,
const std::string &  docString 
)

Add an option in the OptionParser.

Parameters
longOption: long option (start with –) as –version
shortOption: short option (start with -) as -v
optionType: type of the value to be parsed (INT, BOOL_ CHAR, FLOAT, STRING, FILENAME, DIRECTORY, etc)
isRequired: true if the option is required, false if it is optionnal
docString: documentation string of the option

Definition at line 76 of file OptionParser.cpp.

78 {
79  OptionValue value;
80  value.setType(optionType);
81  Option option(longOption, shortOption, value, isRequired, docString);
82  option.setIsAllowEmpty(false);
83  p_vecMode[p_currentMode].addOption(option);
84 }

References p_currentMode, p_vecMode, Option::setIsAllowEmpty(), and OptionValue::setType().

+ Here is the call graph for this function:

◆ checkArgument()

bool OptionParser::checkArgument ( ) const
private

Check the argument of the parser.

Returns
true if all the required arguments are set, false otherwise

Definition at line 292 of file OptionParser.cpp.

292  {
293  bool isArgOk(true);
294  VecMode::const_iterator it(p_vecMode.begin());
295  while(it != p_vecMode.end() && isArgOk){
296  isArgOk = it->checkArgument();
297  ++it;
298  }
299  return isArgOk;
300 }

References p_vecMode.

Referenced by parseArgumentNormalUse().

+ Here is the caller graph for this function:

◆ checkOptionType()

template<typename T >
void OptionParser::checkOptionType ( OptionType::OptionType  optionType)
private

Check the given option type.

Parameters
optionType: type of the option to be checked Throw an exception if there is a problem

Definition at line 129 of file OptionParser_impl.h.

129  {
130  OptionType::OptionType optionTypeFromDefault = getOptionTypeFromType<T>();
131  if(!isOptionTypeCompatible(optionType, optionTypeFromDefault)){
132  std::stringstream strError;
133  strError << "OptionParser::checkOptionType : Incompatible types from parameters (" << convertOptionTypeToString(optionType) << ") ad for default type ("<<convertOptionTypeToString(optionTypeFromDefault)<<")";
134  throw std::runtime_error(strError.str());
135  }
136 }

References convertOptionTypeToString(), and isOptionTypeCompatible().

+ Here is the call graph for this function:

◆ closeMode()

void OptionParser::closeMode ( )

Close the current mode and go back to be default one.

Definition at line 65 of file OptionParser.cpp.

65  {
66  p_currentMode = 0lu;
67 }

References p_currentMode.

◆ completeOptionValue()

bool OptionParser::completeOptionValue ( std::string &  possibleValue,
const std::string &  cursorOption,
const std::string &  prevCursorOption 
) const
private

Complete the possible value of an option (FILENAME, DIRECTORY, FILE_OR_DIR)

Parameters
[out]possibleValue: possible value
cursorOption: option of the cursor which is currently completed
prevCursorOption: previous option of the cursor

Definition at line 338 of file OptionParser.cpp.

338  {
339 // std::cerr << "OptionParser::completeOptionValue : cursorOption = '"<<cursorOption<<"', prevCursorOption = '"<<prevCursorOption<<"'" << std::endl;
340  std::string valueToBeCompleted("");
341  //Check is the cursor option starts with a long option (--option=value) because the value can be completed
342  const Option * op = getLongOptionValue(valueToBeCompleted, cursorOption);
343  if(op == NULL){ //Check is the previous option corresponds to an existing option, to complete the value given by the current option
344  op = getSplitOptionValue(valueToBeCompleted, cursorOption, prevCursorOption);
345  }
346  if(op != NULL){
347  op->getPossibleValue(possibleValue, valueToBeCompleted);
348 // std::cerr << "OptionParser::completeOptionValue : possibleValue = '"<<possibleValue<<"'" << std::endl;
349  return true;
350  }
351  return false;
352 }

References getLongOptionValue(), Option::getPossibleValue(), and getSplitOptionValue().

Referenced by parseArgumentBashCompletion().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ copyOptionParser()

void OptionParser::copyOptionParser ( const OptionParser other)
protected

Copy function of OptionParser.

Parameters
other: class to copy

Definition at line 191 of file OptionParser.cpp.

References p_currentMode, p_enableHelpOption, p_exempleLongOption, p_exempleShortOption, p_programVersion, and p_vecMode.

Referenced by operator=(), and OptionParser().

+ Here is the caller graph for this function:

◆ getCurrentlyParsedMode()

const OptionMode * OptionParser::getCurrentlyParsedMode ( ) const
private

Get the currently parsed OptionMode.

Returns
pointer to the currently parsed OptionMode, it there is one, or NULL is there is not

Definition at line 321 of file OptionParser.cpp.

321  {
322  const OptionMode * mode = NULL;
323  VecMode::const_iterator it(p_vecMode.begin());
324  while(it != p_vecMode.end() && mode == NULL){
325  if(it->isCurrentlyParsed() && it->getName() != ""){
326  mode = &(*it);
327  }
328  ++it;
329  }
330  return mode;
331 }

References p_vecMode.

Referenced by getPossibleOption().

+ Here is the caller graph for this function:

◆ getDefaultMode() [1/2]

OptionMode& OptionParser::getDefaultMode ( )

◆ getDefaultMode() [2/2]

OptionMode & OptionParser::getDefaultMode ( ) const

Get default mode.

Returns
default mode

Definition at line 137 of file OptionParser.cpp.

137  {
138  return p_vecMode[0lu];
139 }

References p_vecMode.

Referenced by getMode().

+ Here is the caller graph for this function:

◆ getLongOptionValue()

const Option * OptionParser::getLongOptionValue ( std::string &  valueToBeCompleted,
const std::string &  cursorOption 
) const
private

Get the long option value to be completed.

Parameters
[out]valueToBeCompleted: base of the value to be completed
cursorOption: option of the cursor which is currently completed
Returns
pointer to the Option to be completed or NULL if there is not

Definition at line 359 of file OptionParser.cpp.

359  {
360  if(cursorOption == ""){return NULL;}
361  const Option * op = NULL;
362  VecMode::const_iterator itMode(p_vecMode.begin());
363  while(itMode != p_vecMode.end() && op == NULL){
364  if(itMode->isCurrentlyParsed()){
365  const VecOption & vecOp = itMode->getVecOption();
366  VecOption::const_iterator itOp(vecOp.begin());
367  while(itOp != vecOp.end() && op == NULL){
368  std::string fullOp("--" + itOp->getLongName() + "=");
369  if(isSameBegining(cursorOption, fullOp)){
370  op = &(*itOp);
371  valueToBeCompleted = cursorOption.substr(fullOp.size());
372  }
373  ++itOp;
374  }
375  }
376  ++itMode;
377  }
378  return op;
379 }

References isSameBegining(), and p_vecMode.

Referenced by completeOptionValue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMode() [1/2]

OptionMode & OptionParser::getMode ( const std::string &  name)

Get mode by name.

Parameters
name: name of the mode to be returned
Returns
corresponding mode if the mode exists or the default mode otherwise

Definition at line 165 of file OptionParser.cpp.

165  {
166  for(VecMode::iterator it(p_vecMode.begin()); it != p_vecMode.end(); ++it){
167  if(it->getName() == name){
168  return *it;
169  }
170  }
171  return getDefaultMode();
172 }

References getDefaultMode(), and p_vecMode.

+ Here is the call graph for this function:

◆ getMode() [2/2]

const OptionMode & OptionParser::getMode ( const std::string &  name) const

Get mode by name.

Parameters
name: name of the mode to be returned
Returns
corresponding mode if the mode exists or the default mode otherwise

Definition at line 152 of file OptionParser.cpp.

152  {
153  for(VecMode::const_iterator it(p_vecMode.begin()); it != p_vecMode.end(); ++it){
154  if(it->getName() == name){
155  return *it;
156  }
157  }
158  return getDefaultMode();
159 }

References getDefaultMode(), and p_vecMode.

Referenced by getParserMode().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getParserMode()

OptionMode & OptionParser::getParserMode ( ArgParser parser)
private

Get a mode if it exist.

Parameters
parser: parser to be used
Returns
corresponding mode

Definition at line 306 of file OptionParser.cpp.

306  {
307  std::string currentOption(parser.getCurrentOption());
308  if(isModeExist(currentOption)){
309  OptionMode & mode = getMode(currentOption);
310  p_currentParserMode = &mode;
311  parser.getNextOption();
312  return mode;
313  }else{
314  return *p_currentParserMode;
315  }
316 }

References getMode(), isModeExist(), p_currentParserMode, and createReleaseCurl::parser.

Referenced by parseArgumentNormalUse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getPossibleOption()

void OptionParser::getPossibleOption ( std::string &  possibleOption,
const std::string &  cursorOption 
) const
private

Get the possible options which can be used.

Parameters
[out]possibleOption: possible options for the bash completion
cursorOption: option of the cursor which is currently completed

Definition at line 416 of file OptionParser.cpp.

416  {
417  if(p_vecMode.size() == 1lu){
418  p_vecMode.front().getPossibleOption(possibleOption, cursorOption);
419  }else{
420  const OptionMode * currentlyParsedMode = getCurrentlyParsedMode();
421  if(currentlyParsedMode != NULL){
422  currentlyParsedMode->getPossibleOption(possibleOption, cursorOption);
423  }else{
424  for(VecMode::const_iterator itMode = p_vecMode.begin(); itMode != p_vecMode.end(); ++itMode){
425  itMode->getPossibleMode(possibleOption, cursorOption);
426  }
427  }
428  }
429 }

References getCurrentlyParsedMode(), OptionMode::getPossibleOption(), and p_vecMode.

Referenced by parseArgumentBashCompletion().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getPossibleOtherOption()

void OptionParser::getPossibleOtherOption ( std::string &  possibleOption,
const std::string &  cursorOption 
) const
private

Get the possible other options which can be used.

Parameters
[out]possibleOption: possible options for the bash completion
cursorOption: option of the cursor which is currently completed

Definition at line 435 of file OptionParser.cpp.

435  {
436  std::vector<std::string> vecOtherOption;
437  vecOtherOption.push_back("--help");
438  vecOtherOption.push_back("-h");
439  vecOtherOption.push_back("--version");
440  vecOtherOption.push_back("-v");
441 
442  for(std::vector<std::string>::iterator it(vecOtherOption.begin()); it != vecOtherOption.end(); ++it){
443  std::string optionStr(*it);
444  if(cursorOption == ""){
445  possibleOption += optionStr + " ";
446  }else{
447  if(isSameBegining(optionStr, cursorOption)){
448  possibleOption += optionStr + " ";
449  }
450  }
451  }
452 }

References isSameBegining().

Referenced by parseArgumentBashCompletion().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getSplitOptionValue()

const Option * OptionParser::getSplitOptionValue ( std::string &  valueToBeCompleted,
const std::string &  cursorOption,
const std::string &  prevCursorOption 
) const
private

Get the split option (without =) value to be completed.

Parameters
[out]valueToBeCompleted: base of the value to be completed
cursorOption: option of the cursor which is currently completed
prevCursorOption: previous option of the cursor
Returns
pointer to the Option to be completed or NULL if there is not

Definition at line 387 of file OptionParser.cpp.

387  {
388  if(prevCursorOption == ""){return NULL;}
389  const Option * op = NULL;
390  VecMode::const_iterator itMode(p_vecMode.begin());
391  while(itMode != p_vecMode.end() && op == NULL){
392  if(itMode->isCurrentlyParsed()){ //Search only in the mode which is currently parsed
393  const VecOption & vecOp = itMode->getVecOption();
394  VecOption::const_iterator itOp(vecOp.begin());
395  while(itOp != vecOp.end() && op == NULL){
396  std::string fullLongOp("--" + itOp->getLongName()), fullShortOption("-" + itOp->getShortName());
397  if(fullLongOp == prevCursorOption){
398  op = &(*itOp);
399  valueToBeCompleted = cursorOption;
400  }else if(fullShortOption == prevCursorOption){
401  op = &(*itOp);
402  valueToBeCompleted = cursorOption;
403  }
404  ++itOp;
405  }
406  }
407  ++itMode;
408  }
409  return op;
410 }

References p_vecMode.

Referenced by completeOptionValue().

+ Here is the caller graph for this function:

◆ initialisationOptionParser()

void OptionParser::initialisationOptionParser ( )
private

Initialisation function of the class OptionParser.

Definition at line 201 of file OptionParser.cpp.

201  {
202  p_currentMode = 0lu;
203  p_currentParserMode = NULL;
204  OptionMode defaultMode;
205  p_vecMode.push_back(defaultMode);
206 }

References p_currentMode, p_currentParserMode, and p_vecMode.

Referenced by OptionParser().

+ Here is the caller graph for this function:

◆ isModeExist()

bool OptionParser::isModeExist ( const std::string &  name) const

Check if the given mode name does exist.

Parameters
name: name of the mode
Returns
true if the given mode name does exist, false otherwise

Definition at line 178 of file OptionParser.cpp.

178  {
179  bool isSearch(true);
180  VecMode::const_iterator it(p_vecMode.begin());
181  while(isSearch && it != p_vecMode.end()){
182  isSearch &= it->getName() != name;
183  ++it;
184  }
185  return !isSearch;
186 }

References p_vecMode.

Referenced by getParserMode().

+ Here is the caller graph for this function:

◆ operator=()

OptionParser & OptionParser::operator= ( const OptionParser other)

Definition of equal operator of OptionParser.

Parameters
other: class to copy
Returns
copied class

Definition at line 38 of file OptionParser.cpp.

38  {
39  copyOptionParser(other);
40  return *this;
41 }

References copyOptionParser().

+ Here is the call graph for this function:

◆ parseArgument()

void OptionParser::parseArgument ( int  argc,
char **  argv 
)

Parse the arguments passed to the program.

Parameters
argc: number of parameters passed to the program
argv: list of arguments passed to the program

Definition at line 125 of file OptionParser.cpp.

125  {
126  ArgParser parser(argc, argv);
127  if(parser.isBashCompletionMode()){
129  }else{
131  }
132 }

References parseArgumentBashCompletion(), parseArgumentNormalUse(), and createReleaseCurl::parser.

+ Here is the call graph for this function:

◆ parseArgumentBashCompletion()

void OptionParser::parseArgumentBashCompletion ( ArgParser parser)
private

Bash completion argument parsing mode.

Parameters
parser: parser to be used

Definition at line 244 of file OptionParser.cpp.

244  {
245  //First step, we parse normally the existing arguments
246  std::string cursorOption(parser.getCursorOption());
247  std::string prevCursorOption(parser.getPrevCursorOption());
248  Option * partialOption = NULL;
249  while(!parser.isEndOfOption()){ //We loop to find the option which has not been parsed well
250  bool isSearch(true);
251  VecMode::iterator itMode = p_vecMode.begin();
252  while(!parser.isEndOfOption() && itMode != p_vecMode.end() && isSearch){
253  isSearch = !itMode->parseOption(parser, partialOption);
254  ++itMode;
255  }
256  if(isSearch){ //If no option matches, we go to the next argument
257  parser.getNextOption();
258  }
259  }
260  //Complete the ongoing option (filename, directory, enum value, etc)
261 // if(partialOption != NULL){ //The option name has no ambiguity but we expect a value
262 // std::string possibleValue("");
263 // partialOption->getPossibleValue(possibleValue, cursorOption);
264 // cout << possibleValue << endl;
265 // }else{
266  //Let's get the mode which is currently parsed
267  //If all the options are fine, we have to show the remaning options. That's the following
268  std::string possibleValue("");
269  if(completeOptionValue(possibleValue, cursorOption, prevCursorOption)){
270 // saveFileContent("listPossibleValues.txt", possibleValue);
271  std::cout << possibleValue << std::endl;
272  }else{
273  //Then, get the remaning arguments (not parsed yet)
274  std::string possibleOption("");
275  getPossibleOption(possibleOption, cursorOption);
276  getPossibleOtherOption(possibleOption, cursorOption);
277 
278 // saveFileContent("listPossibleOption.txt", possibleOption);
279  std::cout << possibleOption << std::endl;
280  }
281 // }
282  exit(0);
283 // La marche a suivre est assez simple
284 // On renvoie une ligne par argument possible
285 // Si il y en a qu'une seule, elle sera ajouté à la fin de la ligne de commande
286 // DONE : mettre en place un méchanisme pour générer le bash qui appellera le programme différemment
287 }

References completeOptionValue(), getPossibleOption(), getPossibleOtherOption(), p_vecMode, and createReleaseCurl::parser.

Referenced by parseArgument().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parseArgumentNormalUse()

void OptionParser::parseArgumentNormalUse ( ArgParser parser)
private

Classical argument parsing mode.

Parameters
parser: parser to be used

Definition at line 211 of file OptionParser.cpp.

211  {
212  p_currentParserMode = &p_vecMode.front();
213  while(!parser.isEndOfOption()){
214  if(p_enableHelpOption){
215  if(parser.getCurrentOption() == "--help" || parser.getCurrentOption() == "-h"){
216  print();
217  exit(0);
218  }
219  }
220  if(p_programVersion != ""){
221  if(parser.getCurrentOption() == "--version" || parser.getCurrentOption() == "-v"){
222  cout << "Program version : " << p_programVersion << endl;
223  exit(0);
224  }
225  }
226  OptionMode & currentMode = getParserMode(parser);
227  if(!currentMode.parseOption(parser)){
228  std::string modeName(currentMode.getName());
229  std::string modeError("");
230  if(modeName != ""){
231  modeError = " in mode '"+modeName+"' ";
232  }
233  throw std::runtime_error("OptionParser::parseArgument : unknown option '"+parser.getCurrentOption()+"'" + modeError);
234  }
235  }
236  if(!checkArgument()){
237  throw std::runtime_error("OptionParser::parseArgument : missing argument");
238  }
239 }

References checkArgument(), OptionMode::getName(), getParserMode(), p_currentParserMode, p_enableHelpOption, p_programVersion, p_vecMode, OptionMode::parseOption(), createReleaseCurl::parser, and print().

Referenced by parseArgument().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print()

void OptionParser::print ( ) const

Print all the options.

Definition at line 105 of file OptionParser.cpp.

105  {
106  if(p_exempleLongOption != "" || p_exempleShortOption != ""){
107  cout << "Usage :" << endl;
108  if(p_exempleLongOption != ""){cout << "\t" << p_exempleLongOption << endl;}
109  if(p_exempleShortOption != ""){cout << "\t" << p_exempleShortOption << endl;}
110  }
111  cout << "Parameters :" << endl;
112  VecMode::const_iterator it(p_vecMode.begin());
113  it->print();
114  ++it;
115  while(it != p_vecMode.end()){
116  it->print();
117  ++it;
118  }
119 }

References p_exempleLongOption, p_exempleShortOption, and p_vecMode.

Referenced by main(), and parseArgumentNormalUse().

+ Here is the caller graph for this function:

◆ setExampleLongOption()

void OptionParser::setExampleLongOption ( const std::string &  example)

Set the example usage of the program.

Parameters
example: example of usage

Definition at line 46 of file OptionParser.cpp.

46 {p_exempleLongOption = example;}

References p_exempleLongOption.

◆ setExampleShortOption()

void OptionParser::setExampleShortOption ( const std::string &  example)

Set the example usage of the program.

Parameters
example: example of usage

Definition at line 51 of file OptionParser.cpp.

51 {p_exempleShortOption = example;}

References p_exempleShortOption.

Member Data Documentation

◆ p_currentMode

size_t OptionParser::p_currentMode
private

Index of the current mode in the OptionParser.

Definition at line 94 of file OptionParser.h.

Referenced by addMode(), addOption(), closeMode(), copyOptionParser(), and initialisationOptionParser().

◆ p_currentParserMode

OptionMode* OptionParser::p_currentParserMode
private

Current mode parsed.

Definition at line 90 of file OptionParser.h.

Referenced by getParserMode(), initialisationOptionParser(), and parseArgumentNormalUse().

◆ p_enableHelpOption

bool OptionParser::p_enableHelpOption
private

True to enable automatically the printing of the help option when the program is called with –help or -h.

Definition at line 100 of file OptionParser.h.

Referenced by addMode(), copyOptionParser(), and parseArgumentNormalUse().

◆ p_exempleLongOption

std::string OptionParser::p_exempleLongOption
private

Usage example with long options.

Definition at line 96 of file OptionParser.h.

Referenced by copyOptionParser(), print(), and setExampleLongOption().

◆ p_exempleShortOption

std::string OptionParser::p_exempleShortOption
private

Usage example with short options.

Definition at line 98 of file OptionParser.h.

Referenced by copyOptionParser(), print(), and setExampleShortOption().

◆ p_programVersion

std::string OptionParser::p_programVersion
private

Program version to be printed on –version or -v option.

Definition at line 102 of file OptionParser.h.

Referenced by addMode(), copyOptionParser(), and parseArgumentNormalUse().

◆ p_vecMode


The documentation for this class was generated from the following files:
OptionParser::p_enableHelpOption
bool p_enableHelpOption
True to enable automatically the printing of the help option when the program is called with –help or...
Definition: OptionParser.h:100
OptionParser::getMode
const OptionMode & getMode(const std::string &name) const
Get mode by name.
Definition: OptionParser.cpp:152
convertOptionTypeToString
std::string convertOptionTypeToString(OptionType::OptionType type)
Convert the OptionType into string.
Definition: OptionType.cpp:69
OptionParser::p_vecMode
VecMode p_vecMode
Vector of all the defined mode in the OptionParser.
Definition: OptionParser.h:92
OptionParser::p_programVersion
std::string p_programVersion
Program version to be printed on –version or -v option.
Definition: OptionParser.h:102
OptionParser::p_currentParserMode
OptionMode * p_currentParserMode
Current mode parsed.
Definition: OptionParser.h:90
OptionValue::setType
void setType(OptionType::OptionType type)
Set the type of the OptionValue.
Definition: OptionValue.cpp:105
OptionParser::copyOptionParser
void copyOptionParser(const OptionParser &other)
Copy function of OptionParser.
Definition: OptionParser.cpp:191
OptionParser::parseArgumentBashCompletion
void parseArgumentBashCompletion(ArgParser &parser)
Bash completion argument parsing mode.
Definition: OptionParser.cpp:244
OptionParser::initialisationOptionParser
void initialisationOptionParser()
Initialisation function of the class OptionParser.
Definition: OptionParser.cpp:201
VecOption
std::vector< Option > VecOption
Vector of option.
Definition: Option.h:88
OptionMode::getName
const std::string & getName() const
Get the name of the OptionMode.
Definition: OptionMode.cpp:146
OptionParser::getDefaultMode
const OptionMode & getDefaultMode() const
Get default mode.
Definition: OptionParser.cpp:137
OptionParser::getLongOptionValue
const Option * getLongOptionValue(std::string &valueToBeCompleted, const std::string &cursorOption) const
Get the long option value to be completed.
Definition: OptionParser.cpp:359
OptionParser::p_currentMode
size_t p_currentMode
Index of the current mode in the OptionParser.
Definition: OptionParser.h:94
OptionMode::parseOption
bool parseOption(ArgParser &parser)
Parse the options in the current OptionMode.
Definition: OptionMode.cpp:47
OptionParser::isModeExist
bool isModeExist(const std::string &name) const
Check if the given mode name does exist.
Definition: OptionParser.cpp:178
Option::getPossibleValue
void getPossibleValue(std::string &possibleValue, const std::string &cursorOption) const
Complete the possible values of the Option.
Definition: Option.cpp:308
isSameBegining
bool isSameBegining(const std::string &str, const std::string &beginig)
Check if two string start the same way.
Definition: string_function.cpp:531
OptionMode::getPossibleOption
void getPossibleOption(std::string &possibleOption, const std::string &cursorOption) const
Get the possible options for the bash completion.
Definition: OptionMode.cpp:210
OptionParser::getCurrentlyParsedMode
const OptionMode * getCurrentlyParsedMode() const
Get the currently parsed OptionMode.
Definition: OptionParser.cpp:321
OptionParser::print
void print() const
Print all the options.
Definition: OptionParser.cpp:105
createReleaseCurl.parser
parser
Definition: createReleaseCurl.py:123
OptionParser::completeOptionValue
bool completeOptionValue(std::string &possibleValue, const std::string &cursorOption, const std::string &prevCursorOption) const
Complete the possible value of an option (FILENAME, DIRECTORY, FILE_OR_DIR)
Definition: OptionParser.cpp:338
OptionParser::checkArgument
bool checkArgument() const
Check the argument of the parser.
Definition: OptionParser.cpp:292
OptionValue::setDefaultValue
void setDefaultValue(const T &value)
Set the value in the OptionValue.
Definition: OptionValue_impl.h:17
isOptionTypeCompatible
bool isOptionTypeCompatible(OptionType::OptionType typeFromParam, OptionType::OptionType typeFromType)
Say if two types are compatible.
Definition: OptionType.cpp:54
OptionParser::getSplitOptionValue
const Option * getSplitOptionValue(std::string &valueToBeCompleted, const std::string &cursorOption, const std::string &prevCursorOption) const
Get the split option (without =) value to be completed.
Definition: OptionParser.cpp:387
OptionParser::parseArgumentNormalUse
void parseArgumentNormalUse(ArgParser &parser)
Classical argument parsing mode.
Definition: OptionParser.cpp:211
Option
Definition: Option.h:13
OptionType::OptionType
OptionType
Definition: OptionType.h:19
OptionParser::getPossibleOtherOption
void getPossibleOtherOption(std::string &possibleOption, const std::string &cursorOption) const
Get the possible other options which can be used.
Definition: OptionParser.cpp:435
OptionValue
Describe the value of an option passed to a program.
Definition: OptionValue.h:20
OptionParser::getParserMode
OptionMode & getParserMode(ArgParser &parser)
Get a mode if it exist.
Definition: OptionParser.cpp:306
OptionParser::getPossibleOption
void getPossibleOption(std::string &possibleOption, const std::string &cursorOption) const
Get the possible options which can be used.
Definition: OptionParser.cpp:416
OptionParser::p_exempleLongOption
std::string p_exempleLongOption
Usage example with long options.
Definition: OptionParser.h:96
OptionParser::p_exempleShortOption
std::string p_exempleShortOption
Usage example with short options.
Definition: OptionParser.h:98
ArgParser
Parse the list of arguments passed to a program.
Definition: ArgParser.h:16
OptionMode
Describe a mode in the program arguments.
Definition: OptionMode.h:13