File: argmap.cpp

package info (click to toggle)
libopendbx 1.4.6-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,960 kB
  • ctags: 1,006
  • sloc: sh: 10,899; ansic: 7,283; xml: 1,837; cpp: 1,696; makefile: 362; sed: 16
file content (507 lines) | stat: -rw-r--r-- 10,777 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 *  Parameter parsing from file, command line and environment
 *  (c) 2006-2008, Norbert Sendetzky <norbert@linuxnetworks.de>
 *  Inspired by PowerDNS ArgvMap code
 *
 *  Distributed under the terms of the GNU Library General Public Licence
 *  version 2 or (at your option) any later version.
 */



#include "argmap.hpp"
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>


#ifndef gettext_noop
#define gettext_noop(string) string
#endif



/**
 *  Declares a parameter and sets an associated short help message
 *
 * @param longopt long name of the parameter
 * @param shortopt short name of the parameter
 * @param help short description
 * @param req if value is required
 * @returns reference to the parameter content
 */

string& ArgMap::set( const string& longopt, const string& shortopt, const string& help, bool req )
{
	struct argopt option;

	option.longopt = longopt;
	option.shortopt = shortopt;
	option.help = help;
	option.req = req;

	m_arg.push_back( option );
	m_long[longopt] = m_arg.size() - 1;
	m_short[shortopt] = m_arg.size() - 1;

	return m_arg[m_arg.size()-1].value;
}



/**
 *  Tests if the parameter value is 'yes'
 *
 *  @param arg name of the parameter
 *  @returns true if the parameter value is true, false otherwise
 *  @throws ArgException - if the parameter doesn't exist
 */

bool ArgMap::mustDo( const string& arg )
{
	if( !m_long.count( arg ) ) {
		throw ArgException( "ArgMap: Undefined parameter '" + arg + "'" );
	}

	return ( m_arg[m_long[arg]].value == "yes" );
}



/**
 *  Converts the parameter value to a long integer
 *
 *  @param arg name of the parameter
 *  @returns long integer value of the string
 * @throws ArgException - if arg is unknown or the value isn't an integer
 */

long ArgMap::asLong( const string& arg )
{
	char* end;
	long num;


	if( !m_long.count( arg ) ) {
		throw ArgException( "ArgMap: Undefined parameter '" + arg + "'" );
	}

	num = strtol( m_arg[m_long[arg]].value.c_str(), &end, 10 );
	if( *end != '\0' ) {
		throw ArgException( "ArgMap: Value of '" + arg + "' is not an integer" );
	}

	return num;
}



/**
 *  Converts the parameter value to a double
 *
 *  @param arg name of the parameter
 *  @returns double floating point value of the string
 *  @throws ArgException - if arg is unknown or the value isn't a float
 */

double ArgMap::asDouble( const string& arg )
{
	char * end;
	double num;


	if( !m_long.count( arg ) ) {
		throw ArgException( "ArgMap: Undefined parameter '" + arg + "'" );
	}

	num = strtod( m_arg[m_long[arg]].value.c_str(), &end );
	if( *end != '\0' ) {
		throw ArgException( "ArgMap: Value of '" + arg + "' is not a float" );
	}

	return num;
}



/**
 *  Returns the value of the parameter as string
 *
 *  @param arg name of the parameter
 *  @returns string value associated to the parameter
 *  @throws ArgException if arg is unknown
 */

const string& ArgMap::asString( const string& arg )
{
	if( !m_long.count( arg ) ) {
		throw ArgException( "ArgMap: Undefined parameter '" + arg + "'" );
	}

	return m_arg[m_long[arg]].value;
}



/**
 *  Returns a list of all known parameter names
 *
 *   @returns list of parameter names
 */

vector<string> ArgMap::getArgList()
{
	vector<string> list;


	for( size_t i = 0; i < m_arg.size(); i++ ) {
		list.push_back( m_arg[i].longopt );
	}

	return list;
}



/**
 *  Returns a list of all non-options. These items are either from the command
 *  line or from a file and are single values.
 *
 *  @returns list of all items
 */

