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
00024
00025 class vul_arg_info_list;
00026 template <class T> class vul_arg;
00027 template <class T> void settype (vul_arg<T> &);
00028 template <class T> void print_value (vcl_ostream &, vul_arg<T> const &);
00029 template <class T> int parse (vul_arg<T>*, char**);
00030
00031
00032 class vul_arg_base
00033 {
00034 public:
00035 static void parse_deprecated(int& argc, char **& argv,
00036 bool warn_about_unrecognized_arguments = true);
00037 static void include_deprecated(vul_arg_info_list& l);
00038
00039 static void add_to_current(vul_arg_base* a);
00040 static void set_help_option( char const*str);
00041 static void set_help_description( char const*str);
00042 static void set_help_precis( char const*str);
00043 static void display_usage(char const* msg = 0);
00044 static void display_usage_and_exit(char const* msg = 0);
00045
00046 friend class vul_arg_info_list;
00047
00048 char const* option();
00049 char const* help();
00050
00051
00052 bool set() const;
00053
00054 virtual vcl_ostream& print_value(vcl_ostream&) = 0;
00055
00056 public:
00057
00058
00059
00060 char const *type_;
00061 protected:
00062
00063 bool set_;
00064
00065 vcl_string option_;
00066
00067 vcl_string help_;
00068
00069 vul_arg_base(vul_arg_info_list& l, char const* option_string,
00070 char const*helpstring);
00071 vul_arg_base(char const* option_string, char const*helpstring);
00072 virtual ~vul_arg_base() {}
00073
00074 virtual int parse(char ** argv) = 0;
00075 };
00076
00077
00078
00079
00080
00081
00082
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 void vul_arg_parse(int& argc, char **& argv,
00111 bool warn_about_unrecognized_arguments = true);
00112
00113
00114 void vul_arg_include(vul_arg_info_list& l);
00115
00116
00117 void vul_arg_display_usage_and_exit(char const* msg = 0);
00118
00119
00120 template <class T>
00121 class vul_arg : public vul_arg_base
00122 {
00123 public:
00124 T value_;
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 vul_arg(char const* option_string = 0,
00137 char const* helpstring = 0,
00138 T default_value = T())
00139 : vul_arg_base(option_string,helpstring),
00140 value_(default_value) { settype(); }
00141
00142
00143 vul_arg(vul_arg_info_list & l,
00144 char const * option_string = 0,
00145 char const * helpstring = 0,
00146 T default_value = T())
00147 : vul_arg_base(l, option_string, helpstring),
00148 value_(default_value) { settype(); }
00149
00150
00151 T & operator () () { return value_; }
00152 T const& operator () () const { return value_; }
00153
00154
00155
00156 int parse(char ** argv) { return ::parse(this, argv); }
00157
00158
00159 vcl_ostream& print_value(vcl_ostream &s) {
00160 ::print_value(s, *this);
00161 return s;
00162 }
00163
00164 private:
00165 void settype() { ::settype(*this); }
00166 };
00167
00168
00169
00170
00171 class vul_arg_info_list
00172 {
00173 public:
00174 enum autonomy {
00175 subset,
00176 all
00177 };
00178
00179 vul_arg_info_list(autonomy autonomy__ = subset)
00180 : help_("-?"),
00181 verbose_(false), autonomy_(autonomy__) {}
00182
00183 ~vul_arg_info_list() {}
00184
00185 void add(vul_arg_base* arg);
00186 void parse(int& argc, char **& argv, bool warn_about_unrecognized_arguments);
00187 void include(vul_arg_info_list& l);
00188 void verbose(bool on) { verbose_ = on; }
00189
00190 void set_help_option(char const* str);
00191
00192
00193 void set_help_precis(char const* str) { command_precis_ = str; }
00194
00195
00196
00197 void set_help_description(char const* str) { description_ = str; }
00198
00199 public:
00200 vcl_vector<vul_arg_base*> args_;
00201 vcl_string help_;
00202 vcl_string description_;
00203 vcl_string command_precis_;
00204 bool verbose_;
00205 autonomy autonomy_;
00206
00207 void display_help( char const* progname= 0);
00208
00209 private:
00210 vul_arg_info_list(vul_arg_info_list const &) {}
00211 void operator=(vul_arg_info_list const &) {}
00212 };
00213
00214 #if defined(VCL_KAI) || defined(VCL_COMO) || defined(VCL_ICC)
00215 #define declare_specialization(T) \
00216 template<> void settype(vul_arg<T > &); \
00217 template<> void print_value(vcl_ostream &, vul_arg<T > const &); \
00218 template<> int parse(vul_arg<T > *, char **)
00219
00220 declare_specialization(bool);
00221 declare_specialization(int);
00222 declare_specialization(unsigned);
00223 declare_specialization(char*);
00224 declare_specialization(char const*);
00225 declare_specialization(float);
00226 declare_specialization(double);
00227 declare_specialization(vcl_list<int>);
00228 declare_specialization(vcl_vector<int>);
00229 declare_specialization(vcl_vector<unsigned>);
00230 declare_specialization(vcl_vector<double>);
00231 declare_specialization(vcl_string);
00232
00233 #undef declare_specialization
00234 #endif // VCL_KAI
00235
00236 #endif // vul_arg_h_