File: CmdLineOpts.h

package info (click to toggle)
libassa 3.5.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,408 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 380; perl: 51
file content (317 lines) | stat: -rw-r--r-- 8,928 bytes parent folder | download | duplicates (3)
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
// -*- c++ -*-
//------------------------------------------------------------------------------
// $Id: CmdLineOpts.h,v 1.7 2005/10/12 02:28:58 vlg Exp $
//------------------------------------------------------------------------------
//                             CmdLineOpts.h
//------------------------------------------------------------------------------
//  Copyright (C) 2000,2005  Vladislav Grinchenko 
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------

#ifndef CMD_LINE_OPTS_H
#define CMD_LINE_OPTS_H

#include "assa/Assure.h"

#include <string> 
#include <vector>
using std::string;
using std::vector;

namespace ASSA {

class CmdLineOpts; 

/** @file CmdLineOpts.h

 	Class to handle processing command-line options.
 */

/** 
 * Option class. This class is a helper class of CmdLineOpts class. 
 * It is not used by any other class and cannot be instantiated.
 */

class Option {
public:
	friend class CmdLineOpts; 

	/** @enum type_t
		Option type. Each option, except for flags has a value
		following it on the command line. Following types are supported:
	*/
	enum type_t { 
		string_t=0,	/**< Convert argument to STL string  */
		int_t,		/**< Convert argument to int  */
		uint_t,		/**< Convert argument to unsigned int  */
		long_t,		/**< Convert argument to long  */
		ulong_t,	/**< Convert argument to unsigned long  */
		double_t,	/**< Convert argument to double  */
		float_t,	/**< Convert argument to float  */
		flag_t,		/**< No argument; bool value is flipped.  */
		func_t,		/**< Convert argument to function  */
		func_one_t,	/**< Convert argument to function with one argument  */
		none_t					
	};

private:
	/// Private default constructor
	Option ();

	/// Private constructor
	Option (char shopt_, const string& lopt_, type_t type_, void* val_);

	/// Write object state to the log file
	void dump () const;

	/// Return the type of the Option object
	const char* type_c_str ();

private:
	/// One-letter option name
	char    m_short_name;

	/// Long option name
	string  m_long_name;

	/// Option type
	type_t  m_type;

	/// Pointer to the option value
	void*   m_val;
};

inline 
Option::Option () :
	m_short_name (' '), m_long_name (""), m_type (none_t), m_val (NULL) 
{
	/* empty */
}

inline
Option::Option (char shopt_, const string& lopt_, type_t type_, void* val_) : 
	m_short_name (shopt_), 	m_long_name (lopt_),
	m_type (type_), m_val (val_) 
{
	trace_with_mask("Option::Option", CMDLINEOPTS);
}

/*----------------------------------------------------------------------------*/
class IniFile;

/** Class CmdLineOpts.

CmdLineOpts class parsers the command line arguments. It is a base class, 
and to use it, it has to be inherited from. See "ASSA Library User's 
Guide" for further details.
*/

class CmdLineOpts
{
public:
	typedef void (* OPTS_FUNC) (void);
	typedef void (* OPTS_FUNC_ONE) (const string&);

	typedef vector<Option> OptionSet;

	/// Default constructor
	CmdLineOpts ();

	/// Do-nothing destructor
	virtual ~CmdLineOpts () { 
		trace_with_mask ("CmdLineOpts::~CmdLineOpts", CMDLINEOPTS); 
	}

	/** Add binary flag option. 
	    @param c  short name
	    @param s  long name
	    @param f  pointer to bool flag variable
	    @return true on success, false on error
	*/
	bool add_flag_opt (const char c, const string& s, bool* f);

	/** Add an option with STL string argument.
	    @param c  short name
	    @param s  long name
	    @param str  pointer to string variable
	    @return true on success, false on error
	*/
	bool add_opt (const char c, const string& s, string* str);

	/** Add an option with integer argument.
	    @param c  short name
	    @param s  long name
	    @param i  pointer to int variable
	    @return true on success, false on error
	*/
	bool add_opt (const char c, const string& s, int* i);

	/** Add an option with unsigned integer argument.
	    @param c  short name
	    @param s  long name
	    @param ui  pointer to u_int variable
	    @return true on success, false on error
	*/
	bool add_opt (const char c, const string& s, unsigned int* ui);

