Go to the documentation of this file.00001
00002 #ifndef vul_arg_h_
00003 #define vul_arg_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <vcl_vector.h>
00020 #include <vcl_string.h>
00021 #include <vcl_list.h>
00022 #include <vcl_iosfwd.h>
00023 #include <vxl_config.h>
00024
00025
00026 class vul_arg_info_list;
00027 template <class T> class vul_arg;
00028 template <class T> void settype (vul_arg<T> &);
00029 template <class T> void print_value (vcl_ostream &, vul_arg<T> const &);
00030 template <class T> int parse (vul_arg<T>*, char**);
00031
00032
00033 class vul_arg_base
00034 {
00035 protected:
00036 struct required_option_type {};
00037
00038 public:
00039 static required_option_type is_required;
00040
00041 static void parse_deprecated(int& argc, char **& argv,
00042 bool warn_about_unrecognized_arguments = true);
00043 static void include_deprecated(vul_arg_info_list& l);
00044
00045 static void add_to_current(vul_arg_base* a);
00046 static void set_help_option( char const*str);
00047 static void set_help_description( char const*str);
00048 static void set_help_precis( char const*str);
00049 static void display_usage(char const* msg = 0);
00050 static void display_usage_and_exit(char const* msg = 0);
00051
00052 friend class vul_arg_info_list;
00053
00054 char const* option();
00055 char const* help();
00056
00057
00058 bool set() const;
00059
00060 virtual vcl_ostream& print_value(vcl_ostream&) = 0;
00061
00062 public:
00063
00064
00065
00066 char const *type_;
00067 protected:
00068
00069 bool set_;
00070
00071 bool required_;
00072
00073 vcl_string option_;
00074
00075 vcl_string help_;
00076
00077 vul_arg_base(vul_arg_info_list& l, char const* option_string,
00078 char const*helpstring, bool required= false);
00079 vul_arg_base(char const* option_string, char const*helpstring, bool required= false);
00080 virtual ~vul_arg_base() {}
00081
00082 virtual int parse(char ** argv) = 0;
00083 };
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 void vul_arg_parse(int& argc, char **& argv,
00119 bool warn_about_unrecognized_arguments = true);
00120
00121
00122 void vul_arg_include(vul_arg_info_list& l);
00123
00124
00125 void vul_arg_display_usage_and_exit(char const* msg = 0);
00126
00127
00128 template <class T>
00129 class vul_arg : public vul_arg_base
00130 {
00131 private:
00132 void settype() { ::settype(*this); }
00133 public:
00134 T value_;
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 vul_arg(char const* option_string = 0,
00147 char const* helpstring = 0,
00148 T default_value = T()
00149 )
00150 : vul_arg_base(option_string,helpstring, false),
00151 value_(default_value) { settype(); }
00152
00153
00154 vul_arg(vul_arg_info_list & l,
00155 char const * option_string = 0,
00156 char const * helpstring = 0,
00157 T default_value = T() )
00158 : vul_arg_base(l, option_string, helpstring, false),
00159 value_(default_value) { settype(); }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 vul_arg(char const* option_string,
00175 char const* helpstring,
00176 required_option_type dummy)
00177 : vul_arg_base(option_string,helpstring, true),
00178 value_(T()) { settype(); }
00179
00180
00181 vul_arg(vul_arg_info_list & l,
00182 char const * option_string,
00183 char const * helpstring,
00184 required_option_type dummy )
00185 : vul_arg_base(l, option_string, helpstring, true),
00186 value_(T()) { settype(); }
00187
00188
00189 T & operator () () { return value_; }
00190 T const& operator () () const { return value_; }
00191
00192
00193
00194 int parse(char ** argv) { return ::parse(this, argv); }
00195
00196
00197 vcl_ostream& print_value(vcl_ostream &s) {
00198 ::print_value(s, *this);
00199 return s;
00200 }
00201 };
00202
00203
00204
00205
00206 class vul_arg_info_list
00207 {
00208 public:
00209 enum autonomy {
00210 subset,
00211 all
00212 };
00213
00214 vul_arg_info_list(autonomy autonomy__ = subset)
00215 : help_("-?"),
00216 verbose_(false), autonomy_(autonomy__) {}
00217
00218 ~vul_arg_info_list() {}
00219
00220 void add(vul_arg_base* arg);
00221 void parse(int& argc, char **& argv, bool warn_about_unrecognized_arguments);
00222 void include(vul_arg_info_list& l);
00223 void verbose(bool on) { verbose_ = on; }
00224
00225 void set_help_option(char const* str);
00226
00227
00228 void set_help_precis(char const* str) { command_precis_ = str; }
00229
00230
00231
00232 void set_help_description(char const* str) { description_ = str; }
00233
00234 public:
00235 vcl_vector<vul_arg_base*> args_;
00236 vcl_string help_;
00237 vcl_string description_;
00238 vcl_string command_precis_;
00239 bool verbose_;
00240 autonomy autonomy_;
00241
00242 void display_help( char const* progname= 0);
00243
00244 private:
00245
00246 vul_arg_info_list(vul_arg_info_list const &) {}
00247 vul_arg_info_list& operator=(vul_arg_info_list const &) { return *this; }
00248 };
00249
00250 #if defined(VCL_KAI) || defined(VCL_COMO) || defined(VCL_ICC)
00251 #define declare_specialization(T) \
00252 template<> void settype(vul_arg<T > &); \
00253 template<> void print_value(vcl_ostream &, vul_arg<T > const &); \
00254 template<> int parse(vul_arg<T > *, char **)
00255
00256 declare_specialization(bool);
00257 declare_specialization(int);
00258 declare_specialization(unsigned);
00259 declare_specialization(char*);
00260 declare_specialization(char const*);
00261 declare_specialization(float);
00262 declare_specialization(double);
00263 declare_specialization(vcl_list<int>);
00264 declare_specialization(vcl_vector<int>);
00265 declare_specialization(vcl_vector<unsigned>);
00266 declare_specialization(vcl_vector<double>);
00267 declare_specialization(vcl_string);
00268
00269 #ifdef VXL_HAS_INT_64
00270 declare_specialization(vxl_int_64);
00271 #endif
00272
00273 #undef declare_specialization
00274 #endif // VCL_KAI
00275
00276 #endif // vul_arg_h_