00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef TEUCHOS_COMMAND_LINE_PROCESSOR_HPP
00033 #define TEUCHOS_COMMAND_LINE_PROCESSOR_HPP
00034
00038 #include "Teuchos_map.hpp"
00039 #include "Teuchos_ConfigDefs.hpp"
00040
00041 namespace Teuchos {
00042
00044
00057
00058
00059
00060
00061
00062 class CommandLineProcessor {
00063 public:
00064
00068 enum EParseCommandLineReturn {
00069 PARSE_SUCCESSFUL = 0
00070 ,PARSE_HELP_PRINTED = 1
00071 ,PARSE_UNRECOGNIZED_OPTION = -1
00072 };
00073
00075
00076
00085 CommandLineProcessor(
00086 bool throwExceptions = true
00087 ,bool recogniseAllOptions = true
00088 );
00089
00091
00093
00095
00107 void setOption(
00108 const char option_true[]
00109 ,const char option_false[]
00110 ,bool *option_val
00111 ,const char documentation[] = NULL
00112 );
00113
00115
00125 void setOption(
00126 const char option_name[]
00127 ,int *option_val
00128 ,const char documentation[] = NULL
00129 );
00130
00132
00142 void setOption(
00143 const char option_name[]
00144 ,double *option_val
00145 ,const char documentation[] = NULL
00146 );
00147
00149
00159 void setOption(
00160 const char option_name[]
00161 ,std::string *option_val
00162 ,const char documentation[] = NULL
00163 );
00164
00166
00168
00170
00199 EParseCommandLineReturn parse(
00200 int argc
00201 ,char* argv[]
00202 ,std::ostream *errout = &std::cerr
00203 ) const;
00204
00206
00208
00210 void throwExceptions ( const bool & throwExceptions ) { throwExceptions_ = throwExceptions; };
00211
00213 const bool& throwExceptions() const { return throwExceptions_; };
00214
00216 void recogniseAllOptions ( const bool & recogniseAllOptions ) { recogniseAllOptions_ = recogniseAllOptions; };
00217
00219 const bool& recogniseAllOptions() const { return recogniseAllOptions_; };
00220
00222
00224
00226 class ParseError : public std::logic_error
00227 {public: ParseError(const std::string& what_arg) : std::logic_error(what_arg) {}};
00228
00230 class HelpPrinted : public ParseError
00231 {public: HelpPrinted(const std::string& what_arg) : ParseError(what_arg) {}};
00232
00234 class UnrecognizedOption : public ParseError
00235 {public: UnrecognizedOption(const std::string& what_arg) : ParseError(what_arg) {}};
00236
00238
00239 public:
00240
00241 enum EOptType { OPT_NONE, OPT_BOOL_TRUE, OPT_BOOL_FALSE, OPT_INT, OPT_DOUBLE, OPT_STRING };
00242
00243
00244
00245
00246
00247 private:
00248
00249
00250
00251
00252
00253 struct opt_val_val_t {
00254 opt_val_val_t()
00255 :opt_type(OPT_NONE),opt_val(NULL)
00256 {}
00257 opt_val_val_t( EOptType opt_type_in, void* opt_val_in )
00258 :opt_type(opt_type_in),opt_val(opt_val_in)
00259 {}
00260 EOptType opt_type;
00261 void *opt_val;
00262 };
00263
00264
00265 typedef Teuchos::map<std::string,opt_val_val_t> options_list_t;
00266
00267
00268 struct opt_doc_t {
00269 opt_doc_t()
00270 :opt_type(OPT_NONE)
00271 {}
00272 opt_doc_t(EOptType opt_type_in, const std::string& opt_name_in, const std::string& opt_name_false_in
00273 ,const std::string &documentation_in, void* default_val_in )
00274 :opt_type(opt_type_in),opt_name(opt_name_in),opt_name_false(opt_name_false_in)
00275 ,documentation(documentation_in),default_val(default_val_in)
00276 {}
00277 EOptType opt_type;
00278 std::string opt_name;
00279 std::string opt_name_false;
00280 std::string documentation;
00281 void *default_val;
00282 };
00283
00284
00285 typedef std::vector<opt_doc_t> options_documentation_list_t;
00286
00287
00288
00289
00290 bool throwExceptions_;
00291 bool recogniseAllOptions_;
00292 options_list_t options_list_;
00293 options_documentation_list_t options_documentation_list_;
00294
00295
00296
00297
00298
00299
00300 bool get_opt_val(
00301 const char str[]
00302 ,std::string *opt_name
00303 ,std::string *opt_val_str
00304 ) const;
00305
00306
00307 void print_help_msg(
00308 int argc
00309 ,char* argv[]
00310 ,std::ostream *errout
00311 ) const;
00312
00313
00314 std::string opt_type_str( EOptType ) const;
00315
00316
00317 void print_bad_opt(
00318 int argv_i
00319 ,char* argv[]
00320 ,std::ostream *errout
00321 ) const;
00322
00323
00324 };
00325
00326
00327
00328
00329 inline
00330 std::string CommandLineProcessor::opt_type_str( EOptType opt_type ) const
00331 {
00332 std::string str;
00333 switch( opt_type ) {
00334 case OPT_BOOL_TRUE:
00335 str = "bool";
00336 break;
00337 case OPT_INT:
00338 str = "int";
00339 break;
00340 case OPT_DOUBLE:
00341 str = "double";
00342 break;
00343 case OPT_STRING:
00344 str = "string";
00345 break;
00346 default:
00347 assert(0);
00348 }
00349 return str;
00350 }
00351
00352 }
00353
00354 #endif // TEUCHOS_COMMAND_LINE_PROCESSOR_HPP