Mercurial > defical
comparison defical-c/src/basic/Argument_helper.h @ 0:ebed2bd0d300
Initial import from svn. History be damned.
author | Edho P. Arief <me@myconan.net> |
---|---|
date | Fri, 02 Apr 2010 23:11:57 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ebed2bd0d300 |
---|---|
1 /* | |
2 * | |
3 * Argument Helper | |
4 * | |
5 * Daniel Russel drussel@alumni.princeton.edu | |
6 * Stanford University | |
7 * | |
8 * | |
9 * This software is not subject to copyright protection and is in the | |
10 * public domain. Neither Stanford nor the author assume any | |
11 * responsibility whatsoever for its use by other parties, and makes no | |
12 * guarantees, expressed or implied, about its quality, reliability, or | |
13 * any other characteristic. | |
14 * | |
15 */ | |
16 #ifdef _MSC_VER | |
17 #define my_sscanf sscanf_s | |
18 #else | |
19 #define my_sscanf sscanf | |
20 #endif | |
21 #include <sstream> | |
22 | |
23 #ifndef _DSR_ARGS_H_ | |
24 #define _DSR_ARGS_H_ | |
25 #include <vector> | |
26 #include <map> | |
27 #include <list> | |
28 #include <string> | |
29 #include <string.h> | |
30 | |
31 namespace dsr{ | |
32 extern bool verbose, VERBOSE; | |
33 | |
34 | |
35 class Argument_helper{ | |
36 private: | |
37 class Argument_target; | |
38 | |
39 | |
40 class FlagTarget; | |
41 class DoubleTarget; | |
42 class IntTarget; | |
43 class UIntTarget; | |
44 class StringTarget; | |
45 class CharTarget; | |
46 class StringVectorTarget; | |
47 | |
48 public: | |
49 Argument_helper(); | |
50 void new_flag(char key, const char *long_name, const char *description,bool &dest); | |
51 | |
52 void new_string( const char *arg_description, const char *description, std::string &dest); | |
53 void new_named_string(char key, const char *long_name, | |
54 const char *arg_description, | |
55 const char *description, std::string &dest); | |
56 void new_optional_string( const char *arg_description, const char *description, std::string &dest); | |
57 | |
58 void new_int( const char *arg_description, const char *description, int &dest); | |
59 void new_named_int(char key, const char *long_name,const char *value_name, | |
60 const char *description, | |
61 int &dest); | |
62 void new_optional_int(const char *value_name, | |
63 const char *description, | |
64 int &dest); | |
65 | |
66 void new_double(const char *value_name, | |
67 const char *description, | |
68 double &dest); | |
69 | |
70 void new_named_double(char key, const char *long_name,const char *value_name, | |
71 const char *description, | |
72 double &dest); | |
73 void new_optional_double(const char *value_name, | |
74 const char *description, | |
75 double &dest); | |
76 | |
77 void new_char(const char *value_name, | |
78 const char *description, | |
79 char &dest); | |
80 void new_named_char(char key, const char *long_name,const char *value_name, | |
81 const char *description, | |
82 char &dest); | |
83 void new_optional_char(const char *value_name, | |
84 const char *description, | |
85 char &dest); | |
86 | |
87 void new_unsigned_int(const char *value_name, const char *description, | |
88 unsigned int &dest); | |
89 void new_optional_unsigned_int(const char *value_name, const char *description, | |
90 unsigned int &dest); | |
91 void new_named_unsigned_int(char key, const char *long_name, | |
92 const char *value_name, const char *description, | |
93 unsigned int &dest); | |
94 | |
95 | |
96 | |
97 void new_named_string_vector(char key, const char *long_name, | |
98 const char *value_name, const char *description, | |
99 std::vector<std::string> &dest); | |
100 | |
101 | |
102 void set_string_vector(const char *arg_description, const char *description, std::vector<std::string> &dest); | |
103 | |
104 void set_author(const char *author); | |
105 | |
106 void set_description(const char *descr); | |
107 | |
108 void set_version(float v); | |
109 void set_version(const char *str); | |
110 | |
111 void set_name(const char *name); | |
112 | |
113 void set_build_date(const char *date); | |
114 | |
115 | |
116 void process(int argc, const char **argv); | |
117 void process(int argc, char **argv){ | |
118 process(argc, const_cast<const char **>(argv)); | |
119 } | |
120 void write_usage(std::ostream &out) const; | |
121 void write_values(std::ostream &out) const; | |
122 | |
123 ~Argument_helper(); | |
124 protected: | |
125 typedef std::map<char, Argument_target*> SMap; | |
126 typedef std::map<std::string, Argument_target*> LMap; | |
127 typedef std::vector<Argument_target*> UVect; | |
128 // A map from short names to arguments. | |
129 SMap short_names_; | |
130 // A map from long names to arguments. | |
131 LMap long_names_; | |
132 std::string author_; | |
133 std::string name_; | |
134 std::string description_; | |
135 std::string date_; | |
136 float version_; | |
137 bool seen_end_named_; | |
138 // List of unnamed arguments | |
139 std::vector<Argument_target*> unnamed_arguments_; | |
140 std::vector<Argument_target*> optional_unnamed_arguments_; | |
141 std::vector<Argument_target*> all_arguments_; | |
142 std::string extra_arguments_descr_; | |
143 std::string extra_arguments_arg_descr_; | |
144 std::vector<std::string> *extra_arguments_; | |
145 std::vector<Argument_target*>::iterator current_unnamed_; | |
146 std::vector<Argument_target*>::iterator current_optional_unnamed_; | |
147 void new_argument_target(Argument_target*); | |
148 void handle_error() const; | |
149 private: | |
150 Argument_helper(const Argument_helper &){}; | |
151 const Argument_helper& operator=(const Argument_helper &){return *this;} | |
152 }; | |
153 | |
154 } | |
155 | |
156 #define ARGUMENT_HELPER_BASICS(ah) ah.set_author("Daniel Russel, drussel@stanford.edu");\ | |
157 ah.set_version(VERSION);\ | |
158 ah.set_build_date(__DATE__); | |
159 | |
160 #endif | |
161 |