PhoenixInkscape  2.0.0
Generate multiple png files with svg inkscape files
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 "OptionMode.h"
8 
10 
13  :p_name(name)
14 {
16 }
17 
19 
22  copyOptionMode(other);
23 }
24 
27 
28 }
29 
31 
35  copyOptionMode(other);
36  return *this;
37 }
38 
40 
45  p_isParsed = true;
46  if(parser.isEndOfOption()){return true;}
47 
48  if(p_vecOption.size() == 0lu){ //If the OptionMode does not have Option we get the rest of passed extra arguments, they could be used later
50  return true;
51  }
52 
53  p_isCurrentlyParsed = true;
54  bool isSearch(true);
55  VecOption::iterator it(p_vecOption.begin());
56  while(isSearch && it != p_vecOption.end() && !parser.isEndOfOption()){
57  if(parser.getCurrentOption() == "--"){ //If we get a --, then the other arguments do not concern us
58  parser.getNextOption(); //Let's get the ext option, because we do not care about --
59  //Let's put them in the p_extraArgument
61  }
63  if(parser.getCurrentOption() == "--help" || parser.getCurrentOption() == "-h"){
64  print();
65  exit(0);
66  }
67  }
68  if(p_programVersion != ""){
69  if(parser.getCurrentOption() == "--version" || parser.getCurrentOption() == "-v"){
70  std::cout << "Program version : " << p_programVersion << std::endl;
71  exit(0);
72  }
73  }
74 
75  isSearch = !it->parseOption(parser);
76  ++it;
77  }
78  p_isParsed |= !isSearch;
79  return !isSearch;
80 }
81 
83 
87 bool OptionMode::parseOption(ArgParser & parser, Option *& partialOption){
88  if(parser.isEndOfOption()){return true;}
89  if(p_name != ""){ //If the mode has no name, it is the default mode
90  if(p_name != parser.getCurrentOption()){return false;}
91  p_isParsed = true;
92  parser.getNextOption();
93  }
94  p_isCurrentlyParsed = true;
95  partialOption = NULL;
96  bool isSearch(true);
97  VecOption::iterator it(p_vecOption.begin());
98  while(isSearch && it != p_vecOption.end() && !parser.isEndOfOption()){
99  try{
100  isSearch = !it->parseOption(parser);
101  }catch(const std::runtime_error & e){
102  partialOption = &(*it);
103  isSearch = false;
104  }
105  ++it;
106  }
107  p_isParsed |= !isSearch;
108  return !isSearch;
109 }
110 
112 void OptionMode::print() const{
113  PString indentation("\t");
114  if(p_name != ""){
115  std::cout << "\tMode '" << p_name << "' :" << std::endl;
116  indentation += "\t";
117  }
118  for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){
119  it->print(indentation);
120  }
121 }
122 
124 
126 void OptionMode::setName(const PString & name){p_name = name;}
127 
129 
131 void OptionMode::setVecOption(const VecOption & vecOption){p_vecOption = vecOption;}
132 
134 
136 void OptionMode::addOption(const Option & option){p_vecOption.push_back(option);}
137 
140 
142 
145 
147 
149 void OptionMode::setProgramVersion(const PString & programVersion){p_programVersion = programVersion;}
150 
152 
154 const PString & OptionMode::getName() const{return p_name;}
155 
157 
160 
162 
165 
167 
170 
172 
175  if(!p_isParsed){return true;}
176  bool isArgOk(true);
177  VecOption::const_iterator it(p_vecOption.begin());
178  while(it != p_vecOption.end() && isArgOk){
179  isArgOk = it->checkArgument();
180  ++it;
181  }
182  return isArgOk;
183 }
184 
186 
189  return p_isCurrentlyParsed;
190 }
191 
193 
195 bool OptionMode::isParsed() const{
196  return p_isParsed;
197 }
198 
200 
203  return p_extraArgument;
204 }
205 
207 
210 bool OptionMode::isOptionExist(const PString & optionName) const{
211  VecOption::const_iterator it(p_vecOption.begin());
212  while(it != p_vecOption.end()){
213  if(it->getLongName() == optionName || it->getShortName() == optionName){
214  return it->isParsed();
215  }
216  ++it;
217  }
218  return false;
219 }
220 
222 
225 void OptionMode::getPossibleOption(PString & possibleOption, const PString & cursorOption) const{
226  if(p_name != ""){
227  if(!p_isCurrentlyParsed){
228  if(p_name.isSameBegining(cursorOption)){
229  possibleOption += p_name + " ";
230  }
231  }
232  }
233  iterGetPossibleOption(possibleOption, cursorOption);
234 }
235 
237 
240 void OptionMode::getPossibleMode(PString & possibleOption, const PString & cursorOption) const{
241  if(p_name != ""){
242  if(p_name.isSameBegining(cursorOption)){
243  possibleOption += p_name + " ";
244  }
245  }
246 }
247 
249 
252  p_name = other.p_name;
253  p_vecOption = other.p_vecOption;
255  p_isParsed = other.p_isParsed;
259 }
260 
263  p_isCurrentlyParsed = false;
264  p_isParsed = false;
265  p_enableHelpOption = false;
266 }
267 
269 
273 bool OptionMode::getOption(Option & option, const PString & optionName) const{
274  VecOption::const_iterator it(p_vecOption.begin());
275  while(it != p_vecOption.end()){
276  if(it->getLongName() == optionName || it->getShortName() == optionName){
277  option = *it;
278  return true;
279  }
280  ++it;
281  }
282  return false;
283 }
284 
286 
289 void OptionMode::iterGetPossibleOption(PString & possibleOption, const PString & cursorOption) const{
290  for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){
291  it->getPossibleOption(possibleOption, cursorOption);
292  }
293 }
294 
295 
std::vector< Option > VecOption
Vector of option.
Definition: Option.h:90
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
PString getRemainingArgument()
Get all remaining arguments of the ArgParser.
Definition: ArgParser.cpp:133
Describe a mode in the program arguments.
Definition: OptionMode.h:13
bool getOption(Option &option, const PString &optionName) const
Get the option with its name.
Definition: OptionMode.cpp:273
void setName(const PString &name)
Set the name of the OptionMode.
Definition: OptionMode.cpp:126
bool p_isCurrentlyParsed
True if the OptionMode is currently parsed but not totally.
Definition: OptionMode.h:64
void print() const
Print the option of the mode.
Definition: OptionMode.cpp:112
void setVecOption(const VecOption &vecOption)
Set the vector of options of the OptionMode.
Definition: OptionMode.cpp:131
OptionMode & operator=(const OptionMode &other)
Definition of equal operator of OptionMode.
Definition: OptionMode.cpp:34
void getPossibleOption(PString &possibleOption, const PString &cursorOption) const
Get the possible options for the bash completion.
Definition: OptionMode.cpp:225
void iterGetPossibleOption(PString &possibleOption, const PString &cursorOption) const
Iterates over the possible options of the current mode.
Definition: OptionMode.cpp:289
void initialisationOptionMode()
Initialisation function of the class OptionMode.
Definition: OptionMode.cpp:262
void setEnableHelpOption(bool b)
Set the attribtue which enables help option.
Definition: OptionMode.cpp:144
bool isCurrentlyParsed() const
Say if the OptionMode is currently parsed (but maybe not totally)
Definition: OptionMode.cpp:188
const PString & getExtraArgument() const
Get Extra argument.
Definition: OptionMode.cpp:202
VecOption p_vecOption
Vector of all the options of the OptionMode.
Definition: OptionMode.h:62
void getPossibleMode(PString &possibleOption, const PString &cursorOption) const
Get the possible mode.
Definition: OptionMode.cpp:240
bool isOptionExist(const PString &optionName) const
Say if the given option has been passed to the program.
Definition: OptionMode.cpp:210
void addOption(const Option &option)
Add an option into the OptionMode.
Definition: OptionMode.cpp:136
void setProgramVersion(const PString &programVersion)
Set the program version.
Definition: OptionMode.cpp:149
const VecOption & getVecOption() const
Get the vector of options of the OptionMode.
Definition: OptionMode.cpp:164
bool parseOption(ArgParser &parser)
Parse the options in the current OptionMode.
Definition: OptionMode.cpp:44
void copyOptionMode(const OptionMode &other)
Copy function of OptionMode.
Definition: OptionMode.cpp:251
const PString & getName() const
Get the name of the OptionMode.
Definition: OptionMode.cpp:154
bool isParsed() const
Say if the OptionMode contains option which are parsed.
Definition: OptionMode.cpp:195
PString p_extraArgument
Extra argument passed to the OptionMode.
Definition: OptionMode.h:72
bool checkArgument() const
Check the argument of the parser.
Definition: OptionMode.cpp:174
virtual ~OptionMode()
Destructeur of OptionMode.
Definition: OptionMode.cpp:26
void clearOption()
Remove all the options of the OptionMode.
Definition: OptionMode.cpp:139
bool p_isParsed
Say if the mode contains options which are parsed.
Definition: OptionMode.h:66
bool p_enableHelpOption
True to enable automatically the printing of the help option when the program is called with –help or...
Definition: OptionMode.h:68
PString p_programVersion
Program version to be printed on –version or -v option.
Definition: OptionMode.h:70
OptionMode(const PString &name="")
Default constructeur of OptionMode.
Definition: OptionMode.cpp:12
PString p_name
name of the OptionMode
Definition: OptionMode.h:60
Describes an option passed to a program.
Definition: Option.h:14
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