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