3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_ARG_PARSER
20 #define LEMON_ARG_PARSER
32 ///\brief A tools to parse command line arguments.
34 ///\author Alpar Juttner
38 ///Command line arguments parser
41 ///Command line arguments parser
45 static void _showHelp(void *p);
51 enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
59 std::string *string_p;
74 ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
75 has_syn(false), syn(false), self_delete(false) {}
78 typedef std::map<std::string,ParData> Opts;
84 typedef std::list<std::string> Opts;
88 GroupData() :only_one(false), mandatory(false) {}
91 typedef std::map<std::string,GroupData> Groups;
98 OtherArg(std::string n, std::string h) :name(n), help(h) {}
102 std::vector<OtherArg> _others_help;
103 std::vector<std::string> _file_args;
104 std::string _command_name;
109 ArgParser(int argc, char **argv);
113 ///Add a new integer type option
115 ///\param name The name of the option. The leading '-' must be omitted.
116 ///\param help A help string.
117 ///\retval value The value of the argument will be written to this variable.
118 ///\param obl Indicate if the option is mandatory.
119 ArgParser &intOption(const std::string &name,
120 const std::string &help,
121 int value=0, bool obl=false);
123 ///Add a new floating type option
125 ///\param name The name of the option. The leading '-' must be omitted.
126 ///\param help A help string.
127 ///\retval value The value of the argument will be written to this variable.
128 ///\param obl Indicate if the option is mandatory.
129 ArgParser &doubleOption(const std::string &name,
130 const std::string &help,
131 double value=0, bool obl=false);
133 ///Add a new bool type option
135 ///\param name The name of the option. The leading '-' must be omitted.
136 ///\param help A help string.
137 ///\retval value The value of the argument will be written to this variable.
138 ///\param obl Indicate if the option is mandatory.
139 ////\note A mandatory bool obtion is of very little use.)
140 ArgParser &boolOption(const std::string &name,
141 const std::string &help,
142 bool value=false, bool obl=false);
144 ///Add a new string type option
146 ///\param name The name of the option. The leading '-' must be omitted.
147 ///\param help A help string.
148 ///\retval value The value of the argument will be written to this variable.
149 ///\param obl Indicate if the option is mandatory.
150 ArgParser &stringOption(const std::string &name,
151 const std::string &help,
152 std::string value="", bool obl=false);
157 ///Add a new integer type option
159 ///\param name The name of the option. The leading '-' must be omitted.
160 ///\param help A help string.
161 ///\retval value The value of the argument will be written to this variable.
162 ///\param obl Indicate if the option is mandatory.
163 ArgParser &refOption(const std::string &name,
164 const std::string &help,
165 int &value, bool obl=false);
167 ///Add a new floating type option
169 ///\param name The name of the option. The leading '-' must be omitted.
170 ///\param help A help string.
171 ///\retval value The value of the argument will be written to this variable.
172 ///\param obl Indicate if the option is mandatory.
173 ArgParser &refOption(const std::string &name,
174 const std::string &help,
175 double &value, bool obl=false);
177 ///Add a new bool type option
179 ///\param name The name of the option. The leading '-' must be omitted.
180 ///\param help A help string.
181 ///\retval value The value of the argument will be written to this variable.
182 ///\param obl Indicate if the option is mandatory.
183 ////\note A mandatory bool obtion is of very little use.)
184 ArgParser &refOption(const std::string &name,
185 const std::string &help,
186 bool &value, bool obl=false);
188 ///Add a new string type option
190 ///\param name The name of the option. The leading '-' must be omitted.
191 ///\param help A help string.
192 ///\retval value The value of the argument will be written to this variable.
193 ///\param obl Indicate if the option is mandatory.
194 ArgParser &refOption(const std::string &name,
195 const std::string &help,
196 std::string &value, bool obl=false);
198 ///Bind a function to an option.
200 ///\param name The name of the option. The leading '-' must be omitted.
201 ///\param help A help string.
202 ///\retval func The function to be called when the option is given. It
203 /// must be of type "void f(void *)"
204 ///\param data Data to be passed to \c func
205 ArgParser &refOption(const std::string &name,
206 const std::string &help,
207 void (*func)(void *),void *data);
209 ///Boundle some options into a group
211 /// You can group some option by calling this function repeatedly for each
212 /// option to be grupped with the same groupname.
213 ///\param group The group name
214 ///\param opt The option name
215 ArgParser &optionGroup(const std::string &group,
216 const std::string &opt);
218 ///Make the members of a group exclusive
220 ///If you call this function for a group, than at most one of them can be
221 ///given at the same time
222 ArgParser &onlyOneGroup(const std::string &group);
224 ///Create synonym to an option
226 ///With this function you can create a sysnonym called \c sys of the
228 ArgParser &synonym(const std::string &syn,
229 const std::string &opt);
231 ///Make a group mandatory
233 ///Using this function, at least one of the members of \c group
235 ArgParser &mandatoryGroup(const std::string &group);
237 ///Give help string for non-parsed arguments.
239 ///With this function you can give help string for non-parsed arguments.
240 ///the parameter \c name will be printed in the short usage line, while
241 ///\c help gives a more detailed description.
242 ArgParser &other(const std::string &name,
243 const std::string &help="");
245 ///Non option type arguments.
247 ///Gives back a reference to a vector consisting of the program arguments
248 ///not starting with a '-' character.
249 std::vector<std::string> &files() { return _file_args; }
251 ///Give back the command name (the 0th argument)
252 const std::string &commandName() { return _command_name; }
254 void show(std::ostream &os,Opts::iterator i);
255 void show(std::ostream &os,Groups::iterator i);
256 void showHelp(Opts::iterator i);
257 void showHelp(std::vector<OtherArg>::iterator i);
261 void unknownOpt(std::string arg);
263 void requiresValue(std::string arg, OptType t);
264 void checkMandatories();
269 /// Synonym for parse()
275 ///Check if an opion has been given to the command.
276 bool given(std::string op)
278 Opts::iterator i = _opts.find(op);
279 return i!=_opts.end()?i->second.set:false;
288 RefType(ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
291 Opts::iterator i = _parser._opts.find(_name);
292 if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
293 else if(i->second.type!=ArgParser::BOOL) exit(3);
294 else return *(i->second.bool_p);
296 operator std::string()
298 Opts::iterator i = _parser._opts.find(_name);
299 if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
300 else if(i->second.type!=ArgParser::STRING) exit(3);
301 else return *(i->second.string_p);
305 Opts::iterator i = _parser._opts.find(_name);
306 if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
307 else if(i->second.type!=ArgParser::DOUBLE) exit(3);
308 else return *(i->second.double_p);
312 Opts::iterator i = _parser._opts.find(_name);
313 if(i==_parser._opts.end()) exit(3); ///\todo throw exception instead
314 else if(i->second.type!=ArgParser::INTEGER) exit(3);
315 else return *(i->second.int_p);
320 RefType operator[](const std::string &n)
322 return RefType(*this, n);
331 #endif // LEMON_MAIN_PARAMS