CUBA
cmd.hh
1 
8 #ifndef CMD_HH_
9 #define CMD_HH_
10 
11 #include <cstdlib>
12 #include <iostream>
13 #include <ostream>
14 #include <sstream>
15 #include <string>
16 #include <map>
17 #include <deque>
18 #include <vector>
19 #include <algorithm>
20 #include <stdexcept>
21 
22 using std::runtime_error;
23 using std::ostream;
24 using std::ostringstream;
25 using std::string;
26 using std::map;
27 using std::deque;
28 using std::vector;
29 using std::cout;
30 using std::endl;
31 
32 extern string v_info;
33 
34 using ushort = unsigned short;
35 using ulong = unsigned long int;
36 
37 namespace cmd {
38 
39 enum class alignment {
40  LEFTJUST, RIGHTJUST, CENTERED
41 };
42 
43 template<class T> string widthify(const T& x, const ushort& width = 0,
44  const alignment& c = alignment::CENTERED, const char& fill = ' ');
45 
49 class cmd_runtime_error: public runtime_error {
50 public:
52  runtime_error("") {
53  }
54 
55  cmd_runtime_error(const std::string& msg) :
56  runtime_error(msg) {
57  }
58 };
59 
60 class Arguments {
61 public:
62  inline Arguments(const short& type, const string& short_name,
63  const string& long_name, const string& meaning) :
64  type(type), short_name(short_name), long_name(long_name), meaning(
65  meaning) {
66  }
67 
68  inline virtual ~Arguments() {
69  }
70 
71  inline const short& get_type() const {
72  return type;
73  }
74 
75  inline const string& get_long_name() const {
76  return long_name;
77  }
78 
79  inline const string& get_meaning() const {
80  return meaning;
81  }
82 
83  inline const string& get_short_name() const {
84  return short_name;
85  }
86 
87 protected:
88  const short type;
89  const string short_name;
90  const string long_name;
91  const string meaning;
92 };
93 
94 inline bool operator==(const Arguments& arg1, const Arguments& arg2) {
95  return (arg1.get_long_name() == arg2.get_long_name());
96 }
97 
98 inline bool operator<(const Arguments& arg1, const Arguments& arg2) {
99  return (arg1.get_long_name() < arg2.get_long_name());
100 }
101 
102 class Options: public Arguments {
103 public:
104  inline Options(const short& type, const string& short_name,
105  const string& long_name, const string& meaning,
106  const string& default_value) :
107  Arguments(type, short_name, long_name, meaning), value(
108  default_value) {
109  }
110 
111  inline Options(const short& type = 0, const string& long_name = "") :
112  Arguments(type, "", long_name, "") {
113  }
114 
115  inline virtual ~Options() {
116  }
117 
118  inline const string& get_value() const {
119  return value;
120  }
121 
122  inline void set_value(const string& value) {
123  this->value = value;
124  }
125 
126 private:
127  string value;
128 };
129 
130 inline bool operator==(const Options& arg1, const Options& arg2) {
131  return (arg1.get_long_name() == arg2.get_long_name());
132 }
133 
134 inline bool operator<(const Options& arg1, const Options& arg2) {
135  return (arg1.get_long_name() < arg2.get_long_name());
136 }
137 
138 class Switch: public Arguments {
139 public:
140  inline Switch(const short& type, const string& short_name,
141  const string& long_name, const string& meaning) :
142  Arguments(type, short_name, long_name, meaning), value(false) {
143  }
144 
145  inline Switch(const short& type = 0, const string& long_name = "") :
146  Arguments(type, "", long_name, ""), value(false) {
147  }
148 
149  inline virtual ~Switch() {
150 
151  }
152 
153  bool is_value() const {
154  return value;
155  }
156 
157  void set_value(bool value) {
158  this->value = value;
159  }
160 
161 private:
162  bool value;
163 };
164 
165 inline bool operator==(const Switch& arg1, const Switch& arg2) {
166  return (arg1.get_long_name() == arg2.get_long_name());
167 }
168 
169 inline bool operator<(const Switch& arg1, const Switch& arg2) {
170  return (arg1.get_long_name() < arg2.get_long_name());
171 }
172 
173 enum class opt_type {
174  DEFAULT, PROB, SEQ, CON, OTHER
175 };
176 
178 class cmd_line {
179 public:
180  cmd_line();
181  ~cmd_line();
182 
183  struct help {
184  };
185 
186  void get_command_line(const int argc, const char* const * const argv);
187  bool arg_bool(const unsigned short& type, const string& arg);
188  string arg_value(const short& type, const string& arg);
189 
190  const vector<string>& get_types() const {
191  return types;
192  }
193 
194  void set_types(const vector<string>& types) {
195  this->types = types;
196  }
197 
198  static string get_opt_types(const opt_type& opt);
199 
200  static short get_opt_index(const opt_type& opt);
201 
202 private:
203  string SHORT_HELP_OPT;
204  string LONG_HELP_OPT;
205  string SHORT_VERSION_OPT;
206  string LONG_VERSION_OPT;
207 
208  string VERSION;
209 
210  ushort name_width;
211  ushort xwidth;
212  const string help_message;
213  string v_info;
214 
215  map<short, deque<Options>> cmd_options;
216  map<short, deque<Switch>> cmd_switches;
217  vector<string> types;
218 
219  void create_argument_list();
220  string create_version_info();
221 
222  void add_option(const short& type, const string& short_name,
223  const string& long_name, const string& meaning,
224  const string& default_value);
225 
226  void add_switch(const short& type, const string& short_name,
227  const string& long_name, const string& meaning);
228 
229  void get_command_line(const string& prog, const vector<string>& args);
230 
231  void print_usage_info(const string& prog_name, const ushort& indent = 1,
232  ostream& out = cout) const;
233 };
234 }
235 #endif /* CMD_HH_ */
bool arg_bool(const unsigned short &type, const string &arg)
return bool value
Definition: cmd.cc:219
Definition: cmd.cc:10
customized runtime_error class for command line class
Definition: cmd.hh:49
void get_command_line(const int argc, const char *const *const argv)
Definition: cmd.cc:110
Definition: cmd.hh:183
string arg_value(const short &type, const string &arg)
return the value of argument
Definition: cmd.cc:234
static short get_opt_index(const opt_type &opt)
Definition: cmd.cc:75
Definition: cmd.hh:60
Definition: cmd.hh:102
const string meaning
the argument&#39;s short name
Definition: cmd.hh:91
Definition: cmd.hh:138
const string long_name
the argument&#39;s long name
Definition: cmd.hh:90
Definition: cmd.hh:178
string widthify(const T &x, const ushort &width, const alignment &c, const char &fill)
Definition: cmd.cc:21
static string get_opt_types(const opt_type &opt)
Definition: cmd.cc:56