PhoenixPresentation  2.0.0
Set of cmake function to automate presentation generation
OptionParser Class Reference

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

#include <OptionParser.h>

+ Collaboration diagram for OptionParser:

Public Member Functions

void addMode (const PString &modeName)
 Add a mode in the option. More...
 
template<typename T >
void addOption (const PString &longOption, const PString &shortOption, const T defaultValue, const PString &docString)
 Add an option in the OptionParser. More...
 
template<typename T >
void addOption (const PString &longOption, const PString &shortOption, const T defaultValue, OptionType::OptionType optionType, const PString &docString)
 Add an option in the OptionParser. More...
 
void addOption (const PString &longOption, const PString &shortOption, OptionType::OptionType optionType, bool isRequired, bool isAllowEmpty, const PString &docString)
 Add an option in the OptionParser. More...
 
void addOption (const PString &longOption, const PString &shortOption, OptionType::OptionType optionType, bool isRequired, bool isAllowEmpty, const PVecString &vecPossibleValue, const PString &docString)
 Add an option in the OptionParser. More...
 
void addOption (const PString &longOption, const PString &shortOption, OptionType::OptionType optionType, bool isRequired, const PString &docString)
 Add an option in the OptionParser. More...
 
void closeMode ()
 Close the current mode and go back to be default one. More...
 
OptionModegetDefaultMode ()
 Get default mode. More...
 
const OptionModegetDefaultMode () const
 Get default mode. More...
 
OptionModegetMode (const PString &name)
 Get mode by name. More...
 
const OptionModegetMode (const PString &name) const
 Get mode by name. More...
 
bool isModeExist (const PString &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 PString &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 PString &example)
 Set the example usage of the program. More...
 
