arg_parser.h: A command line argument parser.
dist_log.h: A tool for measuring one and two dimensional distributions.
2 * lemon/main_params.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_ARG_PARSER
18 #define LEMON_ARG_PARSER
30 ///\brief A tools to parse command line arguments.
32 ///\author Alpar Juttner
36 ///Command line arguments parser
39 ///Command line arguments parser
43 static void _showHelp(void *p);
49 enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 };
57 std::string *string_p;
72 ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false),
73 has_syn(false), syn(false) {}
76 typedef std::map<std::string,ParData> Opts;
82 typedef std::list<std::string> Opts;
86 GroupData() :only_one(false), mandatory(false) {}
89 typedef std::map<std::string,GroupData> Groups;
96 OtherArg(std::string n, std::string h) :name(n), help(h) {}
100 std::vector<OtherArg> _others_help;
101 std::vector<std::string> _file_args;
102 std::string _command_name;
107 ArgParser(int argc, char **argv);
109 ///Add a new integer type option
111 ///\param name The name of the option. The leading '-' must be omitted.
112 ///\param help A help string.
113 ///\retval value The value of the argument will be written to this variable.
114 ///\param obl Indicate if the option is mandatory.
115 ArgParser &option(const std::string &name,
116 const std::string &help,
117 int &value, bool obl=false);
119 ///Add a new floating type option
121 ///\param name The name of the option. The leading '-' must be omitted.
122 ///\param help A help string.
123 ///\retval value The value of the argument will be written to this variable.
124 ///\param obl Indicate if the option is mandatory.
125 ArgParser &option(const std::string &name,
126 const std::string &help,
127 double &value, bool obl=false);
129 ///Add a new bool type option
131 ///\param name The name of the option. The leading '-' must be omitted.
132 ///\param help A help string.
133 ///\retval value The value of the argument will be written to this variable.
134 ///\param obl Indicate if the option is mandatory.
135 ////\note A mandatory bool obtion is of very little use.)
136 ArgParser &option(const std::string &name,
137 const std::string &help,
138 bool &value, bool obl=false);
140 ///Add a new string type option
142 ///\param name The name of the option. The leading '-' must be omitted.
143 ///\param help A help string.
144 ///\retval value The value of the argument will be written to this variable.
145 ///\param obl Indicate if the option is mandatory.
146 ArgParser &option(const std::string &name,
147 const std::string &help,
148 std::string &value, bool obl=false);
150 ///Bind a function to an option.
152 ///\param name The name of the option. The leading '-' must be omitted.
153 ///\param help A help string.
154 ///\retval func The function to be called when the option is given. It
155 /// must be of type "void f(void *)"
156 ///\param data Data to be passed to \c func
157 ArgParser &option(const std::string &name,
158 const std::string &help,
159 void (*func)(void *),void *data);
161 ///Boundle some options into a group
163 /// You can group some option by calling this function repeatedly for each
164 /// option to be grupped with the same groupname.
165 ///\param group The group name
166 ///\param opt The option name
167 ArgParser &optionGroup(const std::string &group,
168 const std::string &opt);
170 ///Make the members of a group exclusive
172 ///If you call this function for a group, than at most one of them can be
173 ///given at the same time
174 ArgParser &onlyOneGroup(const std::string &group);
176 ///Create synonym to an option
178 ///With this function you can create a sysnonym called \c sys of the
180 ArgParser &synonym(const std::string &syn,
181 const std::string &opt);
183 ///Make a group mandatory
185 ///Using this function, at least one of the members of \c group
187 ArgParser &mandatoryGroup(const std::string &group);
189 ///Give help string for non-parsed arguments.
191 ///With this function you can give help string for non-parsed arguments.
192 ///the parameter \c name will be printed in the short usage line, while
193 ///\c help gives a more detailed description.
194 ArgParser &other(const std::string &name,
195 const std::string &help="");
197 ///Non option type arguments.
199 ///Gives back a reference to a vector consisting of the program arguments
200 ///not starting with a '-' character.
201 std::vector<std::string> &files() { return _file_args; }
203 ///Give back the command name (the 0th argument)
204 const std::string &commandName() { return _command_name; }
206 void show(std::ostream &os,Opts::iterator i);
207 void show(std::ostream &os,Groups::iterator i);
208 void showHelp(Opts::iterator i);
209 void showHelp(std::vector<OtherArg>::iterator i);
213 void unknownOpt(std::string arg);
215 void requiresValue(std::string arg, OptType t);
216 void checkMandatories();
221 /// Synonym for parse()
227 ///Check if an opion has been given to the command.
228 bool given(std::string op)
230 Opts::iterator i = _opts.find(op);
231 return i!=_opts.end()?i->second.set:false;
239 #endif // LEMON_MAIN_PARAMS