PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
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 "string_utils.h"
8 #include "Option.h"
9 
11 
15 Option::Option(const std::string & longName, const std::string & shortName, const std::string & docString)
16  :p_longName(longName), p_shortName(shortName), p_value(""), p_isRequired(false), p_docString(docString)
17 {
19 }
20 
22 
27 Option::Option(const std::string & longName, const std::string & shortName, const OptionValue & value, const std::string & docString)
28  :p_longName(longName), p_shortName(shortName), p_value(value), p_isRequired(false), p_docString(docString)
29 {
31 }
32 
34 
40 Option::Option(const std::string & longName, const std::string & shortName, const OptionValue & value, bool isRequired, const std::string & docString)
41  :p_longName(longName), p_shortName(shortName), p_value(value), p_isRequired(isRequired), p_docString(docString)
42 {
44 }
45 
47 
52 Option::Option(const std::string & longName, const std::string & shortName, bool isRequired, const std::string & docString)
53  :p_longName(longName), p_shortName(shortName), p_isRequired(isRequired), p_docString(docString)
54 {
56 }
57 
59 
61 Option::Option(const Option & other){
62  copyOption(other);
63 }
64 
67 
68 }
69 
71 
75  copyOption(other);
76  return *this;
77 }
78 
80 
84  if(parsePartOption(parser, "--", p_longName)){return true;}
85  else if(parsePartOption(parser, "-", p_shortName)){return true;}
86  return false;
87 }
88 
90 
92 void printVecString(const VecValue & vecValue){
93  if(vecValue.size() == 0lu){return;}
94  VecValue::const_iterator it(vecValue.begin());
95  std::cout << *it;
96  ++it;
97  while(it != vecValue.end()){
98  std::cout << ", " << *it;
99  ++it;
100  }
101 }
102 
104 
106 void Option::print(const std::string & indentation) const{
108  std::cout << indentation;
109  if(p_longName != ""){
110  std::cout << "--" << p_longName;
111  if(type != OptionType::NONE){
112  std::cout << "=" << convertOptionTypeToString(type);
113  }
114  }
115  if(p_shortName != ""){
116  if(p_longName != ""){std::cout << " , ";}
117  std::cout << "-" << p_shortName;
118  if(type != OptionType::NONE){
119  std::cout << " " << convertOptionTypeToString(type);
120  }
121  }
122  if(p_docString != ""){
123  std::cout << " : " << p_docString;
124  }
125  std::cout << std::endl;
126  const VecValue & vecDefaultValue = p_value.getDefaultValue();
127  if(vecDefaultValue.size()){
128  std::cout << indentation << "\tDefault value : '";
129  printVecString(vecDefaultValue);
130  std::cout << "'" << std::endl;
131  }
132  const VecValue & vecPossibleValue = p_value.getPossibleValue();
133  if(vecPossibleValue.size()){
134  std::cout << indentation << "\tPossible values : ";
135  printVecString(vecPossibleValue);
136  std::cout << std::endl;
137  }
138  if(p_isRequired){std::cout << indentation << "\tThis argument has to be set" << std::endl;}
139  else{std::cout << indentation << "\tThis argument is optional" << std::endl;}
140  if(p_isAllowEmpty){std::cout << indentation << "\tThis argument can have an empty value" << std::endl;}
141  else{std::cout << indentation << "\tThis argument cannot have an empty value" << std::endl;}
142 }
143 
145 
147 void Option::setLongName(const std::string & longName){p_longName = longName;}
148 
150 
152 void Option::setShortName(const std::string & shortName){p_shortName = shortName;}
153 
155 
157 void Option::setValue(const OptionValue & value){p_value = value;}
158 
160 
163 
165 
167 void Option::setDocString(const std::string & docString){p_docString = docString;}
168 
170 
172 void Option::setIsParsed(bool isParsed){p_isParsed = isParsed;}
173 
175 
178 
180 
182 const std::string & Option::getLongName() const{return p_longName;}
183 
185 
187 std::string & Option::getLongName(){return p_longName;}
188 
190 
192 const std::string & Option::getShortName() const{return p_shortName;}
193 
195 
197 std::string & Option::getShortName(){return p_shortName;}
198 
200 
202 const OptionValue & Option::getValue() const{return p_value;}
203 
205 
208 
210 
212 bool Option::isRequired() const{return p_isRequired;}
213 
215 
217 bool & Option::isRequired(){return p_isRequired;}
218 
220 
223 
225 
227 bool & Option::isAllowEmpty(){return p_isAllowEmpty;}
228 
230 
232 const std::string & Option::getDocString() const{return p_docString;}
233 
235 
237 std::string & Option::getDocString(){return p_docString;}
238 
240 
242 bool Option::isParsed() const{return p_isParsed;}
243 
245 
247 bool & Option::isParsed(){return p_isParsed;}
248 
250 
253  if(p_isRequired){
254  if(!p_isParsed){
256  std::cerr << termRed() << "Missing arguement ";
257  if(p_longName != ""){
258  std::cerr << "--" << p_longName;
259  if(type != OptionType::NONE){
260  std::cout << "=" << convertOptionTypeToString(type);
261  }
262  }
263  if(p_shortName != ""){
264  if(p_longName != ""){std::cerr << " , ";}
265  std::cerr << "-" << p_shortName;
266  if(type != OptionType::NONE){
267  std::cout << " " << convertOptionTypeToString(type);
268  }
269  }
270  std::cerr << termDefault() << std::endl;
271  }
272  return p_isParsed;
273  }else{return true;}
274 }
275 
277 
280 void Option::getPossibleOption(std::string & possibleOption, const std::string & cursorOption) const{
281  if(p_isParsed){return;}
282  if(p_longName != ""){
283  std::string longOption("--" + p_longName);
284  if(cursorOption == ""){
285  possibleOption += longOption + " ";
286  }else{
287  if(isSameBegining(longOption, cursorOption)){
288  possibleOption += longOption + " ";
289  }
290  }
291  }
292  if(p_shortName != ""){
293  std::string shortOption("-" + p_shortName);
294  if(cursorOption == ""){
295  possibleOption += shortOption + " ";
296  }else{
297  if(isSameBegining(shortOption, cursorOption)){
298  possibleOption += shortOption + " ";
299  }
300  }
301  }
302 }
303 
305 
308 void Option::getPossibleValue(std::string & possibleValue, const std::string & cursorOption) const{
309  p_value.bashCompletionValue(possibleValue, cursorOption);
310 }
311 
313 
315 void Option::copyOption(const Option & other){
316  p_longName = other.p_longName;
317  p_shortName = other.p_shortName;
318  p_value = other.p_value;
319  p_isRequired = other.p_isRequired;
320  p_docString = other.p_docString;
321  p_isParsed = other.p_isParsed;
324 }
325 
328  p_isParsed = false;
329  p_isRequired = false;
331  p_isAllowEmpty = false;
332 }
333 
335 
340 bool Option::parsePartOption(ArgParser & parser, const std::string & prefix, const std::string & optionName){
341  if(parser.isEndOfOption()){return true;}
342  if(optionName == ""){return false;}
343  std::string & currentOption = parser.getCurrentOption();
344  std::string longOption(prefix + optionName);
345  size_t sizeLongOption(longOption.size());
346  OptionType::OptionType optionType = p_value.getType();
347  if(currentOption == longOption){
348  checkAlreadyParsed(longOption);
349  p_isParsed = true;
350  if(optionType == OptionType::NONE){ //No value expected
351  parser.getNextOption();
352  return true; //Option found
353  }
354  bool valueOk(true), isInitialised(false);
355  parser.getNextOption();
356  while(!parser.isEndOfOption() && valueOk){
357  std::string & currentOption = parser.getCurrentOption();
358  if(currentOption == ""){
359  if(optionType == OptionType::STRING){
360  p_value.addValue("");
361  parser.getNextOption();
362  valueOk = true;
363  isInitialised = true;
364  }else{
365  throw std::runtime_error("Option::parsePartOption : pass empty value to option '" + longOption + "' which does not expect STRING");
366  }
367  }else{
368  if(currentOption[0] == '-'){
369  valueOk = false;
370  }else{
371  p_value.addValue(currentOption);
372  parser.getNextOption();
373  valueOk = true;
374  isInitialised = true;
375  }
376  }
377  }
378  if(!isInitialised && !p_isAllowEmpty){
379  throw std::runtime_error("Option::parsePartOption : expect value after option '" + longOption + "'");
380  }
381  return isInitialised || p_isAllowEmpty;
382  }else if(isSameBegining(currentOption, longOption)){
383  if(currentOption[sizeLongOption] == '='){
384  if(optionType == OptionType::NONE){ //No value expected
385  throw std::runtime_error("Option::parsePartOption : the option '"+currentOption+"' does not have value");
386  }
387  if(currentOption.size() == sizeLongOption + 1lu){
388  p_firstPartParsedOption = longOption + "=";
389  throw std::runtime_error("Option::parsePartOption : problem with the option '"+currentOption+"' because it ends with a '=' and not a value");
390  //Ici il faut un mode qui renvoie quand même les valeurs possibles quand on a --option=...
391  }
392  checkAlreadyParsed(longOption);
393  std::string value(currentOption.substr(sizeLongOption + 1lu));
394  p_value.addValue(value);
395  p_isParsed = true;
396  parser.getNextOption();
397  return true;
398  }else{
399  return false; //It is an option with a longer name
400  }
401  }
402  return false;
403 }
404 
406 
408 void Option::checkAlreadyParsed(const std::string & longOption){
409  if(p_isParsed){ //The option has already been parsed, there is a mistake
410  throw std::runtime_error("Option::checkAlreadyParsed : option '" + longOption + "' already exists");
411  }
412 }
413 
414 
415 
416 
Option.h
Option::p_value
OptionValue p_value
Value of the Option.
Definition: Option.h:78
Option::isRequired
bool isRequired() const
Get if the option is required.
Definition: Option.cpp:212
Option::setIsParsed
void setIsParsed(bool isParsed)
Say if the Option has been parsed or not.
Definition: Option.cpp:172
convertOptionTypeToString
std::string convertOptionTypeToString(OptionType::OptionType type)
Convert the OptionType into string.
Definition: OptionType.cpp:69
Option::p_shortName
std::string p_shortName
Short name of the Option.
Definition: Option.h:76
printVecString
void printVecString(const VecValue &vecValue)
Print a vector of value.
Definition: Option.cpp:92
OptionType::STRING
@ STRING
Definition: OptionType.h:25
string_utils.h
Option::setDocString
void setDocString(const std::string &docString)
Set the documentation string of the Option.
Definition: Option.cpp:167
Option::p_longName
std::string p_longName
Long name of the Option.
Definition: Option.h:74
Option::isParsed
bool isParsed() const
Say if the Option has been parsed or not.
Definition: Option.cpp:242
Option::p_isRequired
bool p_isRequired
True if the option is required, false if it is optionnal.
Definition: Option.h:80
Option::checkAlreadyParsed
void checkAlreadyParsed(const std::string &longOption)
Check if the Option has been already parsed.
Definition: Option.cpp:408
Option::checkArgument
bool checkArgument() const
Check the argument of the parser.
Definition: Option.cpp:252
Option::getValue
const OptionValue & getValue() const
Get the value of the Option.
Definition: Option.cpp:202
Option::p_isAllowEmpty
bool p_isAllowEmpty
The option can be empty and can have a value.
Definition: Option.h:88
Option::isAllowEmpty
bool isAllowEmpty() const
Get if the option value can be empty.
Definition: Option.cpp:222
Option::print
void print(const std::string &indentation="") const
Print an option.
Definition: Option.cpp:106
Option::setLongName
void setLongName(const std::string &longName)
Set the long name of the option.
Definition: Option.cpp:147
Option::p_firstPartParsedOption
std::string p_firstPartParsedOption
First paet of parsed option (needed for bash completion)
Definition: Option.h:86
OptionValue::getType
OptionType::OptionType getType() const
Get the type of the OptionValue.
Definition: OptionValue.cpp:141
OptionValue::getPossibleValue
const VecValue & getPossibleValue() const
Get the possible values of the OptionValue.
Definition: OptionValue.cpp:161
Option::operator=
Option & operator=(const Option &other)
Definition of equal operator of Option.
Definition: Option.cpp:74
Option::setValue
void setValue(const OptionValue &value)
Set the value of the option.
Definition: Option.cpp:157
Option::copyOption
void copyOption(const Option &other)
Copy function of Option.
Definition: Option.cpp:315
Option::~Option
virtual ~Option()
Destructeur of Option.
Definition: Option.cpp:66
termDefault
std::string termDefault()
affiche le terminal par défaut
Definition: string_color.cpp:20
Option::getShortName
const std::string & getShortName() const
Get the short name of the Option.
Definition: Option.cpp:192
termRed
std::string termRed()
affiche le terminal rouge
Definition: string_color.cpp:55
Option::getPossibleValue
void getPossibleValue(std::string &possibleValue, const std::string &cursorOption) const
Complete the possible values of the Option.
Definition: Option.cpp:308
OptionValue::bashCompletionValue
void bashCompletionValue(std::string &strBashCompletion, const std::string &cursorOption) const
Print the possible value to the bash completion.
Definition: OptionValue.cpp:172
isSameBegining
bool isSameBegining(const std::string &str, const std::string &beginig)
Check if two string start the same way.
Definition: string_function.cpp:531
OptionValue::addValue
void addValue(const std::string &value)
Add value of the OptionValue.
Definition: OptionValue.cpp:126
Option::parseOption
bool parseOption(ArgParser &parser)
Parse the current option with the given parser.
Definition: Option.cpp:83
createReleaseCurl.parser
parser
Definition: createReleaseCurl.py:123
Option::p_isParsed
bool p_isParsed
Say if the option has been parsed or not.
Definition: Option.h:84
Option::p_docString
std::string p_docString
Documentation string of the current Option.
Definition: Option.h:82
Option::setShortName
void setShortName(const std::string &shortName)
Set the short name of the option.
Definition: Option.cpp:152
createReleaseCurl.type
type
Definition: createReleaseCurl.py:124
Option::getLongName
const std::string & getLongName() const
Get the long name of the Option.
Definition: Option.cpp:182
Option
Definition: Option.h:13
Option::Option
Option(const std::string &longName="", const std::string &shortName="", const std::string &docString="")
Default constructor of Option.
Definition: Option.cpp:15
OptionType::OptionType
OptionType
Definition: OptionType.h:19
Option::parsePartOption
bool parsePartOption(ArgParser &parser, const std::string &prefix, const std::string &optionName)
Parse the given option with the parser.
Definition: Option.cpp:340
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
OptionType::NONE
@ NONE
Definition: OptionType.h:24
Option::initialisationOption
void initialisationOption()
Initialisation function of the class Option.
Definition: Option.cpp:327
OptionValue::getDefaultValue
const VecValue & getDefaultValue() const
Get the default value of the OptionValue.
Definition: OptionValue.cpp:151
VecValue
std::vector< std::string > VecValue
Vector of values.
Definition: OptionValue.h:17
Option::setIsRequired
void setIsRequired(bool isRequired)
Set if the option is required.
Definition: Option.cpp:162
Option::getPossibleOption
void getPossibleOption(std::string &possibleOption, const std::string &cursorOption) const
Get the possible options for the bash completion.
Definition: Option.cpp:280
Option::getDocString
const std::string & getDocString() const
Get the documentation string of the Option.
Definition: Option.cpp:232
ArgParser
Parse the list of arguments passed to a program.
Definition: ArgParser.h:16