void setExampleShortOption (const PString &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 (PString &possibleValue, const PString &cursorOption, const PString &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 (PString &valueToBeCompleted, const PString &cursorOption) const
 Get the long option value to be completed. More...
 
OptionModegetParserMode (ArgParser &parser)
 Get a mode if it exist. More...
 
void getPossibleOption (PString &possibleOption, const PString &cursorOption) const
 Get the possible options which can be used. More...
 
void getPossibleOtherOption (PString &possibleOption, const PString &cursorOption) const
 Get the possible other options which can be used. More...
 
const OptiongetSplitOptionValue (PString &valueToBeCompleted, const PString &cursorOption, const PString &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...
 
PString p_exempleLongOption
 Usage example with long options. More...
 
PString p_exempleShortOption
 Usage example with short options. More...
 
PString 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 PString 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 13 of file OptionParser.cpp.

14  :p_enableHelpOption(enableHelpOption), p_programVersion(programVersion)
15 {
17 }
bool p_enableHelpOption
True to enable automatically the printing of the help option when the program is called with –help or...
Definition: OptionParser.h:86
void initialisationOptionParser()
Initialisation function of the class OptionParser.
PString p_programVersion
Program version to be printed on –version or -v option.
Definition: OptionParser.h:88

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 22 of file OptionParser.cpp.

22  {
23  copyOptionParser(other);
24 }
void copyOptionParser(const OptionParser &other)
Copy function of OptionParser.

References copyOptionParser().

+ Here is the call graph for this function:

◆ ~OptionParser()

OptionParser::~OptionParser ( )
virtual

Destructeur of OptionParser.

Definition at line 27 of file OptionParser.cpp.

27  {
28 
29 }

Member Function Documentation

◆ addMode()

void OptionParser::addMode ( const PString modeName)

Add a mode in the option.

Parameters
modeName: name of the new mode to be added

Definition at line 53 of file OptionParser.cpp.

53  {
54  OptionMode mode(modeName);
55  mode.setEnableHelpOption(p_enableHelpOption);
56  mode.setProgramVersion(p_programVersion);
57  p_vecMode.push_back(mode);
58  p_currentMode = p_vecMode.size() - 1lu;
59 }
Describe a mode in the program arguments.
Definition: OptionMode.h:13
VecMode p_vecMode
Vector of all the defined mode in the OptionParser.
Definition: OptionParser.h:78
size_t p_currentMode
Index of the current mode in the OptionParser.
Definition: OptionParser.h:80

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

Referenced by createOptionParser().

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

◆ addOption() [1/5]

template<typename T >
void OptionParser::addOption ( const PString longOption,
const PString shortOption,
const T  defaultValue,
const PString 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 }
Describe the value of an option passed to a program.
Definition: OptionValue.h:16
void setDefaultValue(const T &value)
Set the value in the OptionValue.
void setType(OptionType::OptionType type)
Set the type of the OptionValue.
Describes an option passed to a program.
Definition: Option.h:14

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

+ Here is the call graph for this function:

◆ addOption() [2/5]

template<typename T >
void OptionParser::addOption ( const PString longOption,
const PString shortOption,
const T  defaultValue,
OptionType::OptionType  optionType,
const PString 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() [3/5]

void OptionParser::addOption ( const PString longOption,
const PString shortOption,
OptionType::OptionType  optionType,
bool  isRequired,
bool  isAllowEmpty,
const PString 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 91 of file OptionParser.cpp.

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

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

+ Here is the call graph for this function:

◆ addOption() [4/5]

void OptionParser::addOption ( const PString longOption,
const PString shortOption,
OptionType::OptionType  optionType,
bool  isRequired,
bool  isAllowEmpty,
const PVecString vecPossibleValue,
const PString 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
vecPossibleValue: vector of possible values for this option
docString: documentation string of the option

Definition at line 110 of file OptionParser.cpp.

112 {
113  OptionValue value;
114  value.setType(optionType);
115  value.setVecPossibleValue(vecPossibleValue);
116  Option option(longOption, shortOption, value, isRequired, docString);
117  option.setIsAllowEmpty(isAllowEmpty);
118  p_vecMode[p_currentMode].addOption(option);
119 }
void setVecPossibleValue(const PVecString &vecPossibleValue)
Set the vector of possible values.

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

+ Here is the call graph for this function:

◆ addOption() [5/5]

void OptionParser::addOption ( const PString longOption,
const PString shortOption,
OptionType::OptionType  optionType,
bool  isRequired,
const PString 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 73 of file OptionParser.cpp.

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

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

Referenced by createOptionParser().

+ Here is the call graph for this function:
+ Here is the caller 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 309 of file OptionParser.cpp.

309  {
310  bool isArgOk(true);
311  VecMode::const_iterator it(p_vecMode.begin());
312  while(it != p_vecMode.end() && isArgOk){
313  isArgOk = it->checkArgument();
314  ++it;
315  }
316  return isArgOk;
317 }

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 55 of file OptionParser_impl.h.

55  {
56  OptionType::OptionType optionTypeFromDefault = getOptionTypeFromType<T>();
57  if(!isOptionTypeCompatible(optionType, optionTypeFromDefault)){
58  std::stringstream strError;
59  strError << "OptionParser::checkOptionType : Incompatible types from parameters (" << convertOptionTypeToString(optionType) << ") ad for default type ("<<convertOptionTypeToString(optionTypeFromDefault)<<")";
60  throw std::runtime_error(strError.str());
61  }
62 }
PString convertOptionTypeToString(OptionType::OptionType type)
Convert the OptionType into PString.
Definition: OptionType.cpp:69
bool isOptionTypeCompatible(OptionType::OptionType typeFromParam, OptionType::OptionType typeFromType)
Say if two types are compatible.
Definition: OptionType.cpp:54

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 62 of file OptionParser.cpp.

62  {
63  p_currentMode = 0lu;
64 }

References p_currentMode.

Referenced by createOptionParser().

+ Here is the caller graph for this function:

◆ completeOptionValue()

bool OptionParser::completeOptionValue ( PString possibleValue,
const PString cursorOption,
const PString 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 355 of file OptionParser.cpp.

355  {
356 // std::cerr << "OptionParser::completeOptionValue : cursorOption = '"<<cursorOption<<"', prevCursorOption = '"<<prevCursorOption<<"'" << std::endl;
357  PString valueToBeCompleted("");
358  //Check is the cursor option starts with a long option (--option=value) because the value can be completed
359  const Option * op = getLongOptionValue(valueToBeCompleted, cursorOption);
360  if(op == NULL){ //Check is the previous option corresponds to an existing option, to complete the value given by the current option
361  op = getSplitOptionValue(valueToBeCompleted, cursorOption, prevCursorOption);
362  }
363  if(op != NULL){
364  op->getPossibleValue(possibleValue, valueToBeCompleted);
365 // std::cerr << "OptionParser::completeOptionValue : possibleValue = '"<<possibleValue<<"'" << std::endl;
366  return true;
367  }
368  return false;
369 }
const Option * getSplitOptionValue(PString &valueToBeCompleted, const PString &cursorOption, const PString &prevCursorOption) const
Get the split option (without =) value to be completed.
const Option * getLongOptionValue(PString &valueToBeCompleted, const PString &cursorOption) const
Get the long option value to be completed.
void getPossibleValue(PString &possibleValue, const PString &cursorOption) const
Complete the possible values of the Option.
Definition: Option.cpp:315
Extends the std::string.
Definition: PString.h:16

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 208 of file OptionParser.cpp.

208  {
209  p_vecMode = other.p_vecMode;
215 }
PString p_exempleLongOption
Usage example with long options.
Definition: OptionParser.h:82
PString p_exempleShortOption
Usage example with short options.
Definition: OptionParser.h:84

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 338 of file OptionParser.cpp.

338  {
339  const OptionMode * mode = NULL;
340  VecMode::const_iterator it(p_vecMode.begin());
341  while(it != p_vecMode.end() && mode == NULL){
342  if(it->isCurrentlyParsed() && it->getName() != ""){
343  mode = &(*it);
344  }
345  ++it;
346  }
347  return mode;
348 }

References p_vecMode.

Referenced by getPossibleOption().

+ Here is the caller graph for this function:

◆ getDefaultMode() [1/2]

OptionMode & OptionParser::getDefaultMode ( )

Get default mode.

Returns
default mode

Definition at line 161 of file OptionParser.cpp.

161  {
162  return p_vecMode[0lu];
163 }

References p_vecMode.

◆ getDefaultMode() [2/2]

const OptionMode & OptionParser::getDefaultMode ( ) const

Get default mode.

Returns
default mode

Definition at line 154 of file OptionParser.cpp.

154  {
155  return p_vecMode[0lu];
156 }

References p_vecMode.

Referenced by getMode(), main(), and printConstParser().

+ Here is the caller graph for this function:

◆ getLongOptionValue()

const Option * OptionParser::getLongOptionValue ( PString valueToBeCompleted,
const PString 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 376 of file OptionParser.cpp.

376  {
377  if(cursorOption == ""){return NULL;}
378  const Option * op = NULL;
379  VecMode::const_iterator itMode(p_vecMode.begin());
380  while(itMode != p_vecMode.end() && op == NULL){
381  if(itMode->isCurrentlyParsed()){
382  const VecOption & vecOp = itMode->getVecOption();
383  VecOption::const_iterator itOp(vecOp.begin());
384  while(itOp != vecOp.end() && op == NULL){
385  PString fullOp("--" + itOp->getLongName() + "=");
386  if(cursorOption.isSameBegining(fullOp)){
387  op = &(*itOp);
388  valueToBeCompleted = cursorOption.substr(fullOp.size());
389  }
390  ++itOp;
391  }
392  }
393  ++itMode;
394  }
395  return op;
396 }
std::vector< Option > VecOption
Vector of option.
Definition: Option.h:90
bool isSameBegining(const PString &beginStr) const
Say if the current PString has the same begining of beginStr.
Definition: PString.cpp:306

References PString::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 PString 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 182 of file OptionParser.cpp.

182  {
183  for(VecMode::iterator it(p_vecMode.begin()); it != p_vecMode.end(); ++it){
184  if(it->getName() == name){
185  return *it;
186  }
187  }
188  return getDefaultMode();
189 }
const OptionMode & getDefaultMode() const
Get default mode.

References getDefaultMode(), and p_vecMode.

+ Here is the call graph for this function:

◆ getMode() [2/2]

const OptionMode & OptionParser::getMode ( const PString 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 169 of file OptionParser.cpp.

169  {
170  for(VecMode::const_iterator it(p_vecMode.begin()); it != p_vecMode.end(); ++it){
171  if(it->getName() == name){
172  return *it;
173  }
174  }
175  return getDefaultMode();
176 }

References getDefaultMode(), and p_vecMode.

Referenced by getParserMode(), main(), and printConstParser().

+ 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 323 of file OptionParser.cpp.

323  {
324  PString currentOption(parser.getCurrentOption());
325  if(isModeExist(currentOption)){
326  OptionMode & mode = getMode(currentOption);
327  p_currentParserMode = &mode;
328  parser.getNextOption();
329  return mode;
330  }else{
331  return *p_currentParserMode;
332  }
333 }
const PString & getCurrentOption() const
Get the current option.
Definition: ArgParser.cpp:91
void getNextOption()
Move to the next option.
Definition: ArgParser.cpp:77
const OptionMode & getMode(const PString &name) const
Get mode by name.
bool isModeExist(const PString &name) const
Check if the given mode name does exist.
OptionMode * p_currentParserMode
Current mode parsed.
Definition: OptionParser.h:76

References ArgParser::getCurrentOption(), getMode(), ArgParser::getNextOption(), isModeExist(), and p_currentParserMode.

Referenced by parseArgumentNormalUse().

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

◆ getPossibleOption()

void OptionParser::getPossibleOption ( PString possibleOption,
const PString 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 433 of file OptionParser.cpp.

433  {
434  if(p_vecMode.size() == 1lu){
435  p_vecMode.front().getPossibleOption(possibleOption, cursorOption);
436  }else{
437  const OptionMode * currentlyParsedMode = getCurrentlyParsedMode();
438  if(currentlyParsedMode != NULL){
439  currentlyParsedMode->getPossibleOption(possibleOption, cursorOption);
440  }else{
441  for(VecMode::const_iterator itMode = p_vecMode.begin(); itMode != p_vecMode.end(); ++itMode){
442  itMode->getPossibleMode(possibleOption, cursorOption);
443  }
444  }
445  }
446 }
void getPossibleOption(PString &possibleOption, const PString &cursorOption) const
Get the possible options for the bash completion.
Definition: OptionMode.cpp:225
const OptionMode * getCurrentlyParsedMode() const
Get the currently parsed OptionMode.

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 ( PString possibleOption,
const PString 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 452 of file OptionParser.cpp.

452  {
453  std::vector<PString> vecOtherOption;
454  vecOtherOption.push_back("--help");
455  vecOtherOption.push_back("-h");
456  vecOtherOption.push_back("--version");
457  vecOtherOption.push_back("-v");
458 
459  for(std::vector<PString>::iterator it(vecOtherOption.begin()); it != vecOtherOption.end(); ++it){
460  PString optionStr(*it);
461  if(cursorOption == ""){
462  possibleOption += optionStr + " ";
463  }else{
464  if(optionStr.isSameBegining(cursorOption)){
465  possibleOption += optionStr + " ";
466  }
467  }
468  }
469 }

References PString::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 ( PString valueToBeCompleted,
const PString cursorOption,
const PString 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 404 of file OptionParser.cpp.

404  {
405  if(prevCursorOption == ""){return NULL;}
406  const Option * op = NULL;
407  VecMode::const_iterator itMode(p_vecMode.begin());
408  while(itMode != p_vecMode.end() && op == NULL){
409  if(itMode->isCurrentlyParsed()){ //Search only in the mode which is currently parsed
410  const VecOption & vecOp = itMode->getVecOption();
411  VecOption::const_iterator itOp(vecOp.begin());
412  while(itOp != vecOp.end() && op == NULL){
413  PString fullLongOp("--" + itOp->getLongName()), fullShortOption("-" + itOp->getShortName());
414  if(fullLongOp == prevCursorOption){
415  op = &(*itOp);
416  valueToBeCompleted = cursorOption;
417  }else if(fullShortOption == prevCursorOption){
418  op = &(*itOp);
419  valueToBeCompleted = cursorOption;
420  }
421  ++itOp;
422  }
423  }
424  ++itMode;
425  }
426  return op;
427 }

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 218 of file OptionParser.cpp.

218  {
219  p_currentMode = 0lu;
220  p_currentParserMode = NULL;
221  OptionMode defaultMode;
222  p_vecMode.push_back(defaultMode);
223 }

References p_currentMode, p_currentParserMode, and p_vecMode.

Referenced by OptionParser().

+ Here is the caller graph for this function:

◆ isModeExist()

bool OptionParser::isModeExist ( const PString 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 195 of file OptionParser.cpp.

195  {
196  bool isSearch(true);
197  VecMode::const_iterator it(p_vecMode.begin());
198  while(isSearch && it != p_vecMode.end()){
199  isSearch &= it->getName() != name;
200  ++it;
201  }
202  return !isSearch;
203 }

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 35 of file OptionParser.cpp.

35  {
36  copyOptionParser(other);
37  return *this;
38 }

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 142 of file OptionParser.cpp.

142  {
143  ArgParser parser(argc, argv);
144  if(parser.isBashCompletionMode()){
146  }else{
147  parseArgumentNormalUse(parser);
148  }
149 }
Parse the list of arguments passed to a program.
Definition: ArgParser.h:13
void parseArgumentNormalUse(ArgParser &parser)
Classical argument parsing mode.
void parseArgumentBashCompletion(ArgParser &parser)
Bash completion argument parsing mode.

References ArgParser::isBashCompletionMode(), parseArgumentBashCompletion(), and parseArgumentNormalUse().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller 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 261 of file OptionParser.cpp.

261  {
262  //First step, we parse normally the existing arguments
263  PString cursorOption(parser.getCursorOption());
264  PString prevCursorOption(parser.getPrevCursorOption());
265  Option * partialOption = NULL;
266  while(!parser.isEndOfOption()){ //We loop to find the option which has not been parsed well
267  bool isSearch(true);
268  VecMode::iterator itMode = p_vecMode.begin();
269  while(!parser.isEndOfOption() && itMode != p_vecMode.end() && isSearch){
270  isSearch = !itMode->parseOption(parser, partialOption);
271  ++itMode;
272  }
273  if(isSearch){ //If no option matches, we go to the next argument
274  parser.getNextOption();
275  }
276  }
277  //Complete the ongoing option (filename, directory, enum value, etc)
278 // if(partialOption != NULL){ //The option name has no ambiguity but we expect a value
279 // PString possibleValue("");
280 // partialOption->getPossibleValue(possibleValue, cursorOption);
281 // cout << possibleValue << endl;
282 // }else{
283  //Let's get the mode which is currently parsed
284  //If all the options are fine, we have to show the remaning options. That's the following
285  PString possibleValue("");
286  if(completeOptionValue(possibleValue, cursorOption, prevCursorOption)){
287 // saveFileContent("listPossibleValues.txt", possibleValue);
288  std::cout << possibleValue << std::endl;
289  }else{
290  //Then, get the remaning arguments (not parsed yet)
291  PString possibleOption("");
292  getPossibleOption(possibleOption, cursorOption);
293  getPossibleOtherOption(possibleOption, cursorOption);
294 
295 // saveFileContent("listPossibleOption.txt", possibleOption);
296  std::cout << possibleOption << std::endl;
297  }
298 // }
299  exit(0);
300 // La marche a suivre est assez simple
301 // On renvoie une ligne par argument possible
302 // Si il y en a qu'une seule, elle sera ajouté à la fin de la ligne de commande
303 // DONE : mettre en place un méchanisme pour générer le bash qui appellera le programme différemment
304 }
const PString & getCursorOption() const
Get the cursor option given to the program, in bash completion mode.
Definition: ArgParser.cpp:119
const PString & getPrevCursorOption() const
Get the previous option before the cursor option.
Definition: ArgParser.cpp:126
bool isEndOfOption() const
Say if is it the end of the options.
Definition: ArgParser.cpp:84
bool completeOptionValue(PString &possibleValue, const PString &cursorOption, const PString &prevCursorOption) const
Complete the possible value of an option (FILENAME, DIRECTORY, FILE_OR_DIR)
void getPossibleOtherOption(PString &possibleOption, const PString &cursorOption) const
Get the possible other options which can be used.
void getPossibleOption(PString &possibleOption, const PString &cursorOption) const
Get the possible options which can be used.

References completeOptionValue(), ArgParser::getCursorOption(), ArgParser::getNextOption(), getPossibleOption(), getPossibleOtherOption(), ArgParser::getPrevCursorOption(), ArgParser::isEndOfOption(), and p_vecMode.

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 228 of file OptionParser.cpp.

228  {
229  p_currentParserMode = &p_vecMode.front();
230  while(!parser.isEndOfOption()){
231  if(p_enableHelpOption){
232  if(parser.getCurrentOption() == "--help" || parser.getCurrentOption() == "-h"){
233  print();
234  exit(0);
235  }
236  }
237  if(p_programVersion != ""){
238  if(parser.getCurrentOption() == "--version" || parser.getCurrentOption() == "-v"){
239  std::cout << "Program version : " << p_programVersion << std::endl;
240  exit(0);
241  }
242  }
243  OptionMode & currentMode = getParserMode(parser);
244  if(!currentMode.parseOption(parser)){
245  PString modeName(currentMode.getName());
246  PString modeError("");
247  if(modeName != ""){
248  modeError = " in mode '"+modeName+"' ";
249  }
250  throw std::runtime_error("OptionParser::parseArgument : unknown option '"+parser.getCurrentOption()+"'" + modeError);
251  }
252  }
253  if(!checkArgument()){
254  throw std::runtime_error("OptionParser::parseArgument : missing argument");
255  }
256 }
bool parseOption(ArgParser &parser)
Parse the options in the current OptionMode.
Definition: OptionMode.cpp:44
const PString & getName() const
Get the name of the OptionMode.
Definition: OptionMode.cpp:154
void print() const
Print all the options.
bool checkArgument() const
Check the argument of the parser.
OptionMode & getParserMode(ArgParser &parser)
Get a mode if it exist.

References checkArgument(), ArgParser::getCurrentOption(), OptionMode::getName(), getParserMode(), ArgParser::isEndOfOption(), p_currentParserMode, p_enableHelpOption, p_programVersion, p_vecMode, OptionMode::parseOption(), 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 122 of file OptionParser.cpp.

122  {
123  if(p_exempleLongOption != "" || p_exempleShortOption != ""){
124  std::cout << "Usage :" << std::endl;
125  if(p_exempleLongOption != ""){std::cout << "\t" << p_exempleLongOption << std::endl;}
126  if(p_exempleShortOption != ""){std::cout << "\t" << p_exempleShortOption << std::endl;}
127  }
128  std::cout << "Parameters :" << std::endl;
129  VecMode::const_iterator it(p_vecMode.begin());
130  it->print();
131  ++it;
132  while(it != p_vecMode.end()){
133  it->print();
134  ++it;
135  }
136 }

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 PString example)

Set the example usage of the program.

Parameters
example: example of usage

Definition at line 43 of file OptionParser.cpp.

43 {p_exempleLongOption = example;}

References p_exempleLongOption.

Referenced by createOptionParser().

+ Here is the caller graph for this function:

◆ setExampleShortOption()

void OptionParser::setExampleShortOption ( const PString example)

Set the example usage of the program.

Parameters
example: example of usage

Definition at line 48 of file OptionParser.cpp.

48 {p_exempleShortOption = example;}

References p_exempleShortOption.

Referenced by createOptionParser().

+ Here is the caller graph for this function:

Member Data Documentation

◆ p_currentMode

size_t OptionParser::p_currentMode
private

Index of the current mode in the OptionParser.

Definition at line 80 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 76 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 86 of file OptionParser.h.

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

◆ p_exempleLongOption

PString OptionParser::p_exempleLongOption
private

Usage example with long options.

Definition at line 82 of file OptionParser.h.

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

◆ p_exempleShortOption

PString OptionParser::p_exempleShortOption
private

Usage example with short options.

Definition at line 84 of file OptionParser.h.

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

◆ p_programVersion

PString OptionParser::p_programVersion
private

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

Definition at line 88 of file OptionParser.h.

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

◆ p_vecMode


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