GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/OptionParser/src/OptionMode.cpp Lines: 111 122 91.0 %
Date: 2023-10-11 10:52:07 Branches: 101 116 87.1 %

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
125
OptionMode::OptionMode(const std::string & name)
16
125
	:p_name(name)
17
{
18
125
	initialisationOptionMode();
19
125
}
20
21
///Copy constructor of OptionMode
22
/**	@param other : class to copy
23
*/
24
223
OptionMode::OptionMode(const OptionMode & other){
25
223
	copyOptionMode(other);
26
223
}
27
28
///Destructeur of OptionMode
29
552
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
42
bool OptionMode::parseOption(ArgParser & parser){
48
42
	if(parser.isEndOfOption()){return true;}
49
50
42
	p_isParsed = true;
51
42
	p_isCurrentlyParsed = true;
52
42
	bool isSearch(true);
53
42
	VecOption::iterator it(p_vecOption.begin());
54


97
	while(isSearch && it != p_vecOption.end() && !parser.isEndOfOption()){
55
61
		if(p_enableHelpOption){
56


24
			if(parser.getCurrentOption() == "--help" || parser.getCurrentOption() == "-h"){
57
4
				print();
58
4
				exit(0);
59
			}
60
		}
61
57
		if(p_programVersion != ""){
62


20
			if(parser.getCurrentOption() == "--version" || parser.getCurrentOption() == "-v"){
63
2
				std::cout << "Program version : " << p_programVersion << std::endl;
64
2
				exit(0);
65
			}
66
		}
67
55
		isSearch = !it->parseOption(parser);
68
55
		++it;
69
	}
70
36
	p_isParsed |= !isSearch;
71
36
	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
45
	if(parser.isEndOfOption()){return true;}
81
45
	if(p_name != ""){	//If the mode has no name, it is the default mode
82
22
		if(p_name != parser.getCurrentOption()){return false;}
83
15
		p_isParsed = true;
84
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


67
	while(isSearch && it != p_vecOption.end() && !parser.isEndOfOption()){
91
		try{
92
29
			isSearch = !it->parseOption(parser);
93
		}catch(const std::runtime_error & e){
94
			partialOption = &(*it);
95
			isSearch = false;
96
		}
97
29
		++it;
98
	}
99
38
	p_isParsed |= !isSearch;
100
38
	return !isSearch;
101
}
102
103
///Print the option of the mode
104
26
void OptionMode::print() const{
105
52
	std::string indentation("\t");
106
26
	if(p_name != ""){
107

10
		cout << "\tMode '" << p_name << "' :" << endl;
108
10
		indentation += "\t";
109
	}
110
60
	for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){
111
34
		it->print(indentation);
112
	}
113
26
}
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
197
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
93
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
51
bool OptionMode::checkArgument() const{
167
51
	if(!p_isParsed){return true;}
168
31
	bool isArgOk(true);
169
31
	VecOption::const_iterator it(p_vecOption.begin());
170

115
	while(it != p_vecOption.end() && isArgOk){
171
84
		isArgOk = it->checkArgument();
172
84
		++it;
173
	}
174
31
	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
40
bool OptionMode::isOptionExist(const std::string & optionName) const{
196
40
	VecOption::const_iterator it(p_vecOption.begin());
197
122
	while(it != p_vecOption.end()){
198


122
		if(it->getLongName() == optionName || it->getShortName() == optionName){
199
40
			return it->isParsed();
200
		}
201
82
		++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
13
	if(p_name != ""){
212
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
3
	if(p_name != ""){
227
2
		if(isSameBegining(p_name, cursorOption)){
228
2
			possibleOption += p_name + " ";
229
		}
230
	}
231
3
}
232
233
///Copy function of OptionMode
234
/**	@param other : class to copy
235
*/
236
225
void OptionMode::copyOptionMode(const OptionMode & other){
237
225
	p_name = other.p_name;
238
225
	p_vecOption = other.p_vecOption;
239
225
	p_isCurrentlyParsed = other.p_isCurrentlyParsed;
240
225
	p_isParsed = other.p_isParsed;
241
225
	p_enableHelpOption = other.p_enableHelpOption;
242
225
	p_programVersion = other.p_programVersion;
243
225
}
244
245
///Initialisation function of the class OptionMode
246
125
void OptionMode::initialisationOptionMode(){
247
125
	p_isCurrentlyParsed = false;
248
125
	p_isParsed = false;
249
125
	p_enableHelpOption = false;
250
125
}
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
62
bool OptionMode::getOption(Option & option, const std::string & optionName) const{
258
62
	VecOption::const_iterator it(p_vecOption.begin());
259
133
	while(it != p_vecOption.end()){
260


133
		if(it->getLongName() == optionName || it->getShortName() == optionName){
261
62
			option = *it;
262
62
			return true;
263
		}
264
71
		++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
40
	for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){
275
27
		it->getPossibleOption(possibleOption, cursorOption);
276
	}
277
13
}
278
279