vul_arg.h
Go to the documentation of this file.
1 // This is core/vul/vul_arg.h
2 #ifndef vul_arg_h_
3 #define vul_arg_h_
4 //:
5 // \file
6 // \brief Command-line arguments
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 05 Feb 98
9 //
10 // \verbatim
11 // Modifications
12 // PDA (Manchester) 21/03/2001: Tidied up the documentation
13 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
14 // \endverbatim
15 
16 #include <iosfwd>
17 #include <list>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 #ifdef _MSC_VER
22 # include <vcl_msvc_warnings.h>
23 #endif
24 #include <vul/vul_export.h>
25 #include <vxl_config.h>
26 
27 //: forward declare all classes and their helper functions.
28 class vul_arg_info_list;
29 template <class T> class vul_arg;
30 template <class T> void settype (vul_arg<T> &);
31 template <class T> void print_value (std::ostream &, vul_arg<T> const &);
32 template <class T> int parse (vul_arg<T>*, char**);
33 
34 //: This is the base class for the templated vul_arg<T>s
36 {
37  protected:
38  struct required_option_type {}; // see constructors of vul_arg
39 
40  public:
41  static VUL_EXPORT required_option_type is_required;
42 
43  static void parse_deprecated(int& argc, char **& argv,
44  bool warn_about_unrecognized_arguments = true);
45  static void include_deprecated(vul_arg_info_list& l);
46 
47  static void add_to_current(vul_arg_base* a);
48  static void set_help_option( char const*str);
49  static void set_help_description( char const*str);
50  static void set_help_precis( char const*str);
51  static void display_usage(char const* msg = nullptr);
52  static void display_usage_and_exit(char const* msg = nullptr);
53 
54  friend class vul_arg_info_list;
55 
56  char const* option();
57  char const* help();
58 
59  //: Returns true if arg was set on the command line.
60  bool set() const;
61 
62  virtual std::ostream& print_value(std::ostream&) = 0;
63 
64  public: // Avoid errors on some compilers that don't follow
65  // protected: directive correctly with type_
66 
67  //: Static text describing type of option (e.g. bool or double).
68  char const *type_;
69  protected:
70  //: After parsing, true iff value was set on command line.
71  bool set_;
72  //: if true, this flag must be set on command line.
73  bool required_;
74  //: Option flag including "-" or "--".
75  std::string option_;
76  //: Description of argument.
77  std::string help_;
78 
79  vul_arg_base(vul_arg_info_list& l, char const* option_string,
80  char const*helpstring, bool required= false);
81  vul_arg_base(char const* option_string, char const*helpstring, bool required= false);
82  virtual ~vul_arg_base() = default;
83 
84  virtual int parse(char ** argv) = 0;
85 };
86 
87 //: parse command-line arguments
88 // vul_arg_parse simplifies the parsing of command-line arguments by combining
89 // the variables with the option specifications. To get a variable, you
90 // simply name it along with its flag, a help string, and an optional
91 // default value:
92 // \code
93 // vul_arg<double> threshold("-t", "Intensity threshold", 1.25);
94 // \endcode
95 // Repeat this for any other arguments and then ask the base class to parse
96 // the lot:
97 // \code
98 // vul_arg_parse(argc,argv);
99 // \endcode
100 //
101 // Now parameters such as threshold above can be referred to and will have
102 // either the default value or the one supplied on the command line.
103 //
104 // The big design decision here was whether or not the args should collect
105 // themselves into a global pool, so that the static vul_arg_base::parse can
106 // find them, or whether there should be a local argPool which is passed to
107 // each arg in order that it may add itself. That would give a syntax like
108 // \code
109 // vul_arg_info_list args;
110 // vul_arg<double> threshold(args, "-t", 1.25);
111 // ^^^^^ passing args in
112 // args.parse(argc, argv, true);
113 // \endcode
114 // The latter is "better" but the former is easier to use so I chose it.
115 //
116 // Added by Geoff: call to vul_arg_base::set_help_option("-?") means that a
117 // program call with something like aprog -? will display usage info derived
118 // from the argument list. Note: default is -? but can be anything.
119 //
120 void vul_arg_parse(int& argc, char **& argv,
121  bool warn_about_unrecognized_arguments = true);
122 
123 //: Add an externally supplied list of args to the global list.
125 
126 //: Print all args, and usage messages.
127 void vul_arg_display_usage_and_exit(char const* msg = nullptr);
128 
129 //: parse command-line arguments
130 template <class T>
131 class vul_arg : public vul_arg_base
132 {
133  private:
134  void settype() { ::settype(*this); }
135  public:
136  T value_;// public so we don't have to worry about templated friends.
137 
138  //: Construct a vul_arg<T> with command-line switch and default value.
139  // Command line switch \a option_string, and default value
140  // \a default_value. Add this argument to the global
141  // list of arguments that vul_arg_base::parse() uses when it eventually
142  // gets the command line.
143  //
144  // If \a option_string is null, then the argument is assigned to the
145  // first plain word in the command line (warning: this causes problems for
146  // T=char *, but that just means that you have to have a help string if you
147  // want a default... good)
148  vul_arg(char const* option_string = nullptr,
149  char const* helpstring = nullptr,
150  T default_value = T()
151  )
152  : vul_arg_base(option_string,helpstring, false),
153  value_(std::move(default_value)) { settype(); }
154 
155  //: As above, but add the arg to the list \a l, on which \c parse() can be called later.
157  char const * option_string = nullptr,
158  char const * helpstring = nullptr,
159  T default_value = T() )
160  : vul_arg_base(l, option_string, helpstring, false),
161  value_(std::move(default_value)) { settype(); }
162 
163  //: Dummy parameter to be passed during construction. It sets a flag as required.
164 
165  //: Construct a vul_arg<T> that user must set in command line.
166  // Note that a default value does not make sense.
167  // Add this argument to the global list of arguments that
168  // vul_arg_base::parse() uses when it eventually gets the command line.
169  //
170  // As in the previous constructors, if \a option_string is null, then the argument is assigned to the
171  // first plain word in the command line. However, this constructor adds a new option, allowing us to declare
172  // a non-null flag, which can appears anywhere, and that is REQUIRED.
173  //
174  // Note that the parameters are not optional. This interface has been chosen to ensure backward compatibility.
175  // \seealso is_required
176  vul_arg(char const* option_string,
177  char const* helpstring,
178  required_option_type /*dummy*/)
179  : vul_arg_base(option_string,helpstring, true),
180  value_(T()) { settype(); }
181 
182  //: As above, but add the arg to the list \a l, on which \c parse() can be called later.
184  char const * option_string,
185  char const * helpstring,
186  required_option_type /*dummy*/ )
187  : vul_arg_base(l, option_string, helpstring, true),
188  value_(T()) { settype(); }
189 
190  //: return the arg's current value, whether the default or the one from the command line.
191  T & operator () () { return value_; }
192  T const& operator () () const { return value_; }
193  //operator T& () { return value_; }
194 
195  //: returns number of args chomped, or -1 on failure.
196  int parse(char ** argv) override { return ::parse(this, argv); }
197 
198  //: print
199  std::ostream& print_value(std::ostream &s) override {
200  ::print_value(s, *this);
201  return s; // << flush
202  }
203 };
204 
205 //: a helper for vul_arg::parse.
206 // Users might need it if they wish to parse several command lines.
207 //
209 {
210  public:
211  enum autonomy {
214  };
215  //: Construct an empty vul_arg_info_list.
217  : help_("-?"), // default help operator!
218  verbose_(false), autonomy_(autonomy__) {}
219 
220  ~vul_arg_info_list() = default;
221 
222  void add(vul_arg_base* arg);
223  void parse(int& argc, char **& argv, bool warn_about_unrecognized_arguments);
224  void include(vul_arg_info_list& l);
225  void verbose(bool on) { verbose_ = on; }
226 
227  void set_help_option(char const* str);
228 
229  //: Set the (short) text used to describe the command
230  void set_help_precis(char const* str) { command_precis_ = str; }
231 
232  //: Set the (possibly long) text used to document the command.
233  // It is displayed at the end of the help page.
234  void set_help_description(char const* str) { description_ = str; }
235 
236  public://protected:
237  std::vector<vul_arg_base*> args_;
238  std::string help_;
239  std::string description_;
240  std::string command_precis_;
241  bool verbose_;
243 
244  void display_help( char const* progname= nullptr);
245 
246  private:
247  // Disallow assigning to objects of this class:
249  vul_arg_info_list& operator=(vul_arg_info_list const &) { return *this; }
250 };
251 
252 #endif // vul_arg_h_
void settype(vul_arg< T > &)
vul_arg(char const *option_string, char const *helpstring, required_option_type)
Dummy parameter to be passed during construction. It sets a flag as required.
Definition: vul_arg.h:176
std::string help_
Definition: vul_arg.h:238
static void set_help_precis(char const *str)
Definition: vul_arg.cxx:106
void set_help_description(char const *str)
Set the (possibly long) text used to document the command.
Definition: vul_arg.h:234
int parse(vul_arg< T > *, char **)
void print_value(std::ostream &, vul_arg< T > const &)
vul_arg_base(vul_arg_info_list &l, char const *option_string, char const *helpstring, bool required=false)
Definition: vul_arg.cxx:131
std::vector< vul_arg_base * > args_
Definition: vul_arg.h:237
void vul_arg_parse(int &argc, char **&argv, bool warn_about_unrecognized_arguments=true)
parse command-line arguments.
Definition: vul_arg.cxx:51
static void display_usage(char const *msg=nullptr)
Definition: vul_arg.cxx:116
static void parse_deprecated(int &argc, char **&argv, bool warn_about_unrecognized_arguments=true)
The main static method.
Definition: vul_arg.cxx:96
void add(vul_arg_base *arg)
Add an argument to the list.
Definition: vul_arg.cxx:167
void verbose(bool on)
Definition: vul_arg.h:225
bool required_
if true, this flag must be set on command line.
Definition: vul_arg.h:73
char const * help()
Definition: vul_arg.cxx:47
void include(vul_arg_info_list &l)
Append another list. The other list is not copied, just pointed to.
Definition: vul_arg.cxx:177
void vul_arg_display_usage_and_exit(char const *msg=nullptr)
Print all args, and usage messages.
Definition: vul_arg.cxx:65
void settype()
Definition: vul_arg.h:134
T value_
Definition: vul_arg.h:136
void vul_arg_include(vul_arg_info_list &l)
Add an externally supplied list of args to the global list.
Definition: vul_arg.cxx:59
T & operator()()
return the arg's current value, whether the default or the one from the command line.
Definition: vul_arg.h:191
char const * type_
Static text describing type of option (e.g. bool or double).
Definition: vul_arg.h:68
char const * option()
Definition: vul_arg.cxx:44
vul_arg_info_list(vul_arg_info_list const &)
Definition: vul_arg.h:248
static void display_usage_and_exit(char const *msg=nullptr)
Definition: vul_arg.cxx:122
vul_arg(char const *option_string=nullptr, char const *helpstring=nullptr, T default_value=T())
Construct a vul_arg<T> with command-line switch and default value.
Definition: vul_arg.h:148
virtual int parse(char **argv)=0
vul_arg(vul_arg_info_list &l, char const *option_string, char const *helpstring, required_option_type)
As above, but add the arg to the list l, on which parse() can be called later.
Definition: vul_arg.h:183
static void set_help_option(char const *str)
Definition: vul_arg.cxx:101
std::string description_
Definition: vul_arg.h:239
This is the base class for the templated vul_arg<T>s.
Definition: vul_arg.h:35
a helper for vul_arg::parse.
Definition: vul_arg.h:208
std::string help_
Description of argument.
Definition: vul_arg.h:77
autonomy autonomy_
Definition: vul_arg.h:242
static void add_to_current(vul_arg_base *a)
Definition: vul_arg.cxx:90
~vul_arg_info_list()=default
void set_help_option(char const *str)
Change the help operator (defaults to -?).
Definition: vul_arg.cxx:152
int parse(char **argv) override
returns number of args chomped, or -1 on failure.
Definition: vul_arg.h:196
std::ostream & print_value(std::ostream &s) override
print.
Definition: vul_arg.h:199
std::string option_
Option flag including "-" or "--".
Definition: vul_arg.h:75
virtual ~vul_arg_base()=default
void set_help_precis(char const *str)
Set the (short) text used to describe the command.
Definition: vul_arg.h:230
vul_arg_info_list & operator=(vul_arg_info_list const &)
Definition: vul_arg.h:249
std::string command_precis_
Definition: vul_arg.h:240
static void set_help_description(char const *str)
Definition: vul_arg.cxx:111
void parse(int &argc, char **&argv, bool warn_about_unrecognized_arguments)
Parse the command line, using the current list of args.
Definition: vul_arg.cxx:263
void display_help(char const *progname=nullptr)
Display help about each option in the arg list.
Definition: vul_arg.cxx:187
static void include_deprecated(vul_arg_info_list &l)
Add another vul_arg_info_list to the current one.
Definition: vul_arg.cxx:84
vul_arg(vul_arg_info_list &l, char const *option_string=nullptr, char const *helpstring=nullptr, T default_value=T())
As above, but add the arg to the list l, on which parse() can be called later.
Definition: vul_arg.h:156
bool set() const
Returns true if arg was set on the command line.
Definition: vul_arg.cxx:72
bool set_
After parsing, true iff value was set on command line.
Definition: vul_arg.h:71
parse command-line arguments.
Definition: vul_arg.h:29
static VUL_EXPORT required_option_type is_required
Definition: vul_arg.h:41
virtual std::ostream & print_value(std::ostream &)=0
vul_arg_info_list(autonomy autonomy__=subset)
Construct an empty vul_arg_info_list.
Definition: vul_arg.h:216