Directory: | ./ |
---|---|
File: | tmp_project/PhoenixOptionParser/src/OptionParser.cpp |
Date: | 2025-03-14 11:56:07 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 225 | 235 | 95.7% |
Branches: | 236 | 286 | 82.5% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include "OptionParser.h" | ||
8 | |||
9 | ///Default constructeur of OptionParser | ||
10 | /** @param enableHelpOption : True to enable automatically the printing of the help option when the program is called with --help or -h | ||
11 | * @param programVersion : version of the program to be printed on --version or -v options | ||
12 | */ | ||
13 | 71 | OptionParser::OptionParser(bool enableHelpOption, const PString & programVersion) | |
14 |
3/3✓ Branch 2 taken 71 times.
✓ Branch 5 taken 71 times.
✓ Branch 8 taken 71 times.
|
71 | :p_enableHelpOption(enableHelpOption), p_programVersion(programVersion) |
15 | { | ||
16 |
1/1✓ Branch 1 taken 71 times.
|
71 | initialisationOptionParser(); |
17 | 71 | } | |
18 | |||
19 | ///Copy constructor of OptionParser | ||
20 | /** @param other : class to copy | ||
21 | */ | ||
22 |
3/3✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
|
2 | OptionParser::OptionParser(const OptionParser & other){ |
23 |
1/1✓ Branch 1 taken 2 times.
|
2 | copyOptionParser(other); |
24 | 2 | } | |
25 | |||
26 | ///Destructeur of OptionParser | ||
27 | 78 | OptionParser::~OptionParser(){ | |
28 | |||
29 | } | ||
30 | |||
31 | ///Definition of equal operator of OptionParser | ||
32 | /** @param other : class to copy | ||
33 | * @return copied class | ||
34 | */ | ||
35 | 2 | OptionParser & OptionParser::operator = (const OptionParser & other){ | |
36 | 2 | copyOptionParser(other); | |
37 | 2 | return *this; | |
38 | } | ||
39 | |||
40 | ///Set the example usage of the program | ||
41 | /** @param example : example of usage | ||
42 | */ | ||
43 | 29 | void OptionParser::setExampleLongOption(const PString & example){p_exempleLongOption = example;} | |
44 | |||
45 | ///Set the example usage of the program | ||
46 | /** @param example : example of usage | ||
47 | */ | ||
48 | 29 | void OptionParser::setExampleShortOption(const PString & example){p_exempleShortOption = example;} | |
49 | |||
50 | ///Add a mode in the option | ||
51 | /** @param modeName : name of the new mode to be added | ||
52 | */ | ||
53 | 64 | void OptionParser::addMode(const PString & modeName){ | |
54 |
1/1✓ Branch 1 taken 64 times.
|
64 | OptionMode mode(modeName); |
55 |
1/1✓ Branch 1 taken 64 times.
|
64 | mode.setEnableHelpOption(p_enableHelpOption); |
56 |
1/1✓ Branch 1 taken 64 times.
|
64 | mode.setProgramVersion(p_programVersion); |
57 |
1/1✓ Branch 1 taken 64 times.
|
64 | p_vecMode.push_back(mode); |
58 | 64 | p_currentMode = p_vecMode.size() - 1lu; | |
59 | 64 | } | |
60 | |||
61 | ///Close the current mode and go back to be default one | ||
62 | 64 | void OptionParser::closeMode(){ | |
63 | 64 | p_currentMode = 0lu; | |
64 | 64 | } | |
65 | |||
66 | ///Add an option in the OptionParser | ||
67 | /** @param longOption : long option (start with --) as --version | ||
68 | * @param shortOption : short option (start with -) as -v | ||
69 | * @param optionType : type of the value to be parsed (INT, BOOL_ CHAR, FLOAT, STRING, FILENAME, DIRECTORY, etc) | ||
70 | * @param isRequired : true if the option is required, false if it is optionnal | ||
71 | * @param docString : documentation string of the option | ||
72 | */ | ||
73 | 139 | void OptionParser::addOption(const PString & longOption, const PString & shortOption, OptionType::OptionType optionType, | |
74 | bool isRequired, const PString & docString) | ||
75 | { | ||
76 |
1/1✓ Branch 1 taken 139 times.
|
139 | OptionValue value; |
77 |
1/1✓ Branch 1 taken 139 times.
|
139 | value.setType(optionType); |
78 |
1/1✓ Branch 1 taken 139 times.
|
139 | Option option(longOption, shortOption, value, isRequired, docString); |
79 |
1/1✓ Branch 1 taken 139 times.
|
139 | option.setIsAllowEmpty(false); |
80 |
1/1✓ Branch 2 taken 139 times.
|
139 | p_vecMode[p_currentMode].addOption(option); |
81 | 139 | } | |
82 | |||
83 | ///Add an option in the OptionParser | ||
84 | /** @param longOption : long option (start with --) as --version | ||
85 | * @param shortOption : short option (start with -) as -v | ||
86 | * @param optionType : type of the value to be parsed (INT, BOOL_ CHAR, FLOAT, STRING, FILENAME, DIRECTORY, etc) | ||
87 | * @param isRequired : true if the option is required, false if it is optionnal | ||
88 | * @param isAllowEmpty : the given value can be empty | ||
89 | * @param docString : documentation string of the option | ||
90 | */ | ||
91 | 2 | void OptionParser::addOption(const PString & longOption, const PString & shortOption, OptionType::OptionType optionType, | |
92 | bool isRequired, bool isAllowEmpty, const PString & docString) | ||
93 | { | ||
94 |
1/1✓ Branch 1 taken 2 times.
|
2 | OptionValue value; |
95 |
1/1✓ Branch 1 taken 2 times.
|
2 | value.setType(optionType); |
96 |
1/1✓ Branch 1 taken 2 times.
|
2 | Option option(longOption, shortOption, value, isRequired, docString); |
97 |
1/1✓ Branch 1 taken 2 times.
|
2 | option.setIsAllowEmpty(isAllowEmpty); |
98 |
1/1✓ Branch 2 taken 2 times.
|
2 | p_vecMode[p_currentMode].addOption(option); |
99 | 2 | } | |
100 | |||
101 | ///Add an option in the OptionParser | ||
102 | /** @param longOption : long option (start with --) as --version | ||
103 | * @param shortOption : short option (start with -) as -v | ||
104 | * @param optionType : type of the value to be parsed (INT, BOOL_ CHAR, FLOAT, STRING, FILENAME, DIRECTORY, etc) | ||
105 | * @param isRequired : true if the option is required, false if it is optionnal | ||
106 | * @param isAllowEmpty : the given value can be empty | ||
107 | * @param vecPossibleValue : vector of possible values for this option | ||
108 | * @param docString : documentation string of the option | ||
109 | */ | ||
110 | 7 | void OptionParser::addOption(const PString & longOption, const PString & shortOption, OptionType::OptionType optionType, | |
111 | bool isRequired, bool isAllowEmpty, const PVecString & vecPossibleValue, const PString & docString) | ||
112 | { | ||
113 |
1/1✓ Branch 1 taken 7 times.
|
7 | OptionValue value; |
114 |
1/1✓ Branch 1 taken 7 times.
|
7 | value.setType(optionType); |
115 |
1/1✓ Branch 1 taken 7 times.
|
7 | value.setVecPossibleValue(vecPossibleValue); |
116 |
1/1✓ Branch 1 taken 7 times.
|
7 | Option option(longOption, shortOption, value, isRequired, docString); |
117 |
1/1✓ Branch 1 taken 7 times.
|
7 | option.setIsAllowEmpty(isAllowEmpty); |
118 |
1/1✓ Branch 2 taken 7 times.
|
7 | p_vecMode[p_currentMode].addOption(option); |
119 | 7 | } | |
120 | |||
121 | ///Print all the options | ||
122 | 11 | void OptionParser::print() const{ | |
123 |
7/8✓ Branch 1 taken 11 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 9 times.
✓ Branch 6 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 2 times.
|
11 | if(p_exempleLongOption != "" || p_exempleShortOption != ""){ |
124 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
|
9 | std::cout << "Usage :" << std::endl; |
125 |
5/6✓ Branch 1 taken 9 times.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
✓ Branch 12 taken 9 times.
|
9 | if(p_exempleLongOption != ""){std::cout << "\t" << p_exempleLongOption << std::endl;} |
126 |
5/6✓ Branch 1 taken 9 times.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 9 times.
✓ Branch 9 taken 9 times.
✓ Branch 12 taken 9 times.
|
9 | if(p_exempleShortOption != ""){std::cout << "\t" << p_exempleShortOption << std::endl;} |
127 | } | ||
128 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
11 | std::cout << "Parameters :" << std::endl; |
129 | 11 | VecMode::const_iterator it(p_vecMode.begin()); | |
130 |
1/1✓ Branch 2 taken 11 times.
|
11 | it->print(); |
131 | 11 | ++it; | |
132 |
2/2✓ Branch 2 taken 4 times.
✓ Branch 3 taken 11 times.
|
15 | while(it != p_vecMode.end()){ |
133 |
1/1✓ Branch 2 taken 4 times.
|
4 | it->print(); |
134 | 4 | ++it; | |
135 | } | ||
136 | 11 | } | |
137 | |||
138 | ///Parse the arguments passed to the program | ||
139 | /** @param argc : number of parameters passed to the program | ||
140 | * @param argv : list of arguments passed to the program | ||
141 | */ | ||
142 | 69 | void OptionParser::parseArgument(int argc, char** argv){ | |
143 |
1/1✓ Branch 1 taken 69 times.
|
69 | ArgParser parser(argc, argv); |
144 |
3/3✓ Branch 1 taken 69 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 42 times.
|
69 | if(parser.isBashCompletionMode()){ |
145 |
0/1✗ Branch 1 not taken.
|
27 | parseArgumentBashCompletion(parser); |
146 | }else{ | ||
147 |
1/1✓ Branch 1 taken 35 times.
|
42 | parseArgumentNormalUse(parser); |
148 | } | ||
149 | 35 | } | |
150 | |||
151 | ///Get default mode | ||
152 | /** @return default mode | ||
153 | */ | ||
154 | 6 | const OptionMode & OptionParser::getDefaultMode() const{ | |
155 | 6 | return p_vecMode[0lu]; | |
156 | } | ||
157 | |||
158 | ///Get default mode | ||
159 | /** @return default mode | ||
160 | */ | ||
161 | 26 | OptionMode & OptionParser::getDefaultMode(){ | |
162 | 26 | return p_vecMode[0lu]; | |
163 | } | ||
164 | |||
165 | ///Get mode by name | ||
166 | /** @param name : name of the mode to be returned | ||
167 | * @return corresponding mode if the mode exists or the default mode otherwise | ||
168 | */ | ||
169 | 4 | const OptionMode & OptionParser::getMode(const PString & name) const{ | |
170 |
2/2✓ Branch 4 taken 6 times.
✓ Branch 5 taken 2 times.
|
8 | for(VecMode::const_iterator it(p_vecMode.begin()); it != p_vecMode.end(); ++it){ |
171 |
3/3✓ Branch 2 taken 6 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 4 times.
|
6 | if(it->getName() == name){ |
172 | 2 | return *it; | |
173 | } | ||
174 | } | ||
175 | 2 | return getDefaultMode(); | |
176 | } | ||
177 | |||
178 | ///Get mode by name | ||
179 | /** @param name : name of the mode to be returned | ||
180 | * @return corresponding mode if the mode exists or the default mode otherwise | ||
181 | */ | ||
182 | 38 | OptionMode & OptionParser::getMode(const PString & name){ | |
183 |
2/2✓ Branch 4 taken 89 times.
✓ Branch 5 taken 2 times.
|
91 | for(VecMode::iterator it(p_vecMode.begin()); it != p_vecMode.end(); ++it){ |
184 |
3/3✓ Branch 2 taken 89 times.
✓ Branch 5 taken 36 times.
✓ Branch 6 taken 53 times.
|
89 | if(it->getName() == name){ |
185 | 36 | return *it; | |
186 | } | ||
187 | } | ||
188 | 2 | return getDefaultMode(); | |
189 | } | ||
190 | |||
191 | ///Check if the given mode name does exist | ||
192 | /** @param name : name of the mode | ||
193 | * @return true if the given mode name does exist, false otherwise | ||
194 | */ | ||
195 | 56 | bool OptionParser::isModeExist(const PString & name) const{ | |
196 | 56 | bool isSearch(true); | |
197 | 56 | VecMode::const_iterator it(p_vecMode.begin()); | |
198 |
6/6✓ Branch 0 taken 125 times.
✓ Branch 1 taken 16 times.
✓ Branch 4 taken 85 times.
✓ Branch 5 taken 40 times.
✓ Branch 6 taken 85 times.
✓ Branch 7 taken 56 times.
|
141 | while(isSearch && it != p_vecMode.end()){ |
199 |
1/1✓ Branch 2 taken 85 times.
|
85 | isSearch &= it->getName() != name; |
200 | 85 | ++it; | |
201 | } | ||
202 | 56 | return !isSearch; | |
203 | } | ||
204 | |||
205 | ///Copy function of OptionParser | ||
206 | /** @param other : class to copy | ||
207 | */ | ||
208 | 4 | void OptionParser::copyOptionParser(const OptionParser & other){ | |
209 | 4 | p_vecMode = other.p_vecMode; | |
210 | 4 | p_currentMode = other.p_currentMode; | |
211 | 4 | p_exempleShortOption = other.p_exempleShortOption; | |
212 | 4 | p_exempleLongOption = other.p_exempleLongOption; | |
213 | 4 | p_programVersion = other.p_programVersion; | |
214 | 4 | p_enableHelpOption = other.p_enableHelpOption; | |
215 | 4 | } | |
216 | |||
217 | ///Initialisation function of the class OptionParser | ||
218 | 71 | void OptionParser::initialisationOptionParser(){ | |
219 | 71 | p_currentMode = 0lu; | |
220 | 71 | p_currentParserMode = NULL; | |
221 |
2/2✓ Branch 1 taken 71 times.
✓ Branch 4 taken 71 times.
|
71 | OptionMode defaultMode; |
222 |
1/1✓ Branch 1 taken 71 times.
|
71 | p_vecMode.push_back(defaultMode); |
223 | 71 | } | |
224 | |||
225 | ///Classical argument parsing mode | ||
226 | /** @param parser : parser to be used | ||
227 | */ | ||
228 | 42 | void OptionParser::parseArgumentNormalUse(ArgParser & parser){ | |
229 | 42 | p_currentParserMode = &p_vecMode.front(); | |
230 |
2/2✓ Branch 1 taken 57 times.
✓ Branch 2 taken 35 times.
|
92 | while(!parser.isEndOfOption()){ |
231 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | if(p_enableHelpOption){ |
232 |
5/6✓ Branch 2 taken 56 times.
✓ Branch 3 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 56 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 56 times.
|
57 | if(parser.getCurrentOption() == "--help" || parser.getCurrentOption() == "-h"){ |
233 | 1 | print(); | |
234 | 1 | exit(0); | |
235 | } | ||
236 | } | ||
237 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | if(p_programVersion != ""){ |
238 |
3/6✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 56 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 56 times.
|
56 | if(parser.getCurrentOption() == "--version" || parser.getCurrentOption() == "-v"){ |
239 | ✗ | std::cout << "Program version : " << p_programVersion << std::endl; | |
240 | ✗ | exit(0); | |
241 | } | ||
242 | } | ||
243 | 56 | OptionMode & currentMode = getParserMode(parser); | |
244 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
|
56 | if(!currentMode.parseOption(parser)){ |
245 | ✗ | PString modeName(currentMode.getName()); | |
246 | ✗ | PString modeError(""); | |
247 | ✗ | if(modeName != ""){ | |
248 | ✗ | modeError = " in mode '"+modeName+"' "; | |
249 | } | ||
250 | ✗ | throw std::runtime_error("OptionParser::parseArgument : unknown option '"+parser.getCurrentOption()+"'" + modeError); | |
251 | } | ||
252 | } | ||
253 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
|
35 | if(!checkArgument()){ |
254 | ✗ | throw std::runtime_error("OptionParser::parseArgument : missing argument"); | |
255 | } | ||
256 | 35 | } | |
257 | |||
258 | ///Bash completion argument parsing mode | ||
259 | /** @param parser : parser to be used | ||
260 | */ | ||
261 | 27 | void OptionParser::parseArgumentBashCompletion(ArgParser & parser){ | |
262 | //First step, we parse normally the existing arguments | ||
263 |
2/2✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
|
27 | PString cursorOption(parser.getCursorOption()); |
264 |
2/2✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
|
27 | PString prevCursorOption(parser.getPrevCursorOption()); |
265 | 27 | Option * partialOption = NULL; | |
266 |
3/3✓ Branch 1 taken 54 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 27 times.
|
54 | while(!parser.isEndOfOption()){ //We loop to find the option which has not been parsed well |
267 | 27 | bool isSearch(true); | |
268 | 27 | VecMode::iterator itMode = p_vecMode.begin(); | |
269 |
9/9✓ Branch 1 taken 76 times.
✓ Branch 3 taken 58 times.
✓ Branch 4 taken 18 times.
✓ Branch 7 taken 52 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 49 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 49 times.
✓ Branch 12 taken 27 times.
|
76 | while(!parser.isEndOfOption() && itMode != p_vecMode.end() && isSearch){ |
270 |
1/1✓ Branch 2 taken 49 times.
|
49 | isSearch = !itMode->parseOption(parser, partialOption); |
271 | 49 | ++itMode; | |
272 | } | ||
273 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
|
27 | if(isSearch){ //If no option matches, we go to the next argument |
274 |
1/1✓ Branch 1 taken 11 times.
|
11 | parser.getNextOption(); |
275 | } | ||
276 | } | ||
277 | //Complete the ongoing option (filename, directory, enum value, etc) | ||
278 | // if(partialOption != NULL){ //The option name has no ambiguity but we expect a value | ||
279 | // PString possibleValue(""); | ||
280 | // partialOption->getPossibleValue(possibleValue, cursorOption); | ||
281 | // cout << possibleValue << endl; | ||
282 | // }else{ | ||
283 | //Let's get the mode which is currently parsed | ||
284 | //If all the options are fine, we have to show the remaning options. That's the following | ||
285 |
1/1✓ Branch 1 taken 27 times.
|
27 | PString possibleValue(""); |
286 |
3/3✓ Branch 1 taken 27 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 16 times.
|
27 | if(completeOptionValue(possibleValue, cursorOption, prevCursorOption)){ |
287 | // saveFileContent("listPossibleValues.txt", possibleValue); | ||
288 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 4 taken 11 times.
|
11 | std::cout << possibleValue << std::endl; |
289 | }else{ | ||
290 | //Then, get the remaning arguments (not parsed yet) | ||
291 |
1/1✓ Branch 1 taken 16 times.
|
16 | PString possibleOption(""); |
292 |
1/1✓ Branch 1 taken 16 times.
|
16 | getPossibleOption(possibleOption, cursorOption); |
293 |
1/1✓ Branch 1 taken 16 times.
|
16 | getPossibleOtherOption(possibleOption, cursorOption); |
294 | |||
295 | // saveFileContent("listPossibleOption.txt", possibleOption); | ||
296 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
|
16 | std::cout << possibleOption << std::endl; |
297 | 16 | } | |
298 | // } | ||
299 | 27 | exit(0); | |
300 | // La marche a suivre est assez simple | ||
301 | // On renvoie une ligne par argument possible | ||
302 | // Si il y en a qu'une seule, elle sera ajouté à la fin de la ligne de commande | ||
303 | // DONE : mettre en place un méchanisme pour générer le bash qui appellera le programme différemment | ||
304 | } | ||
305 | |||
306 | ///Check the argument of the parser | ||
307 | /** @return true if all the required arguments are set, false otherwise | ||
308 | */ | ||
309 | 35 | bool OptionParser::checkArgument() const{ | |
310 | 35 | bool isArgOk(true); | |
311 | 35 | VecMode::const_iterator it(p_vecMode.begin()); | |
312 |
5/6✓ Branch 2 taken 55 times.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 55 times.
✓ Branch 7 taken 35 times.
|
90 | while(it != p_vecMode.end() && isArgOk){ |
313 |
1/1✓ Branch 2 taken 55 times.
|
55 | isArgOk = it->checkArgument(); |
314 | 55 | ++it; | |
315 | } | ||
316 | 35 | return isArgOk; | |
317 | } | ||
318 | |||
319 | ///Get a mode if it exist | ||
320 | /** @param parser : parser to be used | ||
321 | * @return corresponding mode | ||
322 | */ | ||
323 | 56 | OptionMode & OptionParser::getParserMode(ArgParser & parser){ | |
324 |
2/2✓ Branch 1 taken 56 times.
✓ Branch 4 taken 56 times.
|
56 | PString currentOption(parser.getCurrentOption()); |
325 |
3/3✓ Branch 1 taken 56 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 40 times.
|
56 | if(isModeExist(currentOption)){ |
326 |
1/1✓ Branch 1 taken 16 times.
|
16 | OptionMode & mode = getMode(currentOption); |
327 | 16 | p_currentParserMode = &mode; | |
328 |
1/1✓ Branch 1 taken 16 times.
|
16 | parser.getNextOption(); |
329 | 16 | return mode; | |
330 | }else{ | ||
331 | 40 | return *p_currentParserMode; | |
332 | } | ||
333 | 56 | } | |
334 | |||
335 | ///Get the currently parsed OptionMode | ||
336 | /** @return pointer to the currently parsed OptionMode, it there is one, or NULL is there is not | ||
337 | */ | ||
338 | 9 | const OptionMode * OptionParser::getCurrentlyParsedMode() const{ | |
339 | 9 | const OptionMode * mode = NULL; | |
340 | 9 | VecMode::const_iterator it(p_vecMode.begin()); | |
341 |
6/6✓ Branch 2 taken 27 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 20 times.
✓ Branch 7 taken 9 times.
|
29 | while(it != p_vecMode.end() && mode == NULL){ |
342 |
9/9✓ Branch 2 taken 20 times.
✓ Branch 4 taken 16 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 16 times.
✓ Branch 11 taken 16 times.
✓ Branch 13 taken 8 times.
✓ Branch 14 taken 8 times.
✓ Branch 15 taken 8 times.
✓ Branch 16 taken 12 times.
|
20 | if(it->isCurrentlyParsed() && it->getName() != ""){ |
343 | 8 | mode = &(*it); | |
344 | } | ||
345 | 20 | ++it; | |
346 | } | ||
347 | 9 | return mode; | |
348 | } | ||
349 | |||
350 | ///Complete the possible value of an option (FILENAME, DIRECTORY, FILE_OR_DIR) | ||
351 | /** @param[out] possibleValue : possible value | ||
352 | * @param cursorOption : option of the cursor which is currently completed | ||
353 | * @param prevCursorOption : previous option of the cursor | ||
354 | */ | ||
355 | 27 | bool OptionParser::completeOptionValue(PString & possibleValue, const PString & cursorOption, const PString & prevCursorOption) const{ | |
356 | // std::cerr << "OptionParser::completeOptionValue : cursorOption = '"<<cursorOption<<"', prevCursorOption = '"<<prevCursorOption<<"'" << std::endl; | ||
357 |
1/1✓ Branch 1 taken 27 times.
|
27 | PString valueToBeCompleted(""); |
358 | //Check is the cursor option starts with a long option (--option=value) because the value can be completed | ||
359 |
1/1✓ Branch 1 taken 27 times.
|
27 | const Option * op = getLongOptionValue(valueToBeCompleted, cursorOption); |
360 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
27 | if(op == NULL){ //Check is the previous option corresponds to an existing option, to complete the value given by the current option |
361 |
1/1✓ Branch 1 taken 27 times.
|
27 | op = getSplitOptionValue(valueToBeCompleted, cursorOption, prevCursorOption); |
362 | } | ||
363 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
|
27 | if(op != NULL){ |
364 |
1/1✓ Branch 1 taken 11 times.
|
11 | op->getPossibleValue(possibleValue, valueToBeCompleted); |
365 | // std::cerr << "OptionParser::completeOptionValue : possibleValue = '"<<possibleValue<<"'" << std::endl; | ||
366 | 11 | return true; | |
367 | } | ||
368 | 16 | return false; | |
369 | 27 | } | |
370 | |||
371 | ///Get the long option value to be completed | ||
372 | /** @param[out] valueToBeCompleted : base of the value to be completed | ||
373 | * @param cursorOption : option of the cursor which is currently completed | ||
374 | * @return pointer to the Option to be completed or NULL if there is not | ||
375 | */ | ||
376 | 27 | const Option * OptionParser::getLongOptionValue(PString & valueToBeCompleted, const PString & cursorOption) const{ | |
377 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 16 times.
|
27 | if(cursorOption == ""){return NULL;} |
378 | 16 | const Option * op = NULL; | |
379 | 16 | VecMode::const_iterator itMode(p_vecMode.begin()); | |
380 |
5/6✓ Branch 2 taken 40 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✓ Branch 7 taken 16 times.
|
56 | while(itMode != p_vecMode.end() && op == NULL){ |
381 |
3/3✓ Branch 2 taken 40 times.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 12 times.
|
40 | if(itMode->isCurrentlyParsed()){ |
382 |
1/1✓ Branch 2 taken 28 times.
|
28 | const VecOption & vecOp = itMode->getVecOption(); |
383 | 28 | VecOption::const_iterator itOp(vecOp.begin()); | |
384 |
5/6✓ Branch 2 taken 40 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✓ Branch 7 taken 28 times.
|
68 | while(itOp != vecOp.end() && op == NULL){ |
385 |
4/4✓ Branch 2 taken 40 times.
✓ Branch 5 taken 40 times.
✓ Branch 8 taken 40 times.
✓ Branch 11 taken 40 times.
|
80 | PString fullOp("--" + itOp->getLongName() + "="); |
386 |
2/3✓ Branch 1 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
|
40 | if(cursorOption.isSameBegining(fullOp)){ |
387 | ✗ | op = &(*itOp); | |
388 | ✗ | valueToBeCompleted = cursorOption.substr(fullOp.size()); | |
389 | } | ||
390 | 40 | ++itOp; | |
391 | 40 | } | |
392 | } | ||
393 | 40 | ++itMode; | |
394 | } | ||
395 | 16 | return op; | |
396 | } | ||
397 | |||
398 | ///Get the split option (without =) value to be completed | ||
399 | /** @param[out] valueToBeCompleted : base of the value to be completed | ||
400 | * @param cursorOption : option of the cursor which is currently completed | ||
401 | * @param prevCursorOption : previous option of the cursor | ||
402 | * @return pointer to the Option to be completed or NULL if there is not | ||
403 | */ | ||
404 | 27 | const Option * OptionParser::getSplitOptionValue(PString & valueToBeCompleted, const PString & cursorOption, const PString & prevCursorOption) const{ | |
405 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 19 times.
|
27 | if(prevCursorOption == ""){return NULL;} |
406 | 19 | const Option * op = NULL; | |
407 | 19 | VecMode::const_iterator itMode(p_vecMode.begin()); | |
408 |
6/6✓ Branch 2 taken 49 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 42 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 42 times.
✓ Branch 7 taken 19 times.
|
61 | while(itMode != p_vecMode.end() && op == NULL){ |
409 |
3/3✓ Branch 2 taken 42 times.
✓ Branch 4 taken 34 times.
✓ Branch 5 taken 8 times.
|
42 | if(itMode->isCurrentlyParsed()){ //Search only in the mode which is currently parsed |
410 |
1/1✓ Branch 2 taken 34 times.
|
34 | const VecOption & vecOp = itMode->getVecOption(); |
411 | 34 | VecOption::const_iterator itOp(vecOp.begin()); | |
412 |
5/6✓ Branch 2 taken 47 times.
✓ Branch 3 taken 34 times.
✓ Branch 4 taken 47 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 47 times.
✓ Branch 7 taken 34 times.
|
81 | while(itOp != vecOp.end() && op == NULL){ |
413 |
6/6✓ Branch 2 taken 47 times.
✓ Branch 5 taken 47 times.
✓ Branch 8 taken 47 times.
✓ Branch 12 taken 47 times.
✓ Branch 15 taken 47 times.
✓ Branch 18 taken 47 times.
|
94 | PString fullLongOp("--" + itOp->getLongName()), fullShortOption("-" + itOp->getShortName()); |
414 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 37 times.
|
47 | if(fullLongOp == prevCursorOption){ |
415 | 10 | op = &(*itOp); | |
416 |
1/1✓ Branch 1 taken 10 times.
|
10 | valueToBeCompleted = cursorOption; |
417 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 36 times.
|
37 | }else if(fullShortOption == prevCursorOption){ |
418 | 1 | op = &(*itOp); | |
419 |
1/1✓ Branch 1 taken 1 times.
|
1 | valueToBeCompleted = cursorOption; |
420 | } | ||
421 | 47 | ++itOp; | |
422 | 47 | } | |
423 | } | ||
424 | 42 | ++itMode; | |
425 | } | ||
426 | 19 | return op; | |
427 | } | ||
428 | |||
429 | ///Get the possible options which can be used | ||
430 | /** @param[out] possibleOption : possible options for the bash completion | ||
431 | * @param cursorOption : option of the cursor which is currently completed | ||
432 | */ | ||
433 | 16 | void OptionParser::getPossibleOption(PString & possibleOption, const PString & cursorOption) const{ | |
434 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 9 times.
|
16 | if(p_vecMode.size() == 1lu){ |
435 | 7 | p_vecMode.front().getPossibleOption(possibleOption, cursorOption); | |
436 | }else{ | ||
437 | 9 | const OptionMode * currentlyParsedMode = getCurrentlyParsedMode(); | |
438 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | if(currentlyParsedMode != NULL){ |
439 | 8 | currentlyParsedMode->getPossibleOption(possibleOption, cursorOption); | |
440 | }else{ | ||
441 |
2/2✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
|
4 | for(VecMode::const_iterator itMode = p_vecMode.begin(); itMode != p_vecMode.end(); ++itMode){ |
442 |
1/1✓ Branch 2 taken 3 times.
|
3 | itMode->getPossibleMode(possibleOption, cursorOption); |
443 | } | ||
444 | } | ||
445 | } | ||
446 | 16 | } | |
447 | |||
448 | ///Get the possible other options which can be used | ||
449 | /** @param[out] possibleOption : possible options for the bash completion | ||
450 | * @param cursorOption : option of the cursor which is currently completed | ||
451 | */ | ||
452 | 16 | void OptionParser::getPossibleOtherOption(PString & possibleOption, const PString & cursorOption) const{ | |
453 | 16 | std::vector<PString> vecOtherOption; | |
454 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
|
16 | vecOtherOption.push_back("--help"); |
455 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
|
16 | vecOtherOption.push_back("-h"); |
456 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
|
16 | vecOtherOption.push_back("--version"); |
457 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 4 taken 16 times.
|
16 | vecOtherOption.push_back("-v"); |
458 | |||
459 |
2/2✓ Branch 4 taken 64 times.
✓ Branch 5 taken 16 times.
|
80 | for(std::vector<PString>::iterator it(vecOtherOption.begin()); it != vecOtherOption.end(); ++it){ |
460 |
1/1✓ Branch 2 taken 64 times.
|
64 | PString optionStr(*it); |
461 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 36 times.
|
64 | if(cursorOption == ""){ |
462 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 4 taken 28 times.
|
28 | possibleOption += optionStr + " "; |
463 | }else{ | ||
464 |
3/3✓ Branch 1 taken 36 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 17 times.
|
36 | if(optionStr.isSameBegining(cursorOption)){ |
465 |
2/2✓ Branch 1 taken 19 times.
✓ Branch 4 taken 19 times.
|
19 | possibleOption += optionStr + " "; |
466 | } | ||
467 | } | ||
468 | 64 | } | |
469 | 16 | } | |
470 | |||
471 |