const vector<string>& ArgMap::getItems()
{
	return m_items;
}



/**
 *  Tests if an parameter is set and returns the value
 *
 *  @param argc number of parameters given in argv
 *  @param argv vector of C strings
 *  @param str name of the parameter you are looking for
 *  @param val reference for storing the parameter value
 *  @returns true if the parameter was found, false otherwise
 */

bool ArgMap::checkArgv( int argc, char* argv[], const string& str, string& val )
{
	val = "";

	for( int i = 1; i < argc; i++ )   // skip program name
	{
		string option( argv[i] );

		switch( this->getOptionType( option ) )
		{
			case ArgMap::Short:

				if( option == str )
				{
					if( i+1 < argc ) { val = string( argv[i+1] ); }
					return true;
				}
				break;

			case ArgMap::Long:

				if( option.substr( 0, str.size() ) == str )
				{
					if( option.size() > str.size() + 1 ) {
						val = option.substr( str.size() + 1, string::npos );
					}
					return true;
				}
				break;
		}
	}

	return false;
}



/**
 *  Parses the command line and seperates parameters from other items
 *
 *  @param argc number of parameters in argv
 *  @param argv vector of C strings
 *  @param strict unknown parameters are handled as error
 *  @throws ArgException if an parameter is unknown in strict mode or an option requires a value
 */

void ArgMap::parseArgv( int argc, char* argv[], bool strict )
{
	string option, value;

	// -c prog.conf --config=prog.conf -- progdir

	for( int pos = 1; pos < argc; pos++ )   // skip program name
	{
		option = string( argv[pos] );

		switch( this->getOptionType( option ) )
		{
			case ArgMap::Item:

				this->parseItem( option, strict );
				break;

			case ArgMap::Delimiter:

				do {
					this->parseItem( string( argv[++pos] ), strict );
				} while( pos < argc );
				break;

			case ArgMap::Short:

				option = string( argv[pos]+1, 1 );
				value = "yes";

				if( m_arg[m_short[option]].req )
				{
					if( pos+1 >= argc ) {
						throw ArgException( "Option '-" + option + "' requires a value" );
					} else {
						value = string( argv[++pos] );
					}
				}

				this->parseShortOption( option, value, strict );
				break;

			case ArgMap::Long:

				this->parseLongOption( string( argv[pos] + 2 ), strict );
				break;

			default:
				throw ArgException( "Invalid option type for '" + option + "'" );
		}
	}
}



/**
 *  Extracts arguments and/or items from a file
 *
 *  @param filename name of the config file
 *  @param strict unknown parameters are handled as error
 *  @throws ArgException if an parameter is unknown in strict mode
 */

void ArgMap::parseFile( const string& filename, bool strict )
{
	string::size_type pos;
	string line, statement;

	std::ifstream file( filename.c_str() );


	if( !file ) {
		throw ArgException( "ArgMap: Can't open file " + filename );
	}

	while( getline( file, line ) )
	{
		if( ( pos = line.find( '#' ) ) != string::npos )
		{
			line.erase( pos, string::npos );
		}

		if( line.find_first_not_of( " \t" ) == string::npos )
		{
			statement = "";
			continue;
		}

		if( ( pos = line.rfind( '\\' ) ) == string::npos )
		{
			this->parseLongOption( statement + line, strict );
			statement = "";
			continue;
		}

		line.erase( pos - 1, string::npos );
		statement += line;
	}

	file.close();
}



/**
 *  Parses the environment variables
 *
 *  @param env environment vector ending by NULL
 *  @param strict unknown parameters are handled as error
 *  @throws ArgException if an parameter is unknown in strict mode
 */

void ArgMap::parseEnv( char* env[], bool strict )
{
	char** i;

	for( i = env; (*i) != NULL; i++ ) {
		this->parseLongOption( string( *i ), strict );
	}
}



