File: options.h

package info (click to toggle)
libwibble 0.1.19
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 832 kB
  • ctags: 1,940
  • sloc: cpp: 9,798; makefile: 163; perl: 84; sh: 11
file content (337 lines) | stat: -rw-r--r-- 8,234 bytes parent folder | download
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
#ifndef WIBBLE_COMMANDLINE_OPTIONS_H
#define WIBBLE_COMMANDLINE_OPTIONS_H

#include <wibble/commandline/core.h>
#include <string>
#include <vector>

namespace wibble {
namespace commandline {

// Types of values for the command line options

struct Bool
{
	typedef bool value_type;
	static bool parse(const std::string& val);

	static bool toBool(const value_type& val);
	static int toInt(const value_type& val);
	static std::string toString(const value_type& val);
};

struct Int
{
	typedef int value_type;
	static int parse(const std::string& val);

	static bool toBool(const value_type& val);
	static int toInt(const value_type& val);
	static std::string toString(const value_type& val);
};

struct String
{
	typedef std::string value_type;
	static std::string parse(const std::string& val);

	static bool toBool(const value_type& val);
	static int toInt(const value_type& val);
	static std::string toString(const value_type& val);
};

struct ExistingFile
{
	typedef std::string value_type;
	static std::string parse(const std::string& val);
	static std::string toString(const value_type& val);
};

/// Interface for a parser for one commandline option
class Option : public Managed
{
	std::string m_name;
	mutable std::string m_fullUsage;

protected:
	bool m_isset;

	Option(const std::string& name) : m_name(name), m_isset(false) {}
	Option(const std::string& name,
			char shortName,
			const std::string& longName,
			const std::string& usage = std::string(),
			const std::string& description = std::string())
		: m_name(name), m_isset(false), usage(usage), description(description)
	{
		if (shortName != 0)
			shortNames.push_back(shortName);
		if (!longName.empty())
			longNames.push_back(longName);
	}

	/**
	 * Parse the next commandline parameter after the short form of the command
	 * has been found.  It may or may not remove the parameter from the list,
	 * depending on if the option wants a value or not.
	 *
	 * Signal that the option has been found, with the given argument (or 0 if
	 * no argument).
	 *
	 * @returns
	 *   true if it used the argument, else false
	 */
	virtual ArgList::iterator parse(ArgList& list, ArgList::iterator begin) = 0;

	/**
	 * Parse the commandline parameter of a long commandline switch
	 *
	 * @returns true if the parameter has been used
	 */
	virtual bool parse(const std::string& param) = 0;

public:
	Option();
	virtual ~Option() {}

	const bool isSet() const { return m_isset; }
	const std::string& name() const { return m_name; }

	void addAlias(char c) { shortNames.push_back(c); }
	void addAlias(const std::string& str) { longNames.push_back(str); }

	/// Return a full usage message including all the aliases for this option
	const std::string& fullUsage() const;
	std::string fullUsageForMan() const;

	std::vector<char> shortNames;
	std::vector<std::string> longNames;

	std::string usage;
	std::string description;

	// Set to true if the option should not be documented
	bool hidden;

	friend class OptionGroup;
	friend class Engine;
};

/// Boolean option
class BoolOption : public Option
{
	bool m_value;

protected:
	BoolOption(const std::string& name)
		: Option(name), m_value(false) {}
	BoolOption(const std::string& name,
			char shortName,
			const std::string& longName,
			const std::string& usage = std::string(),
			const std::string& description = std::string())
		: Option(name, shortName, longName, usage, description), m_value(false) {}

	virtual ArgList::iterator parse(ArgList&, ArgList::iterator begin) { m_isset = true; m_value = true; return begin; }
	virtual bool parse(const std::string&) { m_isset = true; m_value = true; return false; }

public:
	bool boolValue() const { return m_value; }
	std::string stringValue() const { return m_value ? "true" : "false"; }

	friend class OptionGroup;
	friend class Engine;
};

template<typename T>
class SingleOption : public Option
{
protected:
	typename T::value_type m_value;

