1.1 --- a/lemon/arg_parser.h Sun Jul 13 16:46:56 2008 +0100
1.2 +++ b/lemon/arg_parser.h Sun Jul 13 19:51:02 2008 +0100
1.3 @@ -1,6 +1,6 @@
1.4 -/* -*- C++ -*-
1.5 +/* -*- mode: C++; indent-tabs-mode: nil; -*-
1.6 *
1.7 - * This file is a part of LEMON, a generic C++ optimization library
1.8 + * This file is a part of LEMON, a generic C++ optimization library.
1.9 *
1.10 * Copyright (C) 2003-2008
1.11 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.12 @@ -41,27 +41,27 @@
1.13 ///
1.14 ///For a complete example see the \ref arg_parser_demo.cc demo file.
1.15 class ArgParser {
1.16 -
1.17 +
1.18 static void _showHelp(void *p);
1.19 protected:
1.20 -
1.21 +
1.22 int _argc;
1.23 const char **_argv;
1.24 -
1.25 +
1.26 enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
1.27 -
1.28 +
1.29 class ParData {
1.30 public:
1.31 union {
1.32 - bool *bool_p;
1.33 - int *int_p;
1.34 - double *double_p;
1.35 - std::string *string_p;
1.36 - struct {
1.37 - void (*p)(void *);
1.38 - void *data;
1.39 - } func_p;
1.40 -
1.41 + bool *bool_p;
1.42 + int *int_p;
1.43 + double *double_p;
1.44 + std::string *string_p;
1.45 + struct {
1.46 + void (*p)(void *);
1.47 + void *data;
1.48 + } func_p;
1.49 +
1.50 };
1.51 std::string help;
1.52 bool mandatory;
1.53 @@ -72,13 +72,13 @@
1.54 bool syn;
1.55 bool self_delete;
1.56 ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
1.57 - has_syn(false), syn(false), self_delete(false) {}
1.58 + has_syn(false), syn(false), self_delete(false) {}
1.59 };
1.60
1.61 typedef std::map<std::string,ParData> Opts;
1.62 Opts _opts;
1.63
1.64 - class GroupData
1.65 + class GroupData
1.66 {
1.67 public:
1.68 typedef std::list<std::string> Opts;
1.69 @@ -87,7 +87,7 @@
1.70 bool mandatory;
1.71 GroupData() :only_one(false), mandatory(false) {}
1.72 };
1.73 -
1.74 +
1.75 typedef std::map<std::string,GroupData> Groups;
1.76 Groups _groups;
1.77
1.78 @@ -98,12 +98,12 @@
1.79 OtherArg(std::string n, std::string h) :name(n), help(h) {}
1.80
1.81 };
1.82 -
1.83 +
1.84 std::vector<OtherArg> _others_help;
1.85 std::vector<std::string> _file_args;
1.86 std::string _command_name;
1.87
1.88 -
1.89 +
1.90 private:
1.91 //Bind a function to an option.
1.92
1.93 @@ -113,9 +113,9 @@
1.94 // must be of type "void f(void *)"
1.95 //\param data Data to be passed to \c func
1.96 ArgParser &funcOption(const std::string &name,
1.97 - const std::string &help,
1.98 - void (*func)(void *),void *data);
1.99 -
1.100 + const std::string &help,
1.101 + void (*func)(void *),void *data);
1.102 +
1.103 public:
1.104
1.105 ///Constructor
1.106 @@ -136,8 +136,8 @@
1.107 ///\param value A default value for the option.
1.108 ///\param obl Indicate if the option is mandatory.
1.109 ArgParser &intOption(const std::string &name,
1.110 - const std::string &help,
1.111 - int value=0, bool obl=false);
1.112 + const std::string &help,
1.113 + int value=0, bool obl=false);
1.114
1.115 ///Add a new floating point type option
1.116
1.117 @@ -147,8 +147,8 @@
1.118 ///\param value A default value for the option.
1.119 ///\param obl Indicate if the option is mandatory.
1.120 ArgParser &doubleOption(const std::string &name,
1.121 - const std::string &help,
1.122 - double value=0, bool obl=false);
1.123 + const std::string &help,
1.124 + double value=0, bool obl=false);
1.125
1.126 ///Add a new bool type option
1.127
1.128 @@ -159,8 +159,8 @@
1.129 ///\param obl Indicate if the option is mandatory.
1.130 ///\note A mandatory bool obtion is of very little use.
1.131 ArgParser &boolOption(const std::string &name,
1.132 - const std::string &help,
1.133 - bool value=false, bool obl=false);
1.134 + const std::string &help,
1.135 + bool value=false, bool obl=false);
1.136
1.137 ///Add a new string type option
1.138
1.139 @@ -170,8 +170,8 @@
1.140 ///\param value A default value for the option.
1.141 ///\param obl Indicate if the option is mandatory.
1.142 ArgParser &stringOption(const std::string &name,
1.143 - const std::string &help,
1.144 - std::string value="", bool obl=false);
1.145 + const std::string &help,
1.146 + std::string value="", bool obl=false);
1.147
1.148 ///Give help string for non-parsed arguments.
1.149
1.150 @@ -179,8 +179,8 @@
1.151 ///The parameter \c name will be printed in the short usage line, while
1.152 ///\c help gives a more detailed description.
1.153 ArgParser &other(const std::string &name,
1.154 - const std::string &help="");
1.155 -
1.156 + const std::string &help="");
1.157 +
1.158 ///@}
1.159
1.160 ///\name Options with External Storage
1.161 @@ -197,8 +197,8 @@
1.162 ///\param obl Indicate if the option is mandatory.
1.163 ///\retval ref The value of the argument will be written to this variable.
1.164 ArgParser &refOption(const std::string &name,
1.165 - const std::string &help,
1.166 - int &ref, bool obl=false);
1.167 + const std::string &help,
1.168 + int &ref, bool obl=false);
1.169
1.170 ///Add a new floating type option with a storage reference
1.171
1.172 @@ -208,8 +208,8 @@
1.173 ///\param obl Indicate if the option is mandatory.
1.174 ///\retval ref The value of the argument will be written to this variable.
1.175 ArgParser &refOption(const std::string &name,
1.176 - const std::string &help,
1.177 - double &ref, bool obl=false);
1.178 + const std::string &help,
1.179 + double &ref, bool obl=false);
1.180
1.181 ///Add a new bool type option with a storage reference
1.182
1.183 @@ -220,8 +220,8 @@
1.184 ///\retval ref The value of the argument will be written to this variable.
1.185 ///\note A mandatory bool obtion is of very little use.
1.186 ArgParser &refOption(const std::string &name,
1.187 - const std::string &help,
1.188 - bool &ref, bool obl=false);
1.189 + const std::string &help,
1.190 + bool &ref, bool obl=false);
1.191
1.192 ///Add a new string type option with a storage reference
1.193
1.194 @@ -231,14 +231,14 @@
1.195 ///\param obl Indicate if the option is mandatory.
1.196 ///\retval ref The value of the argument will be written to this variable.
1.197 ArgParser &refOption(const std::string &name,
1.198 - const std::string &help,
1.199 - std::string &ref, bool obl=false);
1.200 -
1.201 + const std::string &help,
1.202 + std::string &ref, bool obl=false);
1.203 +
1.204 ///@}
1.205
1.206 ///\name Option Groups and Synonyms
1.207 ///
1.208 -
1.209 +
1.210 ///@{
1.211
1.212 ///Bundle some options into a group
1.213 @@ -248,27 +248,27 @@
1.214 ///\param group The group name.
1.215 ///\param opt The option name.
1.216 ArgParser &optionGroup(const std::string &group,
1.217 - const std::string &opt);
1.218 + const std::string &opt);
1.219
1.220 ///Make the members of a group exclusive
1.221
1.222 ///If you call this function for a group, than at most one of them can be
1.223 ///given at the same time.
1.224 ArgParser &onlyOneGroup(const std::string &group);
1.225 -
1.226 +
1.227 ///Make a group mandatory
1.228
1.229 ///Using this function, at least one of the members of \c group
1.230 ///must be given.
1.231 ArgParser &mandatoryGroup(const std::string &group);
1.232 -
1.233 +
1.234 ///Create synonym to an option
1.235
1.236 ///With this function you can create a synonym \c syn of the
1.237 ///option \c opt.
1.238 ArgParser &synonym(const std::string &syn,
1.239 - const std::string &opt);
1.240 -
1.241 + const std::string &opt);
1.242 +
1.243 ///@}
1.244
1.245 void show(std::ostream &os,Opts::iterator i);
1.246 @@ -282,21 +282,21 @@
1.247
1.248 void requiresValue(std::string arg, OptType t);
1.249 void checkMandatories();
1.250 -
1.251 +
1.252 ///Start the parsing process
1.253 ArgParser &parse();
1.254
1.255 /// Synonym for parse()
1.256 - ArgParser &run()
1.257 + ArgParser &run()
1.258 {
1.259 return parse();
1.260 }
1.261 -
1.262 +
1.263 ///Give back the command name (the 0th argument)
1.264 const std::string &commandName() { return _command_name; }
1.265
1.266 ///Check if an opion has been given to the command.
1.267 - bool given(std::string op)
1.268 + bool given(std::string op)
1.269 {
1.270 Opts::iterator i = _opts.find(op);
1.271 return i!=_opts.end()?i->second.set:false;
1.272 @@ -304,12 +304,12 @@
1.273
1.274
1.275 ///Magic type for operator[]
1.276 -
1.277 +
1.278 ///This is the type of the return value of ArgParser::operator[]().
1.279 ///It automatically converts to \c int, \c double, \c bool or
1.280 ///\c std::string if the type of the option matches, otherwise it
1.281 ///throws an exception (i.e. it performs runtime type checking).
1.282 - class RefType
1.283 + class RefType
1.284 {
1.285 ArgParser &_parser;
1.286 std::string _name;
1.287 @@ -317,65 +317,65 @@
1.288 ///\e
1.289 RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
1.290 ///\e
1.291 - operator bool()
1.292 + operator bool()
1.293 {
1.294 - Opts::iterator i = _parser._opts.find(_name);
1.295 - LEMON_ASSERT(i!=_parser._opts.end(),
1.296 - std::string()+"Unkown option: '"+_name+"'");
1.297 - LEMON_ASSERT(i->second.type==ArgParser::BOOL,
1.298 - std::string()+"'"+_name+"' is a bool option");
1.299 - return *(i->second.bool_p);
1.300 + Opts::iterator i = _parser._opts.find(_name);
1.301 + LEMON_ASSERT(i!=_parser._opts.end(),
1.302 + std::string()+"Unkown option: '"+_name+"'");
1.303 + LEMON_ASSERT(i->second.type==ArgParser::BOOL,
1.304 + std::string()+"'"+_name+"' is a bool option");
1.305 + return *(i->second.bool_p);
1.306 }
1.307 ///\e
1.308 operator std::string()
1.309 {
1.310 - Opts::iterator i = _parser._opts.find(_name);
1.311 - LEMON_ASSERT(i!=_parser._opts.end(),
1.312 - std::string()+"Unkown option: '"+_name+"'");
1.313 - LEMON_ASSERT(i->second.type==ArgParser::STRING,
1.314 - std::string()+"'"+_name+"' is a string option");
1.315 - return *(i->second.string_p);
1.316 + Opts::iterator i = _parser._opts.find(_name);
1.317 + LEMON_ASSERT(i!=_parser._opts.end(),
1.318 + std::string()+"Unkown option: '"+_name+"'");
1.319 + LEMON_ASSERT(i->second.type==ArgParser::STRING,
1.320 + std::string()+"'"+_name+"' is a string option");
1.321 + return *(i->second.string_p);
1.322 }
1.323 ///\e
1.324 - operator double()
1.325 + operator double()
1.326 {
1.327 - Opts::iterator i = _parser._opts.find(_name);
1.328 - LEMON_ASSERT(i!=_parser._opts.end(),
1.329 - std::string()+"Unkown option: '"+_name+"'");
1.330 - LEMON_ASSERT(i->second.type==ArgParser::DOUBLE ||
1.331 - i->second.type==ArgParser::INTEGER,
1.332 - std::string()+"'"+_name+"' is a floating point option");
1.333 - return i->second.type==ArgParser::DOUBLE ?
1.334 - *(i->second.double_p) : *(i->second.int_p);
1.335 + Opts::iterator i = _parser._opts.find(_name);
1.336 + LEMON_ASSERT(i!=_parser._opts.end(),
1.337 + std::string()+"Unkown option: '"+_name+"'");
1.338 + LEMON_ASSERT(i->second.type==ArgParser::DOUBLE ||
1.339 + i->second.type==ArgParser::INTEGER,
1.340 + std::string()+"'"+_name+"' is a floating point option");
1.341 + return i->second.type==ArgParser::DOUBLE ?
1.342 + *(i->second.double_p) : *(i->second.int_p);
1.343 }
1.344 ///\e
1.345 - operator int()
1.346 + operator int()
1.347 {
1.348 - Opts::iterator i = _parser._opts.find(_name);
1.349 - LEMON_ASSERT(i!=_parser._opts.end(),
1.350 - std::string()+"Unkown option: '"+_name+"'");
1.351 - LEMON_ASSERT(i->second.type==ArgParser::INTEGER,
1.352 - std::string()+"'"+_name+"' is an integer option");
1.353 - return *(i->second.int_p);
1.354 + Opts::iterator i = _parser._opts.find(_name);
1.355 + LEMON_ASSERT(i!=_parser._opts.end(),
1.356 + std::string()+"Unkown option: '"+_name+"'");
1.357 + LEMON_ASSERT(i->second.type==ArgParser::INTEGER,
1.358 + std::string()+"'"+_name+"' is an integer option");
1.359 + return *(i->second.int_p);
1.360 }
1.361
1.362 };
1.363
1.364 ///Give back the value of an option
1.365 -
1.366 +
1.367 ///Give back the value of an option.
1.368 ///\sa RefType
1.369 RefType operator[](const std::string &n)
1.370 {
1.371 return RefType(*this, n);
1.372 - }
1.373 + }
1.374
1.375 ///Give back the non-option type arguments.
1.376
1.377 ///Give back a reference to a vector consisting of the program arguments
1.378 ///not starting with a '-' character.
1.379 std::vector<std::string> &files() { return _file_args; }
1.380 -
1.381 +
1.382 };
1.383 }
1.384