Directory: | ./ |
---|---|
File: | tmp_project/PhoenixOptionParser/src/OptionMode.cpp |
Date: | 2025-03-14 12:04:36 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 118 | 132 | 89.4% |
Branches: | 109 | 137 | 79.6% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include "OptionMode.h" | ||
8 | |||
9 | ///Default constructeur of OptionMode | ||
10 | /** @param name : name of the mode | ||
11 | */ | ||
12 | 127 | OptionMode::OptionMode(const PString & name) | |
13 |
2/2✓ Branch 3 taken 127 times.
✓ Branch 6 taken 127 times.
|
127 | :p_name(name) |
14 | { | ||
15 |
1/1✓ Branch 1 taken 127 times.
|
127 | initialisationOptionMode(); |
16 | 127 | } | |
17 | |||
18 | ///Copy constructor of OptionMode | ||
19 | /** @param other : class to copy | ||
20 | */ | ||
21 |
2/2✓ Branch 3 taken 225 times.
✓ Branch 6 taken 225 times.
|
225 | OptionMode::OptionMode(const OptionMode & other){ |
22 |
1/1✓ Branch 1 taken 225 times.
|
225 | copyOptionMode(other); |
23 | 225 | } | |
24 | |||
25 | ///Destructeur of OptionMode | ||
26 | 548 | OptionMode::~OptionMode(){ | |
27 | |||
28 | } | ||
29 | |||
30 | ///Definition of equal operator of OptionMode | ||
31 | /** @param other : class to copy | ||
32 | * @return copied class | ||
33 | */ | ||
34 | 2 | OptionMode & OptionMode::operator = (const OptionMode & other){ | |
35 | 2 | copyOptionMode(other); | |
36 | 2 | return *this; | |
37 | } | ||
38 | |||
39 | ///Parse the options in the current OptionMode | ||
40 | /** @param[out] parser : parser of option to be used | ||
41 | * @return true on success, false otherwise | ||
42 | * This function, is called, knowing, the current OptionMode has to be parsed | ||
43 | */ | ||
44 | 39 | bool OptionMode::parseOption(ArgParser & parser){ | |
45 | 39 | p_isParsed = true; | |
46 |
2/3✓ Branch 1 taken 39 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 39 times.
|
39 | if(parser.isEndOfOption()){return true;} |
47 | |||
48 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
|
39 | 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 |
49 | ✗ | p_extraArgument = parser.getRemainingArgument(); | |
50 | ✗ | return true; | |
51 | } | ||
52 | |||
53 | 39 | p_isCurrentlyParsed = true; | |
54 | 39 | bool isSearch(true); | |
55 | 39 | VecOption::iterator it(p_vecOption.begin()); | |
56 |
7/9✓ Branch 0 taken 45 times.
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 45 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 45 times.
✓ Branch 9 taken 45 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 45 times.
✓ Branch 12 taken 33 times.
|
78 | while(isSearch && it != p_vecOption.end() && !parser.isEndOfOption()){ |
57 |
2/3✓ Branch 1 taken 45 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 45 times.
|
45 | 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 | ||
60 | ✗ | p_extraArgument = parser.getRemainingArgument(); | |
61 | } | ||
62 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 21 times.
|
45 | if(p_enableHelpOption){ |
63 |
8/8✓ Branch 1 taken 24 times.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 2 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 20 times.
✓ Branch 12 taken 4 times.
✓ Branch 13 taken 20 times.
|
24 | if(parser.getCurrentOption() == "--help" || parser.getCurrentOption() == "-h"){ |
64 |
1/1✓ Branch 1 taken 4 times.
|
4 | print(); |
65 | 4 | exit(0); | |
66 | } | ||
67 | } | ||
68 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 21 times.
|
41 | if(p_programVersion != ""){ |
69 |
8/8✓ Branch 1 taken 20 times.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 1 times.
✓ Branch 7 taken 19 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 18 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 18 times.
|
20 | if(parser.getCurrentOption() == "--version" || parser.getCurrentOption() == "-v"){ |
70 |
3/3✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
|
2 | std::cout << "Program version : " << p_programVersion << std::endl; |
71 | 2 | exit(0); | |
72 | } | ||
73 | } | ||
74 | |||
75 |
1/1✓ Branch 2 taken 39 times.
|
39 | isSearch = !it->parseOption(parser); |
76 | 39 | ++it; | |
77 | } | ||
78 | 33 | p_isParsed |= !isSearch; | |
79 | 33 | return !isSearch; | |
80 | } | ||
81 | |||
82 | ///Parse the options in the current OptionMode | ||
83 | /** @param[out] parser : parser of option to be used | ||
84 | * @param[out] partialOption : pointer to an option partially parsed | ||
85 | * @return true on success, false otherwise | ||
86 | */ | ||
87 | 49 | bool OptionMode::parseOption(ArgParser & parser, Option *& partialOption){ | |
88 |
2/3✓ Branch 1 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 49 times.
|
49 | if(parser.isEndOfOption()){return true;} |
89 |
2/2✓ Branch 1 taken 22 times.
✓ Branch 2 taken 27 times.
|
49 | if(p_name != ""){ //If the mode has no name, it is the default mode |
90 |
3/3✓ Branch 1 taken 22 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 15 times.
|
22 | if(p_name != parser.getCurrentOption()){return false;} |
91 | 15 | p_isParsed = true; | |
92 |
1/1✓ Branch 1 taken 15 times.
|
15 | parser.getNextOption(); |
93 | } | ||
94 | 42 | p_isCurrentlyParsed = true; | |
95 | 42 | partialOption = NULL; | |
96 | 42 | bool isSearch(true); | |
97 | 42 | VecOption::iterator it(p_vecOption.begin()); | |
98 |
9/9✓ Branch 0 taken 59 times.
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 38 times.
✓ Branch 5 taken 21 times.
✓ Branch 7 taken 38 times.
✓ Branch 9 taken 33 times.
✓ Branch 10 taken 5 times.
✓ Branch 11 taken 33 times.
✓ Branch 12 taken 42 times.
|
75 | while(isSearch && it != p_vecOption.end() && !parser.isEndOfOption()){ |
99 | try{ | ||
100 |
1/1✓ Branch 2 taken 28 times.
|
33 | isSearch = !it->parseOption(parser); |
101 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | }catch(const std::runtime_error & e){ |
102 | 5 | partialOption = &(*it); | |
103 | 5 | isSearch = false; | |
104 | 5 | } | |
105 | 33 | ++it; | |
106 | } | ||
107 | 42 | p_isParsed |= !isSearch; | |
108 | 42 | return !isSearch; | |
109 | } | ||
110 | |||
111 | ///Print the option of the mode | ||
112 | 29 | void OptionMode::print() const{ | |
113 |
1/1✓ Branch 1 taken 29 times.
|
29 | PString indentation("\t"); |
114 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 19 times.
|
29 | if(p_name != ""){ |
115 |
4/4✓ Branch 1 taken 10 times.
✓ Branch 4 taken 10 times.
✓ Branch 7 taken 10 times.
✓ Branch 10 taken 10 times.
|
10 | std::cout << "\tMode '" << p_name << "' :" << std::endl; |
116 |
1/1✓ Branch 1 taken 10 times.
|
10 | indentation += "\t"; |
117 | } | ||
118 |
2/2✓ Branch 4 taken 40 times.
✓ Branch 5 taken 29 times.
|
69 | for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){ |
119 |
1/1✓ Branch 2 taken 40 times.
|
40 | it->print(indentation); |
120 | } | ||
121 | 29 | } | |
122 | |||
123 | ///Set the name of the OptionMode | ||
124 | /** @param name : name of the OptionMode | ||
125 | */ | ||
126 | ✗ | void OptionMode::setName(const PString & name){p_name = name;} | |
127 | |||
128 | ///Set the vector of options of the OptionMode | ||
129 | /** @param vecOption : vector of options of the OptionMode | ||
130 | */ | ||
131 | ✗ | void OptionMode::setVecOption(const VecOption & vecOption){p_vecOption = vecOption;} | |
132 | |||
133 | ///Add an option into the OptionMode | ||
134 | /** @param option option to be added into the OptionMode | ||
135 | */ | ||
136 | 176 | void OptionMode::addOption(const Option & option){p_vecOption.push_back(option);} | |
137 | |||
138 | ///Remove all the options of the OptionMode | ||
139 | ✗ | void OptionMode::clearOption(){p_vecOption.clear();} | |
140 | |||
141 | ///Set the attribtue which enables help option | ||
142 | /** @param b : true to enable help option, false otherwise | ||
143 | */ | ||
144 | 64 | void OptionMode::setEnableHelpOption(bool b){p_enableHelpOption = b;} | |
145 | |||
146 | ///Set the program version | ||
147 | /** @param programVersion : version of the program | ||
148 | */ | ||
149 | 64 | void OptionMode::setProgramVersion(const PString & programVersion){p_programVersion = programVersion;} | |
150 | |||
151 | ///Get the name of the OptionMode | ||
152 | /** @return name of the OptionMode | ||
153 | */ | ||
154 | 90 | const PString & OptionMode::getName() const{return p_name;} | |
155 | |||
156 | ///Get the name of the OptionMode | ||
157 | /** @return name of the OptionMode | ||
158 | */ | ||
159 | 89 | PString & OptionMode::getName(){return p_name;} | |
160 | |||
161 | ///Get the vector of options of the OptionMode | ||
162 | /** @return vector of options of the OptionMode | ||
163 | */ | ||
164 | 62 | const VecOption & OptionMode::getVecOption() const{return p_vecOption;} | |
165 | |||
166 | ///Get the vector of options of the OptionMode | ||
167 | /** @return vector of options of the OptionMode | ||
168 | */ | ||
169 | ✗ | VecOption & OptionMode::getVecOption(){return p_vecOption;} | |
170 | |||
171 | ///Check the argument of the parser | ||
172 | /** @return true if all the required arguments are set, false otherwise | ||
173 | */ | ||
174 | 47 | bool OptionMode::checkArgument() const{ | |
175 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 27 times.
|
47 | if(!p_isParsed){return true;} |
176 | 27 | bool isArgOk(true); | |
177 | 27 | VecOption::const_iterator it(p_vecOption.begin()); | |
178 |
5/6✓ Branch 2 taken 54 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 54 times.
✓ Branch 7 taken 27 times.
|
81 | while(it != p_vecOption.end() && isArgOk){ |
179 |
1/1✓ Branch 2 taken 54 times.
|
54 | isArgOk = it->checkArgument(); |
180 | 54 | ++it; | |
181 | } | ||
182 | 27 | return isArgOk; | |
183 | } | ||
184 | |||
185 | ///Say if the OptionMode is currently parsed (but maybe not totally) | ||
186 | /** @return true if the OptionMode is currently parsed (but maybe not totally), false otherwise | ||
187 | */ | ||
188 | 102 | bool OptionMode::isCurrentlyParsed() const{ | |
189 | 102 | return p_isCurrentlyParsed; | |
190 | } | ||
191 | |||
192 | ///Say if the OptionMode contains option which are parsed | ||
193 | /** @return true if the OptionMode contains option which are parsed, false otherwise | ||
194 | */ | ||
195 | 20 | bool OptionMode::isParsed() const{ | |
196 | 20 | return p_isParsed; | |
197 | } | ||
198 | |||
199 | ///Get Extra argument | ||
200 | /** @return get the extra arguments of the OptionMode | ||
201 | */ | ||
202 | ✗ | const PString & OptionMode::getExtraArgument() const{ | |
203 | ✗ | return p_extraArgument; | |
204 | } | ||
205 | |||
206 | ///Say if the given option has been passed to the program | ||
207 | /** @param[out] optionName : name of the option to be checked | ||
208 | * @return true if hte option has been passed to the program, false if not | ||
209 | */ | ||
210 | 5 | bool OptionMode::isOptionExist(const PString & optionName) const{ | |
211 | 5 | VecOption::const_iterator it(p_vecOption.begin()); | |
212 |
1/2✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
17 | while(it != p_vecOption.end()){ |
213 |
7/8✓ Branch 2 taken 17 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 5 times.
✓ Branch 9 taken 12 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 12 times.
✓ Branch 14 taken 5 times.
✓ Branch 15 taken 12 times.
|
17 | if(it->getLongName() == optionName || it->getShortName() == optionName){ |
214 |
1/1✓ Branch 2 taken 5 times.
|
5 | return it->isParsed(); |
215 | } | ||
216 | 12 | ++it; | |
217 | } | ||
218 | ✗ | return false; | |
219 | } | ||
220 | |||
221 | ///Get the possible options for the bash completion | ||
222 | /** @param[out] possibleOption : possible options for the bash completion | ||
223 | * @param cursorOption : option of the cursor which is currently completed | ||
224 | */ | ||
225 | 15 | void OptionMode::getPossibleOption(PString & possibleOption, const PString & cursorOption) const{ | |
226 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 7 times.
|
15 | if(p_name != ""){ |
227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if(!p_isCurrentlyParsed){ |
228 | ✗ | if(p_name.isSameBegining(cursorOption)){ | |
229 | ✗ | possibleOption += p_name + " "; | |
230 | } | ||
231 | } | ||
232 | } | ||
233 | 15 | iterGetPossibleOption(possibleOption, cursorOption); | |
234 | 15 | } | |
235 | |||
236 | ///Get the possible mode | ||
237 | /** @param[out] possibleOption : possible options for the bash completion | ||
238 | * @param cursorOption : option of the cursor which is currently completed | ||
239 | */ | ||
240 | 3 | void OptionMode::getPossibleMode(PString & possibleOption, const PString & cursorOption) const{ | |
241 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if(p_name != ""){ |
242 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if(p_name.isSameBegining(cursorOption)){ |
243 |
1/1✓ Branch 2 taken 2 times.
|
2 | possibleOption += p_name + " "; |
244 | } | ||
245 | } | ||
246 | 3 | } | |
247 | |||
248 | ///Copy function of OptionMode | ||
249 | /** @param other : class to copy | ||
250 | */ | ||
251 | 227 | void OptionMode::copyOptionMode(const OptionMode & other){ | |
252 | 227 | p_name = other.p_name; | |
253 | 227 | p_vecOption = other.p_vecOption; | |
254 | 227 | p_isCurrentlyParsed = other.p_isCurrentlyParsed; | |
255 | 227 | p_isParsed = other.p_isParsed; | |
256 | 227 | p_enableHelpOption = other.p_enableHelpOption; | |
257 | 227 | p_programVersion = other.p_programVersion; | |
258 | 227 | p_extraArgument = other.p_extraArgument; | |
259 | 227 | } | |
260 | |||
261 | ///Initialisation function of the class OptionMode | ||
262 | 127 | void OptionMode::initialisationOptionMode(){ | |
263 | 127 | p_isCurrentlyParsed = false; | |
264 | 127 | p_isParsed = false; | |
265 | 127 | p_enableHelpOption = false; | |
266 | 127 | } | |
267 | |||
268 | ///Get the option with its name | ||
269 | /** @param[out] option : option if it has been found | ||
270 | * @param optionName : name of the option to be searched | ||
271 | * @return true if the option has been found, false otherwise | ||
272 | */ | ||
273 | 37 | bool OptionMode::getOption(Option & option, const PString & optionName) const{ | |
274 | 37 | VecOption::const_iterator it(p_vecOption.begin()); | |
275 |
1/2✓ Branch 2 taken 59 times.
✗ Branch 3 not taken.
|
59 | while(it != p_vecOption.end()){ |
276 |
7/8✓ Branch 2 taken 59 times.
✓ Branch 5 taken 22 times.
✓ Branch 6 taken 37 times.
✓ Branch 9 taken 22 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 22 times.
✓ Branch 14 taken 37 times.
✓ Branch 15 taken 22 times.
|
59 | if(it->getLongName() == optionName || it->getShortName() == optionName){ |
277 |
1/1✓ Branch 2 taken 37 times.
|
37 | option = *it; |
278 | 37 | return true; | |
279 | } | ||
280 | 22 | ++it; | |
281 | } | ||
282 | ✗ | return false; | |
283 | } | ||
284 | |||
285 | ///Iterates over the possible options of the current mode | ||
286 | /** @param[out] possibleOption : possible options for the bash completion | ||
287 | * @param cursorOption : option of the cursor which is currently completed | ||
288 | */ | ||
289 | 15 | void OptionMode::iterGetPossibleOption(PString & possibleOption, const PString & cursorOption) const{ | |
290 |
2/2✓ Branch 4 taken 29 times.
✓ Branch 5 taken 15 times.
|
44 | for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){ |
291 |
1/1✓ Branch 2 taken 29 times.
|
29 | it->getPossibleOption(possibleOption, cursorOption); |
292 | } | ||
293 | 15 | } | |
294 | |||
295 | |||
296 |