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) {}
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);
111 ///Add a new integer type option
113 ///\param name The name of the option. The leading '-' must be omitted.
114 ///\param help A help string.
115 ///\retval value The value of the argument will be written to this variable.
116 ///\param obl Indicate if the option is mandatory.
117 ArgParser &option(const std::string &name,
118 const std::string &help,
119 int &value, bool obl=false);
121 ///Add a new floating type option
123 ///\param name The name of the option. The leading '-' must be omitted.
124 ///\param help A help string.
125 ///\retval value The value of the argument will be written to this variable.
126 ///\param obl Indicate if the option is mandatory.
127 ArgParser &option(const std::string &name,
128 const std::string &help,
129 double &value, bool obl=false);
131 ///Add a new bool type option
133 ///\param name The name of the option. The leading '-' must be omitted.
134 ///\param help A help string.
135 ///\retval value The value of the argument will be written to this variable.
136 ///\param obl Indicate if the option is mandatory.
137 ////\note A mandatory bool obtion is of very little use.)
138 ArgParser &option(const std::string &name,
139 const std::string &help,
140 bool &value, bool obl=false);
142 ///Add a new string type option
144 ///\param name The name of the option. The leading '-' must be omitted.
145 ///\param help A help string.
146 ///\retval value The value of the argument will be written to this variable.
147 ///\param obl Indicate if the option is mandatory.
148 ArgParser &option(const std::string &name,
149 const std::string &help,
150 std::string &value, bool obl=false);
152 ///Bind a function to an option.
154 ///\param name The name of the option. The leading '-' must be omitted.
155 ///\param help A help string.
156 ///\retval func The function to be called when the option is given. It
157 /// must be of type "void f(void *)"
158 ///\param data Data to be passed to \c func
159 ArgParser &option(const std::string &name,
160 const std::string &help,
161 void (*func)(void *),void *data);
163 ///Boundle some options into a group
165 /// You can group some option by calling this function repeatedly for each
166 /// option to be grupped with the same groupname.
167 ///\param group The group name
168 ///\param opt The option name
169 ArgParser &optionGroup(const std::string &group,
170 const std::string &opt);
172 ///Make the members of a group exclusive
174 ///If you call this function for a group, than at most one of them can be
175 ///given at the same time
176 ArgParser &onlyOneGroup(const std::string &group);
178 ///Create synonym to an option
180 ///With this function you can create a sysnonym called \c sys of the
182 ArgParser &synonym(const std::string &syn,
183 const std::string &opt);
185 ///Make a group mandatory
187 ///Using this function, at least one of the members of \c group
189 ArgParser &mandatoryGroup(const std::string &group);
191 ///Give help string for non-parsed arguments.
193 ///With this function you can give help string for non-parsed arguments.
194 ///the parameter \c name will be printed in the short usage line, while
195 ///\c help gives a more detailed description.
196 ArgParser &other(const std::string &name,
197 const std::string &help="");
199 ///Non option type arguments.
201 ///Gives back a reference to a vector consisting of the program arguments
202 ///not starting with a '-' character.
203 std::vector<std::string> &files() { return _file_args; }
205 ///Give back the command name (the 0th argument)
206 const std::string &commandName() { return _command_name; }
208 void show(std::ostream &os,Opts::iterator i);
209 void show(std::ostream &os,Groups::iterator i);
210 void showHelp(Opts::iterator i);
211 void showHelp(std::vector<OtherArg>::iterator i);
215 void unknownOpt(std::string arg);
217 void requiresValue(std::string arg, OptType t);
218 void checkMandatories();
223 /// Synonym for parse()
229 ///Check if an opion has been given to the command.
230 bool given(std::string op)
232 Opts::iterator i = _opts.find(op);
233 return i!=_opts.end()?i->second.set:false;
241 #endif // LEMON_MAIN_PARAMS