PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
OptionParser.cpp
Go to the documentation of this file.
1 /***************************************
2  Auteur : Pierre Aubert
3  Mail : pierre.aubert@lapp.in2p3.fr
4  Licence : CeCILL-C
5 ****************************************/
6 
7 #include "string_utils.h"
8 #include "OptionParser.h"
9 
10 using namespace std;
11 
13 
16 OptionParser::OptionParser(bool enableHelpOption, const std::string & programVersion)
17  :p_enableHelpOption(enableHelpOption), p_programVersion(programVersion)
18 {
20 }
21 
23 
26  copyOptionParser(other);
27 }
28 
31 
32 }
33 
35 
39  copyOptionParser(other);
40  return *this;
41 }
42 
44 
46 void OptionParser::setExampleLongOption(const std::string & example){p_exempleLongOption = example;}
47 
49 
51 void OptionParser::setExampleShortOption(const std::string & example){p_exempleShortOption = example;}
52 
54 
56 void OptionParser::addMode(const std::string & modeName){
57  OptionMode mode(modeName);
60  p_vecMode.push_back(mode);
61  p_currentMode = p_vecMode.size() - 1lu;
62 }
63 
66  p_currentMode = 0lu;
67 }
68 
70 
76 void OptionParser::addOption(const std::string & longOption, const std::string & shortOption, OptionType::OptionType optionType,
77  bool isRequired, const std::string & docString)
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 }
85 
87 
94 void OptionParser::addOption(const std::string & longOption, const std::string & shortOption, OptionType::OptionType optionType,
95  bool isRequired, bool isAllowEmpty, const std::string & docString)
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 }
103 
105 void OptionParser::print() const{
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 }
120 
122 
125 void OptionParser::parseArgument(int argc, char** argv){
126  ArgParser parser(argc, argv);
127  if(parser.isBashCompletionMode()){
129  }else{
131  }
132 }
133 
135 
138  return p_vecMode[0lu];
139 }
140 
142 
145  return p_vecMode[0lu];
146 }
147 
149 
152 const OptionMode & OptionParser::getMode(const std::string & name) const{
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 }
160 
162 
165 OptionMode & OptionParser::getMode(const std::string & name){
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 }
173 
175 
178 bool OptionParser::isModeExist(const std::string & name) const{
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 }
187 
189 
192  p_vecMode = other.p_vecMode;
198 }
199 
202  p_currentMode = 0lu;
203  p_currentParserMode = NULL;
204  OptionMode defaultMode;
205  p_vecMode.push_back(defaultMode);
206 }
207 
209 
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 }
240 
242 
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 }
288 
290 
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 }
301 
303 
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 }
317 
319 
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 }
332 
334 
338 bool OptionParser::completeOptionValue(std::string & possibleValue, const std::string & cursorOption, const std::string & prevCursorOption) const{
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 }
353 
355 
359 const Option * OptionParser::getLongOptionValue(std::string & valueToBeCompleted, const std::string & cursorOption) const{
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 }
380 
382 
387 const Option * OptionParser::getSplitOptionValue(std::string & valueToBeCompleted, const std::string & cursorOption, const std::string & prevCursorOption) const{
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 }
411 
413 
416 void OptionParser::getPossibleOption(std::string & possibleOption, const std::string & cursorOption) const{
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 }
430 
432 
435 void OptionParser::getPossibleOtherOption(std::string & possibleOption, const std::string & cursorOption) const{
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 }
453 
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::setExampleShortOption
void setExampleShortOption(const std::string &example)
Set the example usage of the program.
Definition: OptionParser.cpp:51
OptionParser::getMode
const OptionMode & getMode(const std::string &name) const
Get mode by name.
Definition: OptionParser.cpp:152
OptionParser::operator=
OptionParser & operator=(const OptionParser &other)
Definition of equal operator of OptionParser.
Definition: OptionParser.cpp:38
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
string_utils.h
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
OptionMode::setProgramVersion
void setProgramVersion(const std::string &programVersion)
Set the program version.
Definition: OptionMode.cpp:141
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::closeMode
void closeMode()
Close the current mode and go back to be default one.
Definition: OptionParser.cpp:65
OptionParser::~OptionParser
virtual ~OptionParser()
Destructeur of OptionParser.
Definition: OptionParser.cpp:30
OptionParser::isModeExist
bool isModeExist(const std::string &name) const
Check if the given mode name does exist.
Definition: OptionParser.cpp:178
OptionParser::OptionParser
OptionParser(bool enableHelpOption=true, const std::string &programVersion="")
Default constructeur of OptionParser.
Definition: OptionParser.cpp:16
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
OptionParser::setExampleLongOption
void setExampleLongOption(const std::string &example)
Set the example usage of the program.
Definition: OptionParser.cpp:46
OptionParser
Parse the options passed to a program.
Definition: OptionParser.h:15
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::addOption
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.
Definition: OptionParser.cpp:76
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::addMode
void addMode(const std::string &modeName)
Add a mode in the option.
Definition: OptionParser.cpp:56
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
Option::setIsAllowEmpty
void setIsAllowEmpty(bool isAllowEmpty)
Say if the option can be empty or not.
Definition: Option.cpp:177
OptionValue
Describe the value of an option passed to a program.
Definition: OptionValue.h:20
OptionParser.h
OptionParser::getParserMode
OptionMode & getParserMode(ArgParser &parser)
Get a mode if it exist.
Definition: OptionParser.cpp:306
OptionMode::setEnableHelpOption
void setEnableHelpOption(bool b)
Set the attribtue which enables help option.
Definition: OptionMode.cpp:136
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
OptionParser::parseArgument
void parseArgument(int argc, char **argv)
Parse the arguments passed to the program.
Definition: OptionParser.cpp:125
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