1 #ifndef __GNUC__
2 #pragma once
3 #endif
4 #ifndef __CL_PARSER_H__
5 #define __CL_PARSER_H__
6 
7 #include <string>
8 
9 // depends on the argv persistence.
10 class cl_parser {
11 public:
12 	enum option_type {
13 		OT_STRING,
14 		OT_BOOL,
15 		OT_FLOAT,
16 		OT_INTEGER,
17 		OT_PARAMETER,
18 	};
19 	struct option_desc {
20 		const char*	name;
21 		option_type	type;
22 	};
23 
24 			cl_parser();
25 			~cl_parser();
26 
27 	bool		parse(int argc, const char* const argv[],
28 					int optc, const option_desc* optv);
29 	bool		get_bool(const char* name) const;
30 	bool		get_string(const char* name, const char*& value) const;
31 	bool		get_string(const char* name, std::string& value) const;
32 	bool		get_float(const char* name, float& value) const;
33 	bool		get_integer(const char* name, int& value) const;
34 	bool		exist(const char* name) const;
35 
36 	const char*	param(size_t index) const;
37 	size_t		num_options() const;
38 	size_t		num_params() const;
39 
40 private:
41 	struct option {
42 		const char*	name;
43 		option_type	type;
44 		union {
45 			const char*	v_string;
46 			int		v_integer;
47 			float		v_float;
48 			bool		v_bool;
49 		};
50 	};
51 
52 	const option*	find_option(const char* name) const;
53 
54 private:
55 	option*		m_options;
56 	size_t		m_num_options;
57 	size_t		m_num_params;
58 	size_t		m_argc;
59 };
60 
param(size_t index)61 inline const char* cl_parser::param(size_t index) const { return m_options[m_argc - index - 1].name; }
62 
num_options()63 inline size_t cl_parser::num_options() const { return m_num_options; }
num_params()64 inline size_t cl_parser::num_params() const { return m_num_params; }
65 
exist(const char * name)66 inline bool cl_parser::exist(const char* name) const { return find_option(name) != 0; }
67 
68 #endif
69