Inshaping: include/anyarg.h Source File

Inshaping

Inshaping  0.1
anyarg.h
Go to the documentation of this file.
1 
38 /*
39 A single letter option begins with a hyphen '-'. The parsing of single letter options follows POSIX conventions.
40 
41 A long option begins with two hyphens '--'. The parsing of long options follows GNU conventions.
42 
43 Only specify/define a option once, otherwise you will incurr an error.
44 
45 Example:
46 Supposing program \c foo has two flags (-a -all, -v --verbose) and two taking-value options (-s
47 --buffer-size, -n), you can turn on flag a and v, set option s to 100 and n to 50, and pass another two
48 non-option arguments (abc and xyz) to foo by:
49 
50 foo -av -s 100 -n 50 abc xyz foo -av
51 --buffer-size=100 -n 50 abc xyz
52 foo --all --verbose --buffer-size=100 -n 50 abc xyz
53 
54 POSIX conventions:
55 http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
56 
57 GNU extensions:
58 http://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html
59 */
60 
61 
62 #ifndef __ANYARG_H__
63 #define __ANYARG_H__
64 
65 #include <string>
66 #include <vector>
67 #include <cstdlib>
68 #include <cstring>
69 
70 using std::string;
71 using std::vector;
72 
73 
75 struct Option
76 {
77  char type; // type of option
78  char letter; // single-letter label of an option
79  string name; // long name of an option
80 // char valtype; // value type, [BSID], bool: B, string: S, int: I, double: D
81  string value_str; // option value as a string
82  union { // option value as a bool, int or double value
83  bool value_bool;
84  int value_int;
85  double value_double;
86  };
87  string desc; // a short sentence to describe an option
88  string meta; // a META word to specify the type of option argument, such as "FILE", "SIZE"
89 
90  Option():type(0), letter(), name(), value_str(), value_double(0.0), desc(), meta() {}
91  bool set_value(const char *opt_value);
92  bool set_desc_meta(const char *opt_desc);
93 };
94 
95 
97 class Anyarg
98 {
99  public:
101  Anyarg();
102 
104 
112  bool add_flag(const char *name, char letter, const char *desc);
113  bool add_flag(char letter, const char *desc);
115 
117 
127  // Add an option with a string value
128  bool add_option_str(const char *name, char letter, const char *v0, const char *desc);
129  bool add_option_str(char letter, const char *v0, const char *desc);
130 
131  // Add an option with an integer value
132  bool add_option_int(char letter, int v0, const char *desc);
133  bool add_option_int(const char *name, char letter, int v0, const char *desc);
134 
135  // Add an option with an double value
136  bool add_option_double(char letter, double v0, const char *desc);
137  bool add_option_double(const char *name, char letter, double v0, const char *desc);
139 
148  bool parse_argv(int argc, char **argv);
149 
151 
157  bool is_true(const char *name) const;
158  bool is_true(char letter) const;
160 
162 
170  // Get the value of an option with string values
171  const char *get_value_str(const char *name) const;
172  const char *get_value_str(char letter) const;
173 
174  // Get the value of an option with integer values
175  int get_value_int(const char *name) const;
176  int get_value_int(char letter) const;
177 
178  // Get the value of an option with double values
179  double get_value_double(const char *name) const;
180  double get_value_double(char letter) const;
182 
187  int get_argc() const;
188 
194  const char *get_arg(int i) const;
195 
200  const char *auto_usage();
201 
202  private:
203  string prog_name_; // name of the program
204  int argc_; // count of non-option arguments
205  vector<string> argv_; // vector of non-option arguments
206 
207  vector<Option> options_; // vector of options
208 
209  string help_; // formatted help for options
210 
211  Anyarg(const Anyarg &); // prevent the copy of a anyarg object
212 
213  Anyarg & operator = (const Anyarg &); // prevent assignment of anyarg object
214 
215  bool is_new_option(const char *name, char letter);
216 
217  int get_optind(char letter) const;
218 
219  int get_optind(const char *name) const;
220 
221  // Show information of all options, for debugging.
222  void show_options() const;
223 };
224 
225 #endif
bool add_option_int(char letter, int v0, const char *desc)
int value_int
Definition: anyarg.h:84
bool add_option_str(const char *name, char letter, const char *v0, const char *desc)
Option()
Definition: anyarg.h:90
double get_value_double(const char *name) const
bool value_bool
Definition: anyarg.h:83
int get_value_int(const char *name) const
bool add_flag(const char *name, char letter, const char *desc)
string value_str
Definition: anyarg.h:81
int get_argc() const
const char * auto_usage()
bool set_value(const char *opt_value)
string desc
Definition: anyarg.h:87
bool add_option_double(char letter, double v0, const char *desc)
bool parse_argv(int argc, char **argv)
Data structure of a program option.
Definition: anyarg.h:75
double value_double
Definition: anyarg.h:85
char type
Definition: anyarg.h:77
string name
Definition: anyarg.h:79
Anyarg()
Construct a Anyarg object.
const char * get_arg(int i) const
string meta
Definition: anyarg.h:88
Use this class to define program options and parse command line arguments.
Definition: anyarg.h:97
char letter
Definition: anyarg.h:78
bool set_desc_meta(const char *opt_desc)
bool is_true(const char *name) const
const char * get_value_str(const char *name) const
Generated by   doxygen 1.8.14