# HG changeset patch
# User Peter Kovacs <kpeter@inf.elte.hu>
# Date 1215252481 -7200
# Node ID eea4295af4ad79661250aeaf42822051e6942026
# Parent 1e6af6f0843c373a0579fc699ac4a83bb4f5948a
Doc improvements related to ArgParser
diff -r 1e6af6f0843c -r eea4295af4ad demo/arg_parser_demo.cc
|
a
|
b
|
|
| 29 | 29 | using namespace lemon; |
| 30 | 30 | int main(int argc, const char **argv) |
| 31 | 31 | { |
| | 32 | // Initialize the argument parser and set all options. |
| 32 | 33 | ArgParser ap(argc,argv); |
| 33 | 34 | int i; |
| 34 | 35 | std::string s; |
| 35 | | double d; |
| 36 | | bool b,sil; |
| | 36 | double d=1.0; |
| | 37 | bool b,nh; |
| 37 | 38 | bool g1,g2,g3; |
| 38 | | ap.refOption("n", "An integer input.", i, true) |
| 39 | | .refOption("val", "A double input.", d) |
| 40 | | .doubleOption("val2", "A double input.", d) |
| 41 | | .synonym("vals","val") |
| 42 | | .refOption("name", "A string input.", s) |
| | 39 | ap.refOption("n", "An integer input.", i, true) // mandatory integer option with storage reference |
| | 40 | .refOption("val", "A double input.", d) // double option with storage reference (default value: 1.0) |
| | 41 | .doubleOption("val2", "A double input.", 3.14) // double option without storage reference (default value: 3.14) |
| | 42 | .synonym("vals","val") // synonym for -val option |
| | 43 | .refOption("name", "A string input.", s) // string option |
| 43 | 44 | .refOption("f", "A switch.", b) |
| 44 | | .refOption("nohelp", "", sil) |
| | 45 | .refOption("nohelp", "", nh) |
| 45 | 46 | .refOption("gra","Choice A",g1) |
| 46 | 47 | .refOption("grb","Choice B",g2) |
| 47 | | .refOption("grc","Choice C",g3) |
| | 48 | .refOption("grc","Choice C",g3) // bool options |
| 48 | 49 | .optionGroup("gr","gra") |
| 49 | 50 | .optionGroup("gr","grb") |
| 50 | | .optionGroup("gr","grc") |
| 51 | | .mandatoryGroup("gr") |
| 52 | | .onlyOneGroup("gr") |
| | 51 | .optionGroup("gr","grc") // bundle -gr* options into a group |
| | 52 | .mandatoryGroup("gr") // the group is mandatory |
| | 53 | .onlyOneGroup("gr") // only one option can be given from the group |
| 53 | 54 | .other("infile","The input file.") |
| 54 | | .other("..."); |
| | 55 | .other("..."); // non-parsed arguments (e.g. input files) |
| 55 | 56 | |
| | 57 | // Perform the parsing process. In case of any error it terminates the program. |
| 56 | 58 | ap.parse(); |
| 57 | 59 | |
| | 60 | // Check each option if it has been given and print its value. |
| 58 | 61 | std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
| 59 | 62 | |
| 60 | | if(ap.given("n")) std::cout << " Value of -n: " << i << std::endl; |
| | 63 | std::cout << " Value of -n: " << i << std::endl; |
| 61 | 64 | if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; |
| | 65 | if(ap.given("val2")) { |
| | 66 | d = ap["val2"]; |
| | 67 | std::cout << " Value of -val2: " << d << std::endl; |
| | 68 | } |
| 62 | 69 | if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; |
| 63 | 70 | if(ap.given("f")) std::cout << " -f is given\n"; |
| 64 | | if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << sil << std::endl; |
| | 71 | if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl; |
| 65 | 72 | if(ap.given("gra")) std::cout << " -gra is given\n"; |
| 66 | 73 | if(ap.given("grb")) std::cout << " -grb is given\n"; |
| 67 | 74 | if(ap.given("grc")) std::cout << " -grc is given\n"; |
| … |
… |
|
| 80 | 87 | for(unsigned int i=0;i<ap.files().size();++i) |
| 81 | 88 | std::cout << " '" << ap.files()[i] << "'\n"; |
| 82 | 89 | |
| | 90 | return 0; |
| 83 | 91 | } |
diff -r 1e6af6f0843c -r eea4295af4ad lemon/arg_parser.h
|
a
|
b
|
|
| 118 | 118 | |
| 119 | 119 | public: |
| 120 | 120 | |
| 121 | | ///\e |
| | 121 | ///Constructor |
| 122 | 122 | ArgParser(int argc, const char **argv); |
| 123 | 123 | |
| 124 | 124 | ~ArgParser(); |
| 125 | 125 | |
| | 126 | ///\name Options |
| | 127 | /// |
| | 128 | |
| | 129 | ///@{ |
| | 130 | |
| 126 | 131 | ///Add a new integer type option |
| 127 | 132 | |
| | 133 | ///Add a new integer type option. |
| 128 | 134 | ///\param name The name of the option. The leading '-' must be omitted. |
| 129 | 135 | ///\param help A help string. |
| 130 | 136 | ///\param value A default value for the option. |
| … |
… |
|
| 135 | 141 | |
| 136 | 142 | ///Add a new floating point type option |
| 137 | 143 | |
| | 144 | ///Add a new floating point type option. |
| 138 | 145 | ///\param name The name of the option. The leading '-' must be omitted. |
| 139 | 146 | ///\param help A help string. |
| 140 | 147 | ///\param value A default value for the option. |
| … |
… |
|
| 145 | 152 | |
| 146 | 153 | ///Add a new bool type option |
| 147 | 154 | |
| | 155 | ///Add a new bool type option. |
| 148 | 156 | ///\param name The name of the option. The leading '-' must be omitted. |
| 149 | 157 | ///\param help A help string. |
| 150 | 158 | ///\param value A default value for the option. |
| … |
… |
|
| 156 | 164 | |
| 157 | 165 | ///Add a new string type option |
| 158 | 166 | |
| | 167 | ///Add a new string type option. |
| 159 | 168 | ///\param name The name of the option. The leading '-' must be omitted. |
| 160 | 169 | ///\param help A help string. |
| 161 | 170 | ///\param value A default value for the option. |
| … |
… |
|
| 164 | 173 | const std::string &help, |
| 165 | 174 | std::string value="", bool obl=false); |
| 166 | 175 | |
| 167 | | ///\name Options with external storage |
| | 176 | ///Give help string for non-parsed arguments. |
| | 177 | |
| | 178 | ///With this function you can give help string for non-parsed arguments. |
| | 179 | ///The parameter \c name will be printed in the short usage line, while |
| | 180 | ///\c help gives a more detailed description. |
| | 181 | ArgParser &other(const std::string &name, |
| | 182 | const std::string &help=""); |
| | 183 | |
| | 184 | ///@} |
| | 185 | |
| | 186 | ///\name Options with External Storage |
| 168 | 187 | ///Using this functions, the value of the option will be directly written |
| 169 | 188 | ///into a variable once the option appears in the command line. |
| 170 | 189 | |
| … |
… |
|
| 172 | 191 | |
| 173 | 192 | ///Add a new integer type option with a storage reference |
| 174 | 193 | |
| | 194 | ///Add a new integer type option with a storage reference. |
| 175 | 195 | ///\param name The name of the option. The leading '-' must be omitted. |
| 176 | 196 | ///\param help A help string. |
| 177 | 197 | ///\param obl Indicate if the option is mandatory. |
| … |
… |
|
| 182 | 202 | |
| 183 | 203 | ///Add a new floating type option with a storage reference |
| 184 | 204 | |
| | 205 | ///Add a new floating type option with a storage reference. |
| 185 | 206 | ///\param name The name of the option. The leading '-' must be omitted. |
| 186 | 207 | ///\param help A help string. |
| 187 | 208 | ///\param obl Indicate if the option is mandatory. |
| … |
… |
|
| 192 | 213 | |
| 193 | 214 | ///Add a new bool type option with a storage reference |
| 194 | 215 | |
| | 216 | ///Add a new bool type option with a storage reference. |
| 195 | 217 | ///\param name The name of the option. The leading '-' must be omitted. |
| 196 | 218 | ///\param help A help string. |
| 197 | 219 | ///\param obl Indicate if the option is mandatory. |
| … |
… |
|
| 203 | 225 | |
| 204 | 226 | ///Add a new string type option with a storage reference |
| 205 | 227 | |
| | 228 | ///Add a new string type option with a storage reference. |
| 206 | 229 | ///\param name The name of the option. The leading '-' must be omitted. |
| 207 | 230 | ///\param help A help string. |
| 208 | 231 | ///\param obl Indicate if the option is mandatory. |
| … |
… |
|
| 218 | 241 | |
| 219 | 242 | ///@{ |
| 220 | 243 | |
| 221 | | ///Boundle some options into a group |
| | 244 | ///Bundle some options into a group |
| 222 | 245 | |
| 223 | 246 | /// You can group some option by calling this function repeatedly for each |
| 224 | 247 | /// option to be grouped with the same groupname. |
| … |
… |
|
| 230 | 253 | ///Make the members of a group exclusive |
| 231 | 254 | |
| 232 | 255 | ///If you call this function for a group, than at most one of them can be |
| 233 | | ///given at the same time |
| | 256 | ///given at the same time. |
| 234 | 257 | ArgParser &onlyOneGroup(const std::string &group); |
| 235 | 258 | |
| 236 | 259 | ///Make a group mandatory |
| … |
… |
|
| 247 | 270 | const std::string &opt); |
| 248 | 271 | |
| 249 | 272 | ///@} |
| 250 | | |
| 251 | | ///Give help string for non-parsed arguments. |
| 252 | | |
| 253 | | ///With this function you can give help string for non-parsed arguments. |
| 254 | | ///The parameter \c name will be printed in the short usage line, while |
| 255 | | ///\c help gives a more detailed description. |
| 256 | | ArgParser &other(const std::string &name, |
| 257 | | const std::string &help=""); |
| 258 | | |
| 259 | | ///Give back the non-option type arguments. |
| 260 | | |
| 261 | | ///Give back a reference to a vector consisting of the program arguments |
| 262 | | ///not starting with a '-' character. |
| 263 | | std::vector<std::string> &files() { return _file_args; } |
| 264 | | |
| 265 | | ///Give back the command name (the 0th argument) |
| 266 | | const std::string &commandName() { return _command_name; } |
| 267 | 273 | |
| 268 | 274 | void show(std::ostream &os,Opts::iterator i); |
| 269 | 275 | void show(std::ostream &os,Groups::iterator i); |
| … |
… |
|
| 286 | 292 | return parse(); |
| 287 | 293 | } |
| 288 | 294 | |
| | 295 | ///Give back the command name (the 0th argument) |
| | 296 | const std::string &commandName() { return _command_name; } |
| | 297 | |
| 289 | 298 | ///Check if an opion has been given to the command. |
| 290 | 299 | bool given(std::string op) |
| 291 | 300 | { |
| … |
… |
|
| 360 | 369 | { |
| 361 | 370 | return RefType(*this, n); |
| 362 | 371 | } |
| | 372 | |
| | 373 | ///Give back the non-option type arguments. |
| | 374 | |
| | 375 | ///Give back a reference to a vector consisting of the program arguments |
| | 376 | ///not starting with a '-' character. |
| | 377 | std::vector<std::string> &files() { return _file_args; } |
| 363 | 378 | |
| 364 | 379 | }; |
| 365 | 380 | } |
| 366 | 381 | |
| 367 | | |
| 368 | | |
| 369 | | #endif // LEMON_MAIN_PARAMS |
| | 382 | #endif // LEMON_ARG_PARSER |