PhoenixPresentation  2.0.0
Set of cmake function to automate presentation generation
Option.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 "phoenix_color.h"
8 #include "Option.h"
9 
12  :p_longName(""), p_shortName(""), p_value(""), p_isRequired(false), p_docString("")
13 {
15 }
16 
18 
22 Option::Option(const PString & longName, const PString & shortName, const PString & docString)
23  :p_longName(longName), p_shortName(shortName), p_value(""), p_isRequired(false), p_docString(docString)
24 {
26 }
27 
29 
34 Option::Option(const PString & longName, const PString & shortName, const OptionValue & value, const PString & docString)
35  :p_longName(longName), p_shortName(shortName), p_value(value), p_isRequired(false), p_docString(docString)
36 {
38 }
39 
41 
47 Option::Option(const PString & longName, const PString & shortName, const OptionValue & value, bool isRequired, const PString & docString)
48  :p_longName(longName), p_shortName(shortName), p_value(value), p_isRequired(isRequired), p_docString(docString)
49 {
51 }
52 
54 
59 Option::Option(const PString & longName, const PString & shortName, bool isRequired, const PString & docString)
60  :p_longName(longName), p_shortName(shortName), p_isRequired(isRequired), p_docString(docString)
61 {
63 }
64 
66 
68 Option::Option(const Option & other){
69  copyOption(other);
70 }
71 
74 
75 }
76 
78 
82  copyOption(other);
83  return *this;
84 }
85 
87 
91  if(parsePartOption(parser, "--", p_longName)){return true;}
92  else if(parsePartOption(parser, "-", p_shortName)){return true;}
93  return false;
94 }
95 
97 
99 void printVecString(const PVecString & vecValue){
100  if(vecValue.size() == 0lu){return;}
101  PVecString::const_iterator it(vecValue.begin());
102  std::cout << *it;
103  ++it;
104  while(it != vecValue.end()){
105  std::cout << ", " << *it;
106  ++it;
107  }
108 }
109 
111 
113 void Option::print(const PString & indentation) const{
115  std::cout << indentation;
116  if(p_shortName != ""){
117  std::cout << "-" << p_shortName;
118  if(type != OptionType::NONE){
119  std::cout << " " << convertOptionTypeToString(type);
120  }
121  }
122  if(p_longName != ""){
123  if(p_shortName != ""){std::cout << " , ";}
124  std::cout << "--" << p_longName;
125  if(type != OptionType::NONE){
126  std::cout << "=" << convertOptionTypeToString(type);
127  }
128  }
129  if(p_docString != ""){
130  std::cout << " : " << p_docString;
131  }
132  std::cout << std::endl;
133  const PVecString & vecDefaultValue = p_value.getDefaultValue();
134  if(vecDefaultValue.size()){
135  std::cout << indentation << "\tDefault value : '";
136  printVecString(vecDefaultValue);
137  std::cout << "'" << std::endl;
138  }
139  const PVecString & vecPossibleValue = p_value.getPossibleValue();
140  if(vecPossibleValue.size()){
141  std::cout << indentation << "\tPossible values : ";
142  printVecString(vecPossibleValue);
143  std::cout << std::endl;
144  }
145  if(p_isRequired){std::cout << indentation << "\tThis argument has to be set" << std::endl;}
146  else{std::cout << indentation << "\tThis argument is optional" << std::endl;}
147  if(p_isAllowEmpty){std::cout << indentation << "\tThis argument can have an empty value" << std::endl;}
148  else{std::cout << indentation << "\tThis argument cannot have an empty value" << std::endl;}
149 }
150 
152 
154 void Option::setLongName(const PString & longName){p_longName = longName;}
155 
157 
159 void Option::setShortName(const PString & shortName){p_shortName = shortName;}
160 
162 
164 void Option::setValue(const OptionValue & value){p_value = value;}
165 
167 
170 
172 
174 void Option::setDocString(const PString & docString){p_docString = docString;}
175 
177 
179 void Option::setIsParsed(bool isParsed){p_isParsed = isParsed;}
180 
182 
185 
187 
189 const PString & Option::getLongName() const{return p_longName;}
190 
192 
195 
197 
199 const PString & Option::getShortName() const{return p_shortName;}
200 
202 
205 
207 
209 const OptionValue & Option::getValue() const{return p_value;}
210 
212 
215 
217 
219 bool Option::isRequired() const{return p_isRequired;}
220 
222 
225 
227 
230 
232 
235 
237 
239 const PString & Option::getDocString() const{return p_docString;}
240 
242 
245 
247 
249 bool Option::isParsed() const{return p_isParsed;}
250 
252 
254 bool & Option::isParsed(){return p_isParsed;}
255 
257 
260  if(p_isRequired){
261  if(!p_isParsed){
263  std::cerr << termRed() << "Missing arguement ";
264  if(p_longName != ""){
265  std::cerr << "--" << p_longName;
266  if(type != OptionType::NONE){
267  std::cout << "=" << convertOptionTypeToString(type);
268  }
269  }
270  if(p_shortName != ""){
271  if(p_longName != ""){std::cerr << " , ";}
272  std::cerr << "-" << p_shortName;
273  if(type != OptionType::NONE){
274  std::cout << " " << convertOptionTypeToString(type);
275  }
276  }
277  std::cerr << termDefault() << std::endl;
278  }
279  return p_isParsed;
280  }else{return true;}
281 }
282 
284 
287 void Option::getPossibleOption(PString & possibleOption, const PString & cursorOption) const{
288  if(p_isParsed){return;}
289  if(p_longName != ""){
290  PString longOption("--" + p_longName);
291  if(cursorOption == ""){
292  possibleOption += longOption + " ";
293  }else{
294  if(longOption.isSameBegining(cursorOption)){
295  possibleOption += longOption + " ";
296  }
297  }
298  }
299  if(p_shortName != ""){
300  PString shortOption("-" + p_shortName);
301  if(cursorOption == ""){
302  possibleOption += shortOption + " ";
303  }else{
304  if(shortOption.isSameBegining(cursorOption)){
305  possibleOption += shortOption + " ";
306  }
307  }
308  }
309 }
310 
312 
315 void Option::getPossibleValue(PString & possibleValue, const PString & cursorOption) const{
316  p_value.bashCompletionValue(possibleValue, cursorOption);
317 }
318 
320 
322 void Option::copyOption(const Option & other){
323  p_longName = other.p_longName;
324  p_shortName = other.p_shortName;
325  p_value = other.p_value;
326  p_isRequired = other.p_isRequired;
327  p_docString = other.p_docString;
328  p_isParsed = other.p_isParsed;
331 }
332 
335  p_isParsed = false;
337  p_isAllowEmpty = false;
338 }
339 
341 
346 bool Option::parsePartOption(ArgParser & parser, const PString & prefix, const PString & optionName){
347  if(parser.isEndOfOption()){return true;}
348  if(optionName == ""){return false;}
349  PString & currentOption = parser.getCurrentOption();
350  PString longOption(prefix + optionName);
351  size_t sizeLongOption(longOption.size());
352  OptionType::OptionType optionType = p_value.getType();
353  if(currentOption == longOption){
354  checkAlreadyParsed(longOption);
355  p_isParsed = true;
356  if(optionType == OptionType::NONE){ //No value expected
357  parser.getNextOption();
358  return true; //Option found
359  }
360  bool valueOk(true), isInitialised(false);
361  parser.getNextOption();
362  while(!parser.isEndOfOption() && valueOk){
363  PString & currentOption = parser.getCurrentOption();
364  if(currentOption == ""){
365  if(optionType == OptionType::STRING){
366  p_value.addValue("");
367  parser.getNextOption();
368  valueOk = true;
369  isInitialised = true;
370  }else{
371  throw std::runtime_error("Option::parsePartOption : pass empty value to option '" + longOption + "' which does not expect STRING");
372  }
373  }else{
374  if(currentOption[0] == '-'){
375  valueOk = false;
376  }else{
377  p_value.addValue(currentOption);
378  parser.getNextOption();
379  valueOk = true;
380  isInitialised = true;
381  }
382  }
383  }
384  if(!isInitialised && !p_isAllowEmpty){
385  throw std::runtime_error("Option::parsePartOption : expect value after option '" + longOption + "'");
386  }
387  return isInitialised || p_isAllowEmpty;
388  }else if(currentOption.isSameBegining(longOption)){
389  if(currentOption[sizeLongOption] == '='){
390  if(optionType == OptionType::NONE){ //No value expected
391  throw std::runtime_error("Option::parsePartOption : the option '"+currentOption+"' does not have value");
392  }
393  if(currentOption.size() == sizeLongOption + 1lu){
394  p_firstPartParsedOption = longOption + "=";
395  throw std::runtime_error("Option::parsePartOption : problem with the option '"+currentOption+"' because it ends with a '=' and not a value");
396  //Ici il faut un mode qui renvoie quand même les valeurs possibles quand on a --option=...
397  }
398  checkAlreadyParsed(longOption);
399  PString value(currentOption.substr(sizeLongOption + 1lu));
400  p_value.addValue(value);
401  p_isParsed = true;
402  parser.getNextOption();
403  return true;
404  }else{
405  return false; //It is an option with a longer name
406  }
407  }
408  return false;
409 }
410 
412 
414 void Option::checkAlreadyParsed(const PString & longOption){
415  if(p_isParsed){ //The option has already been parsed, there is a mistake
416  throw std::runtime_error("Option::checkAlreadyParsed : option '" + longOption + "' already exists");
417  }
418 }
419 
420 
421 
422 
PString convertOptionTypeToString(OptionType::OptionType type)
Convert the OptionType into PString.
Definition: OptionType.cpp:69
void printVecString(const PVecString &vecValue)
Print a vector of value.
Definition: Option.cpp:99
std::vector< PString > PVecString
Definition: PString.h:96
Parse the list of arguments passed to a program.
Definition: ArgParser.h:13
const PString & getCurrentOption() const
Get the current option.
Definition: ArgParser.cpp:91
void getNextOption()
Move to the next option.
Definition: ArgParser.cpp:77
bool isEndOfOption() const
Say if is it the end of the options.
Definition: ArgParser.cpp:84
Describe the value of an option passed to a program.
Definition: OptionValue.h:16
void bashCompletionValue(PString &strBashCompletion, const PString &cursorOption) const
Print the possible value to the bash completion.
void addValue(const PString &value)
Add value of the OptionValue.
OptionType::OptionType getType() const
Get the type of the OptionValue.
const PVecString & getDefaultValue() const
Get the default value of the OptionValue.
const PVecString & getPossibleValue() const
Get the possible values of the OptionValue.
Describes an option passed to a program.
Definition: Option.h:14
virtual ~Option()
Destructeur of Option.
Definition: Option.cpp:73
void setIsParsed(bool isParsed)
Say if the Option has been parsed or not.
Definition: Option.cpp:179
void setIsAllowEmpty(bool isAllowEmpty)
Say if the option can be empty or not.
Definition: Option.cpp:184
bool p_isAllowEmpty
The option can be empty and can have a value.
Definition: Option.h:86
const PString & getLongName() const
Get the long name of the Option.
Definition: Option.cpp:189
void checkAlreadyParsed(const PString &longOption)
Check if the Option has been already parsed.
Definition: Option.cpp:414
bool parsePartOption(ArgParser &parser, const PString &prefix, const PString &optionName)
Parse the given option with the parser.
Definition: Option.cpp:346
void getPossibleOption(PString &possibleOption, const PString &cursorOption) const
Get the possible options for the bash completion.
Definition: Option.cpp:287
void initialisationOption()
Initialisation function of the class Option.
Definition: Option.cpp:334
void setLongName(const PString &longName)
Set the long name of the option.
Definition: Option.cpp:154
bool parseOption(ArgParser &parser)
Parse the current option with the given parser.
Definition: Option.cpp:90
PString p_shortName
Short name of the Option.
Definition: Option.h:74
const OptionValue & getValue() const
Get the value of the Option.
Definition: Option.cpp:209
bool isParsed() const
Say if the Option has been parsed or not.
Definition: Option.cpp:249
void copyOption(const Option &other)
Copy function of Option.
Definition: Option.cpp:322
bool p_isRequired
True if the option is required, false if it is optionnal.
Definition: Option.h:78
OptionValue p_value
Value of the Option.
Definition: Option.h:76
const PString & getDocString() const
Get the documentation string of the Option.
Definition: Option.cpp:239
void setShortName(const PString &shortName)
Set the short name of the option.
Definition: Option.cpp:159
bool checkArgument() const
Check the argument of the parser.
Definition: Option.cpp:259
bool p_isParsed
Say if the option has been parsed or not.
Definition: Option.h:82
PString p_firstPartParsedOption
First paet of parsed option (needed for bash completion)
Definition: Option.h:84
void setIsRequired(bool isRequired)
Set if the option is required.
Definition: Option.cpp:169
PString p_longName
Long name of the Option.
Definition: Option.h:72
const PString & getShortName() const
Get the short name of the Option.
Definition: Option.cpp:199
void setValue(const OptionValue &value)
Set the value of the option.
Definition: Option.cpp:164
bool isAllowEmpty() const
Get if the option value can be empty.
Definition: Option.cpp:229
Option & operator=(const Option &other)
Definition of equal operator of Option.
Definition: Option.cpp:81
void getPossibleValue(PString &possibleValue, const PString &cursorOption) const
Complete the possible values of the Option.
Definition: Option.cpp:315
PString p_docString
Documentation string of the current Option.
Definition: Option.h:80
void print(const PString &indentation="") const
Print an option.
Definition: Option.cpp:113
void setDocString(const PString &docString)
Set the documentation string of the Option.
Definition: Option.cpp:174
Option()
Default constructor of Option.
Definition: Option.cpp:11
bool isRequired() const
Get if the option is required.
Definition: Option.cpp:219
Extends the std::string.
Definition: PString.h:16
bool isSameBegining(const PString &beginStr) const
Say if the current PString has the same begining of beginStr.
Definition: PString.cpp:306
std::string termDefault()
affiche le terminal par défaut
std::string termRed()
affiche le terminal rouge