/**
 *  Extracts a single item from the string
 *
 *  @param str string containing the item
 */

 void ArgMap::parseItem( const string& str, bool strict )
 {
	string::size_type begin, end;


	begin = str.find_first_not_of( " \t", 0 );
	end = str.find_last_not_of( " \t", string::npos );

	str[begin] == '"' ? begin++ : begin;
	str[end] == '"' ? end-- : end;

	if( begin != string::npos ) {
		m_items.push_back( str.substr( begin, end - begin + 1 ) );
	}
 }



/**
 *  Extracts a single parameter name and value from a string
 *
 *  @param keyvalue pair of name and value seperated by a equal sign
 *  @param strict unknown parameters are handled as error
 *  @throws ArgException if an parameter is unknown in strict mode
 */

void ArgMap::parseLongOption( const string& keyvalue, bool strict )
{
	string::size_type pos, begin, end;
	string name;


	if( ( pos = keyvalue.find( "=" ) ) == string::npos ) {
		parseItem( keyvalue, strict );
		return;
	}

	begin = keyvalue.find_first_not_of( " \t", 0 );
	end = keyvalue.find_last_not_of( " \t", pos - 1 );

	name = keyvalue.substr( begin, end - begin + 1 );

	if( strict && !m_long.count( name ) ) {
		throw ArgException( "ArgMap: Undefined parameter '" + name + "'" );
	}

	begin = keyvalue.find_first_not_of( " \t", pos + 1 );
	end = keyvalue.find_last_not_of( " \t", string::npos );

	keyvalue[begin] == '"' ? begin++ : begin;
	keyvalue[end] == '"' ? end-- : end;

	if( begin != string::npos ) {
		m_arg[m_long[name]].value = keyvalue.substr( begin, end - begin + 1 );
	}
}



/**
 *  Extracts a single parameter name and value
 *
 *  @param name name of the parameter
 *  @param value value of the parameter
 *  @param strict unknown parameters are handled as error
 *  @throws ArgException if an parameter is unknown in strict mode
 */

void ArgMap::parseShortOption( const string& name, const string& value, bool strict )
{
	if( strict && !m_short.count( name ) ) {
		throw ArgException( "ArgMap: Undefined parameter '" + name + "'" );
	}

	m_arg[m_short[name]].value = value;
}



/**
 *  Assembles the help output of all parameters. The output may be limited to
 *  parameters beginning with the prefix
 *
 *  @param prefix string parameters must begin with
 *  @returns assembled help output
 */

string ArgMap::help( const string& prefix )
{
	string help;

	for( size_t i = 0; i < m_arg.size(); i++ )
	{
		if( m_arg[i].longopt.substr( 0, prefix.length() ) == prefix )
		{
			help += "  -" + m_arg[i].shortopt + ", --" + m_arg[i].longopt + ( m_arg[i].req ? "=... (default: '" + m_arg[i].value + "')" : "" ) + "\n";
			help += "        " + m_arg[i].help + "\n";
		}
	}

	return help;
}



/**
 *  Assembles all parameters, values and descriptions to an output suitable for
 *  a config file
 *
 *  @returns string suitable for config file
 */

string ArgMap::config()
{
	string config = "# Autogenerated configuration file template\n\n";

	for( size_t i = 0; i < m_arg.size(); i++ )
	{
		config += "#################################\n";
		config += "# " + m_arg[i].longopt + "\t" + m_arg[i].help + "\n";
		config += "#\n";
		config += "# " + m_arg[i].longopt + ( m_arg[i].req ? "=" + m_arg[i].value : "" ) + "\n\n";
	}

	return config;
}


/**
 * Returns the type of the parameter given by the user.
 *
 * @return Type constant defined in ArgMap
 */
int ArgMap::getOptionType( const string& arg )
{
	if( arg.size() < 2 ) { return ArgMap::Item; }
	if( arg[0] != '-' ) { return ArgMap::Item; }
	if( arg[1] != '-' ) { return ArgMap::Short; }
	if( arg.size() > 2 ) { return ArgMap::Long; }

	return ArgMap::Delimiter;
}