Changes in / [205:436fe75092b7:203:215bfc30b14f] in lemon-main
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
demo/arg_parser_demo.cc
r204 r137 30 30 int main(int argc, const char **argv) 31 31 { 32 // Initialize the argument parser 33 ArgParser ap(argc, argv); 32 ArgParser ap(argc,argv); 34 33 int i; 35 34 std::string s; 36 double d = 1.0; 37 bool b, nh; 38 bool g1, g2, g3; 39 40 // Add a mandatory integer option with storage reference 41 ap.refOption("n", "An integer input.", i, true); 42 // Add a double option with storage reference (the default value is 1.0) 43 ap.refOption("val", "A double input.", d); 44 // Add a double option without storage reference (the default value is 3.14) 45 ap.doubleOption("val2", "A double input.", 3.14); 46 // Set synonym for -val option 47 ap.synonym("vals", "val"); 48 // Add a string option 49 ap.refOption("name", "A string input.", s); 50 // Add bool options 51 ap.refOption("f", "A switch.", b) 52 .refOption("nohelp", "", nh) 53 .refOption("gra", "Choice A", g1) 54 .refOption("grb", "Choice B", g2) 55 .refOption("grc", "Choice C", g3); 56 // Bundle -gr* options into a group 57 ap.optionGroup("gr", "gra") 58 .optionGroup("gr", "grb") 59 .optionGroup("gr", "grc"); 60 // Set the group mandatory 61 ap.mandatoryGroup("gr"); 62 // Set the options of the group exclusive (only one option can be given) 63 ap.onlyOneGroup("gr"); 64 // Add non-parsed arguments (e.g. input files) 65 ap.other("infile", "The input file.") 35 double d; 36 bool b,sil; 37 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) 43 .refOption("f", "A switch.", b) 44 .refOption("nohelp", "", sil) 45 .refOption("gra","Choice A",g1) 46 .refOption("grb","Choice B",g2) 47 .refOption("grc","Choice C",g3) 48 .optionGroup("gr","gra") 49 .optionGroup("gr","grb") 50 .optionGroup("gr","grc") 51 .mandatoryGroup("gr") 52 .onlyOneGroup("gr") 53 .other("infile","The input file.") 66 54 .other("..."); 67 55 68 // Perform the parsing process69 // (in case of any error it terminates the program)70 56 ap.parse(); 71 57 72 // Check each option if it has been given and print its value73 58 std::cout << "Parameters of '" << ap.commandName() << "':\n"; 74 59 75 std::cout << " Value of -n: " << i << std::endl;60 if(ap.given("n")) std::cout << " Value of -n: " << i << std::endl; 76 61 if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; 77 if(ap.given("val2")) {78 d = ap["val2"];79 std::cout << " Value of -val2: " << d << std::endl;80 }81 62 if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; 82 63 if(ap.given("f")) std::cout << " -f is given\n"; 83 if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh<< std::endl;64 if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << sil << std::endl; 84 65 if(ap.given("gra")) std::cout << " -gra is given\n"; 85 66 if(ap.given("grb")) std::cout << " -grb is given\n"; 86 67 if(ap.given("grc")) std::cout << " -grc is given\n"; 87 68 88 69 switch(ap.files().size()) { 89 70 case 0: … … 100 81 std::cout << " '" << ap.files()[i] << "'\n"; 101 82 102 return 0;103 83 } -
lemon/arg_parser.h
r204 r108 119 119 public: 120 120 121 /// Constructor121 ///\e 122 122 ArgParser(int argc, const char **argv); 123 123 124 124 ~ArgParser(); 125 125 126 ///\name Options127 ///128 129 ///@{130 131 126 ///Add a new integer type option 132 127 133 ///Add a new integer type option.134 128 ///\param name The name of the option. The leading '-' must be omitted. 135 129 ///\param help A help string. … … 142 136 ///Add a new floating point type option 143 137 144 ///Add a new floating point type option.145 138 ///\param name The name of the option. The leading '-' must be omitted. 146 139 ///\param help A help string. … … 153 146 ///Add a new bool type option 154 147 155 ///Add a new bool type option.156 148 ///\param name The name of the option. The leading '-' must be omitted. 157 149 ///\param help A help string. … … 165 157 ///Add a new string type option 166 158 167 ///Add a new string type option.168 159 ///\param name The name of the option. The leading '-' must be omitted. 169 160 ///\param help A help string. … … 174 165 std::string value="", bool obl=false); 175 166 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 167 ///\name Options with external storage 187 168 ///Using this functions, the value of the option will be directly written 188 169 ///into a variable once the option appears in the command line. … … 192 173 ///Add a new integer type option with a storage reference 193 174 194 ///Add a new integer type option with a storage reference.195 175 ///\param name The name of the option. The leading '-' must be omitted. 196 176 ///\param help A help string. … … 203 183 ///Add a new floating type option with a storage reference 204 184 205 ///Add a new floating type option with a storage reference.206 185 ///\param name The name of the option. The leading '-' must be omitted. 207 186 ///\param help A help string. … … 214 193 ///Add a new bool type option with a storage reference 215 194 216 ///Add a new bool type option with a storage reference.217 195 ///\param name The name of the option. The leading '-' must be omitted. 218 196 ///\param help A help string. … … 226 204 ///Add a new string type option with a storage reference 227 205 228 ///Add a new string type option with a storage reference.229 206 ///\param name The name of the option. The leading '-' must be omitted. 230 207 ///\param help A help string. … … 242 219 ///@{ 243 220 244 ///B undle some options into a group221 ///Boundle some options into a group 245 222 246 223 /// You can group some option by calling this function repeatedly for each … … 254 231 255 232 ///If you call this function for a group, than at most one of them can be 256 ///given at the same time .233 ///given at the same time 257 234 ArgParser &onlyOneGroup(const std::string &group); 258 235 … … 271 248 272 249 ///@} 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; } 273 267 274 268 void show(std::ostream &os,Opts::iterator i); … … 293 287 } 294 288 295 ///Give back the command name (the 0th argument)296 const std::string &commandName() { return _command_name; }297 298 289 ///Check if an opion has been given to the command. 299 290 bool given(std::string op) … … 370 361 return RefType(*this, n); 371 362 } 372 373 ///Give back the non-option type arguments.374 375 ///Give back a reference to a vector consisting of the program arguments376 ///not starting with a '-' character.377 std::vector<std::string> &files() { return _file_args; }378 363 379 364 }; 380 365 } 381 366 382 #endif // LEMON_ARG_PARSER 367 368 369 #endif // LEMON_MAIN_PARAMS
Note: See TracChangeset
for help on using the changeset viewer.