PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
OptionMode.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 "OptionMode.h"
9 
10 using namespace std;
11 
13 
15 OptionMode::OptionMode(const std::string & name)
16  :p_name(name)
17 {
19 }
20 
22 
25  copyOptionMode(other);
26 }
27 
30 
31 }
32 
34 
38  copyOptionMode(other);
39  return *this;
40 }
41 
43 
48  if(parser.isEndOfOption()){return true;}
49 
50  p_isParsed = true;
51  p_isCurrentlyParsed = true;
52  bool isSearch(true);
53  VecOption::iterator it(p_vecOption.begin());
54  while(isSearch && it != p_vecOption.end() && !parser.isEndOfOption()){
56  if(parser.getCurrentOption() == "--help" || parser.getCurrentOption() == "-h"){
57  print();
58  exit(0);
59  }
60  }
61  if(p_programVersion != ""){
62  if(parser.getCurrentOption() == "--version" || parser.getCurrentOption() == "-v"){
63  std::cout << "Program version : " << p_programVersion << std::endl;
64  exit(0);
65  }
66  }
67  isSearch = !it->parseOption(parser);
68  ++it;
69  }
70  p_isParsed |= !isSearch;
71  return !isSearch;
72 }
73 
75 
79 bool OptionMode::parseOption(ArgParser & parser, Option *& partialOption){
80  if(parser.isEndOfOption()){return true;}
81  if(p_name != ""){ //If the mode has no name, it is the default mode
82  if(p_name != parser.getCurrentOption()){return false;}
83  p_isParsed = true;
84  parser.getNextOption();
85  }
86  p_isCurrentlyParsed = true;
87  partialOption = NULL;
88  bool isSearch(true);
89  VecOption::iterator it(p_vecOption.begin());
90  while(isSearch && it != p_vecOption.end() && !parser.isEndOfOption()){
91  try{
92  isSearch = !it->parseOption(parser);
93  }catch(const std::runtime_error & e){
94  partialOption = &(*it);
95  isSearch = false;
96  }
97  ++it;
98  }
99  p_isParsed |= !isSearch;
100  return !isSearch;
101 }
102 
104 void OptionMode::print() const{
105  std::string indentation("\t");
106  if(p_name != ""){
107  cout << "\tMode '" << p_name << "' :" << endl;
108  indentation += "\t";
109  }
110  for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){
111  it->print(indentation);
112  }
113 }
114 
116 
118 void OptionMode::setName(const std::string & name){p_name = name;}
119 
121 
123 void OptionMode::setVecOption(const VecOption & vecOption){p_vecOption = vecOption;}
124 
126 
128 void OptionMode::addOption(const Option & option){p_vecOption.push_back(option);}
129 
132 
134 
137 
139 
141 void OptionMode::setProgramVersion(const std::string & programVersion){p_programVersion = programVersion;}
142 
144 
146 const std::string & OptionMode::getName() const{return p_name;}
147 
149 
151 std::string & OptionMode::getName(){return p_name;}
152 
154 
157 
159 
162 
164 
167  if(!p_isParsed){return true;}
168  bool isArgOk(true);
169  VecOption::const_iterator it(p_vecOption.begin());
170  while(it != p_vecOption.end() && isArgOk){
171  isArgOk = it->checkArgument();
172  ++it;
173  }
174  return isArgOk;
175 }
176 
178 
181  return p_isCurrentlyParsed;
182 }
183 
185 
187 bool OptionMode::isParsed() const{
188  return p_isParsed;
189 }
190 
192 
195 bool OptionMode::isOptionExist(const std::string & optionName) const{
196  VecOption::const_iterator it(p_vecOption.begin());
197  while(it != p_vecOption.end()){
198  if(it->getLongName() == optionName || it->getShortName() == optionName){
199  return it->isParsed();
200  }
201  ++it;
202  }
203  return false;
204 }
205 
207 
210 void OptionMode::getPossibleOption(std::string & possibleOption, const std::string & cursorOption) const{
211  if(p_name != ""){
212  if(!p_isCurrentlyParsed){
213  if(isSameBegining(p_name, cursorOption)){
214  possibleOption += p_name + " ";
215  }
216  }
217  }
218  iterGetPossibleOption(possibleOption, cursorOption);
219 }
220 
222 
225 void OptionMode::getPossibleMode(std::string & possibleOption, const std::string & cursorOption) const{
226  if(p_name != ""){
227  if(isSameBegining(p_name, cursorOption)){
228  possibleOption += p_name + " ";
229  }
230  }
231 }
232 
234 
237  p_name = other.p_name;
238  p_vecOption = other.p_vecOption;
240  p_isParsed = other.p_isParsed;
243 }
244 
247  p_isCurrentlyParsed = false;
248  p_isParsed = false;
249  p_enableHelpOption = false;
250 }
251 
253 
257 bool OptionMode::getOption(Option & option, const std::string & optionName) const{
258  VecOption::const_iterator it(p_vecOption.begin());
259  while(it != p_vecOption.end()){
260  if(it->getLongName() == optionName || it->getShortName() == optionName){
261  option = *it;
262  return true;
263  }
264  ++it;
265  }
266  return false;
267 }
268 
270 
273 void OptionMode::iterGetPossibleOption(std::string & possibleOption, const std::string & cursorOption) const{
274  for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){
275  it->getPossibleOption(possibleOption, cursorOption);
276  }
277 }
278 
279 
OptionMode::p_isCurrentlyParsed
bool p_isCurrentlyParsed
True if the OptionMode is currently parsed but not totally.
Definition: OptionMode.h:67
OptionMode.h
OptionMode::getVecOption
const VecOption & getVecOption() const
Get the vector of options of the OptionMode.
Definition: OptionMode.cpp:156
OptionMode::checkArgument
bool checkArgument() const
Check the argument of the parser.
Definition: OptionMode.cpp:166
OptionMode::iterGetPossibleOption
void iterGetPossibleOption(std::string &possibleOption, const std::string &cursorOption) const
Iterates over the possible options of the current mode.
Definition: OptionMode.cpp:273
OptionMode::p_name
std::string p_name
name of the OptionMode
Definition: OptionMode.h:63
string_utils.h
OptionMode::~OptionMode
virtual ~OptionMode()
Destructeur of OptionMode.
Definition: OptionMode.cpp:29
OptionMode::p_isParsed
bool p_isParsed
Say if the mode contains options which are parsed.
Definition: OptionMode.h:69
VecOption
std::vector< Option > VecOption
Vector of option.
Definition: Option.h:88
OptionMode::p_vecOption
VecOption p_vecOption
Vector of all the options of the OptionMode.
Definition: OptionMode.h:65
OptionMode::getName
const std::string & getName() const
Get the name of the OptionMode.
Definition: OptionMode.cpp:146
OptionMode::setProgramVersion
void setProgramVersion(const std::string &programVersion)
Set the program version.
Definition: OptionMode.cpp:141
OptionMode::setVecOption
void setVecOption(const VecOption &vecOption)
Set the vector of options of the OptionMode.
Definition: OptionMode.cpp:123
OptionMode::addOption
void addOption(const Option &option)
Add an option into the OptionMode.
Definition: OptionMode.cpp:128
OptionMode::OptionMode
OptionMode(const std::string &name="")
Default constructeur of OptionMode.
Definition: OptionMode.cpp:15
OptionMode::parseOption
bool parseOption(ArgParser &parser)
Parse the options in the current OptionMode.
Definition: OptionMode.cpp:47
OptionMode::getOption
bool getOption(Option &option, const std::string &optionName) const
Get the option with its name.
Definition: OptionMode.cpp:257
OptionMode::print
void print() const
Print the option of the mode.
Definition: OptionMode.cpp:104
OptionMode::setName
void setName(const std::string &name)
Set the name of the OptionMode.
Definition: OptionMode.cpp:118
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
createReleaseCurl.parser
parser
Definition: createReleaseCurl.py:123
OptionMode::isCurrentlyParsed
bool isCurrentlyParsed() const
Say if the OptionMode is currently parsed (but maybe not totally)
Definition: OptionMode.cpp:180
OptionMode::isParsed
bool isParsed() const
Say if the OptionMode contains option which are parsed.
Definition: OptionMode.cpp:187
OptionMode::initialisationOptionMode
void initialisationOptionMode()
Initialisation function of the class OptionMode.
Definition: OptionMode.cpp:246
Option
Definition: Option.h:13
OptionMode::p_programVersion
std::string p_programVersion
Program version to be printed on –version or -v option.
Definition: OptionMode.h:73
OptionMode::p_enableHelpOption
bool p_enableHelpOption
True to enable automatically the printing of the help option when the program is called with –help or...
Definition: OptionMode.h:71
OptionMode::setEnableHelpOption
void setEnableHelpOption(bool b)
Set the attribtue which enables help option.
Definition: OptionMode.cpp:136
OptionMode::clearOption
void clearOption()
Remove all the options of the OptionMode.
Definition: OptionMode.cpp:131
OptionMode::isOptionExist
bool isOptionExist(const std::string &optionName) const
Say if the given option has been passed to the program.
Definition: OptionMode.cpp:195
ArgParser
Parse the list of arguments passed to a program.
Definition: ArgParser.h:16
OptionMode::operator=
OptionMode & operator=(const OptionMode &other)
Definition of equal operator of OptionMode.
Definition: OptionMode.cpp:37
OptionMode::copyOptionMode
void copyOptionMode(const OptionMode &other)
Copy function of OptionMode.
Definition: OptionMode.cpp:236
OptionMode
Describe a mode in the program arguments.
Definition: OptionMode.h:13
OptionMode::getPossibleMode
void getPossibleMode(std::string &possibleOption, const std::string &cursorOption) const
Get the possible mode.
Definition: OptionMode.cpp:225