GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/OptionParser/src/OptionMode.cpp Lines: 114 122 93.4 %
Date: 2024-12-09 15:30:52 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
550
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
41
bool OptionMode::parseOption(ArgParser & parser){
48
41
	if(parser.isEndOfOption()){return true;}
49
50
41
	p_isParsed = true;
51
41
	p_isCurrentlyParsed = true;
52
41
	bool isSearch(true);
53
41
	VecOption::iterator it(p_vecOption.begin());
54


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


24
			if(parser.getCurrentOption() == "--help" || parser.getCurrentOption() == "-h"){
57
4
				print();
58
4
				exit(0);
59
			}
60
		}
61
42
		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
40
		isSearch = !it->parseOption(parser);
68
40
		++it;
69
	}
70
35
	p_isParsed |= !isSearch;
71
35
	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
2
		}catch(const std::runtime_error & e){
94
2
			partialOption = &(*it);
95
2
			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
27
void OptionMode::print() const{
105
54
	std::string indentation("\t");
106
27
	if(p_name != ""){
107

10
		cout << "\tMode '" << p_name << "' :" << endl;
108
10
		indentation += "\t";
109
	}
110
65
	for(VecOption::const_iterator it(p_vecOption.begin()); it != p_vecOption.end(); ++it){
111
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
183
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
92
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
50
bool OptionMode::checkArgument() const{
167
50
	if(!p_isParsed){return true;}
168
30
	bool isArgOk(true);
169
30
	VecOption::const_iterator it(p_vecOption.begin());
170

96
	while(it != p_vecOption.end() && isArgOk){
171
66
		isArgOk = it->checkArgument();
172
66
		++it;
173
	}
174
30
	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
17
	while(it != p_vecOption.end()){
198


17
		if(it->getLongName() == optionName || it->getShortName() == optionName){
199
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
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
50
bool OptionMode::getOption(Option & option, const std::string & optionName) const{
258
50
	VecOption::const_iterator it(p_vecOption.begin());
259
94
	while(it != p_vecOption.end()){
260


94
		if(it->getLongName() == optionName || it->getShortName() == optionName){
261
50
			option = *it;
262
50
			return true;
263
		}
264
44
		++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