	/** Add an option with long argument.
	    @param c  short name
	    @param s  long name
	    @param l  pointer to long variable
	    @return true on success, false on error
	*/
	bool add_opt (const char c, const string& s, long* l);

	/** Add an option with unsigned long argument.
	    @param c  short name
	    @param s  long name
	    @param ul  pointer to unsigned long variable
	    @return true on success, false on error
	*/
	bool add_opt (const char c, const string& s, unsigned long* ul);

	/** Add an option with double argument.
	    @param c  short name
	    @param s  long name
	    @param d  pointer to double variable
	    @return true on success, false on error
	*/
	bool add_opt (const char c, const string& s, double* d);

	/** Add an option with float argument.
	    @param c  short name
	    @param s  long name
	    @param f  pointer to float variable
	    @return true on success, false on error
	*/
	bool add_opt (const char c, const string& s, float* f);

	/** Add an option with static function argument.
	    This void function with no arguments will be called 
	    when command line option is processed. An option installed
	    is treated as binary flag option.

	    @param c_  short name
	    @param s_  long name
	    @param f_  pointer to the static function

	    @return true on success, false on error
	*/
	bool add_opt (const char c_, const string& s_, OPTS_FUNC f_);

	/** Add an option with static function argument.
	    This void function with STL string arguments will be called 
	    when command line option is processed. The option value is
	    delivered via function's argument.

	    @param c_  short name
	    @param s_  long name
	    @param f_  pointer to the static function
	    @return true on success, false on error
	*/
	bool add_opt (const char c_, const string& s_, OPTS_FUNC_ONE f_);

	/** Remove option for the option list.
	    @param c_ short name
	    @param s_ long name
	    @return true if found, false if not
	*/
	bool rm_opt (const char c_, const string& s_);

	/** Parse command line arguments based on installed options set.
	    @return true on success, false on error.
	*/
	bool parse_args (const char* argv[]);

	/** Parse configuration parameters found in [options] section
		of the INI file. File should be already loaded with load().

		@param inifile_ The INI file to parse.
		@return The number of options parsed or -1 if [options] section
		        is missing
	*/
	int parse_config_file (IniFile& inifile_);

	/** If previous call to one of member functions returned false,
	    retrieve detailed error message.
	*/
	const char* get_opt_error () const;

	/** Static function. Convert string list of command line options
	    into dynamically allocated argv-like array. The array is 
	    terminated with NULL. This memory must be freed after it has 
	    been used. Remember that the first parameter is process name.
	    @param src_  command line option string
	    @param argc_ number of options found in the source string
	    @param argv_ returns a pointer to the heap-allocated memory
	*/
	static void str_to_argv (const string& src_, int&  argc_, char**& argv_);

	/** Free up memory allocated by <TT>str_to_argv()</TT> function	 */
	static void free_argv (char**& argv_);

	/// Write options set to the log file.
	void dump () const;

protected:
	/// Detect if supplied option is valid
	bool is_valid (const char sopt_, const string& lopt_);
	
	/// Reset error message to an empty string
	void set_error_none ();

	/** Perform value assignment to the node. Data conversion  happens here. */
	bool assign (Option* node_, const char* op_);

	/// Locate option in the options set
	Option* find_option (const char* str_);

	/// Locate option in the options set
	Option* find_option (const char letter_);

	/** Process positional argument arg_. This method must be
	    overloaded by the derived class to take advantage of it.
	    @param arg_ positional argument value
	*/
	virtual void pos_arg (const char* arg_);

private:
	/// Options set
	OptionSet m_opts_set;

	/// Last reported error
	string    m_error;
};


inline void 
CmdLineOpts::pos_arg (const char* /*arg_*/) { /* no-opt*/ }
	
inline
CmdLineOpts::CmdLineOpts () : m_opts_set (), m_error ("")
{
	trace_with_mask("CmdLineOpts::CmdLineOpts", CMDLINEOPTS);
	set_error_none ();
}

inline void
CmdLineOpts::set_error_none ()
{
	trace_with_mask("CmdLineOpts::set_error_none", CMDLINEOPTS);
	m_error = "";
}

inline const char*
CmdLineOpts::get_opt_error () const
{
	return (m_error.c_str ());
}


} // end namespace ASSA

#endif /* CMD_LINE_OPTS_H */