	SingleOption(const std::string& name)
		: Option(name)
	{
		usage = "<val>";
	}
	SingleOption(const std::string& name,
			char shortName,
			const std::string& longName,
			const std::string& usage = std::string(),
			const std::string& description = std::string())
		: Option(name, shortName, longName, usage, description)
	{
		if (usage.empty())
			this->usage = "<val>";
	}

	ArgList::iterator parse(ArgList& list, ArgList::iterator begin)
	{
		if (begin == list.end())
			throw exception::BadOption("no string argument found");
		m_value = T::parse(*begin);
		m_isset = true;
		// Remove the parsed element
		return list.eraseAndAdvance(begin);
	}
	bool parse(const std::string& param)
	{
		m_value = T::parse(param);
		m_isset = true;
		return true;
	}

public:
        void setValue( const typename T::value_type &a ) {
            m_value = a;
        }

	typename T::value_type value() const { return m_value; }

	// Deprecated
	bool boolValue() const { return T::toBool(m_value); }
	int intValue() const { return T::toInt(m_value); }
	std::string stringValue() const { return T::toString(m_value); }

	friend class OptionGroup;
	friend class Engine;
};

// Option needing a compulsory string value
typedef SingleOption<String> StringOption;

// Option needing a compulsory int value
struct IntOption : public SingleOption<Int>
{
protected:
	IntOption(const std::string& name) : SingleOption<Int>(name)
	{
		m_value = 0;
	}
	IntOption(const std::string& name,
			char shortName,
			const std::string& longName,
			const std::string& usage = std::string(),
			const std::string& description = std::string())
		: SingleOption<Int>(name, shortName, longName, usage, description)
	{
		m_value = 0;
	}
public:
	friend class OptionGroup;
	friend class Engine;
};

/**
 * Commandline option with a mandatory argument naming a file which must exist.
 */
typedef SingleOption<ExistingFile> ExistingFileOption;


// Option that can be specified multiple times
template<typename T>
class VectorOption : public Option
{
	std::vector< typename T::value_type > m_values;

protected:
	VectorOption(const std::string& name)
		: Option(name)
	{
		usage = "<val>";
	}
	VectorOption(const std::string& name,
			char shortName,
			const std::string& longName,
			const std::string& usage = std::string(),
			const std::string& description = std::string())
		: Option(name, shortName, longName, usage, description)
	{
		if (usage.empty())
			this->usage = "<val>";
	}

	ArgList::iterator parse(ArgList& list, ArgList::iterator begin)
	{
		if (begin == list.end())
			throw exception::BadOption("no string argument found");
		m_isset = true;
		m_values.push_back(T::parse(*begin));
		// Remove the parsed element
		return list.eraseAndAdvance(begin);
	}
	bool parse(const std::string& param)
	{
		m_isset = true;
		m_values.push_back(T::parse(param));
		return true;
	}

public:
	bool boolValue() const { return !m_values.empty(); }
	const std::vector< typename T::value_type >& values() const { return m_values; }

	friend class OptionGroup;
	friend class Engine;
};


/**
 * Group related commandline options
 */
class OptionGroup : public Managed
{
	MemoryManager* m_manager;

protected:
	OptionGroup(MemoryManager* mman = 0, const std::string& description = std::string())
		: m_manager(mman), description(description), hidden(false) {}

public:
	Option* add(Option* o) { options.push_back(o); return o; }

	std::vector<Option*> options;

	std::string description;

	// Set to true if the option group should not be documented
	bool hidden;

	/**
	 * Create a new option
	 */
	template<typename T>
	T* create(const std::string& name,
			char shortName,
			const std::string& longName,
			const std::string& usage = std::string(),
			const std::string& description = std::string())
	{
		T* item = new T(name, shortName, longName, usage, description);
		if (m_manager) m_manager->add(item);
		return item;
	}

	/**
	 * Create a new option and add it to this group
	 */
	template<typename T>
	T* add(const std::string& name,
			char shortName,
			const std::string& longName,
			const std::string& usage = std::string(),
			const std::string& description = std::string())
	{
		T* res = create<T>(name, shortName, longName, usage, description);
		add(res);
		return res;
	}

	friend class Engine;
};

}
}

// vim:set ts=4 sw=4:
#endif