diff --git a/demo/arg_parser_demo.cc b/demo/arg_parser_demo.cc --- a/demo/arg_parser_demo.cc +++ b/demo/arg_parser_demo.cc @@ -29,43 +29,62 @@ using namespace lemon; int main(int argc, const char **argv) { - ArgParser ap(argc,argv); + // Initialize the argument parser + ArgParser ap(argc, argv); int i; std::string s; - double d; - bool b,sil; - bool g1,g2,g3; - ap.refOption("n", "An integer input.", i, true) - .refOption("val", "A double input.", d) - .doubleOption("val2", "A double input.", d) - .synonym("vals","val") - .refOption("name", "A string input.", s) - .refOption("f", "A switch.", b) - .refOption("nohelp", "", sil) - .refOption("gra","Choice A",g1) - .refOption("grb","Choice B",g2) - .refOption("grc","Choice C",g3) - .optionGroup("gr","gra") - .optionGroup("gr","grb") - .optionGroup("gr","grc") - .mandatoryGroup("gr") - .onlyOneGroup("gr") - .other("infile","The input file.") + double d = 1.0; + bool b, nh; + bool g1, g2, g3; + + // Add a mandatory integer option with storage reference + ap.refOption("n", "An integer input.", i, true); + // Add a double option with storage reference (the default value is 1.0) + ap.refOption("val", "A double input.", d); + // Add a double option without storage reference (the default value is 3.14) + ap.doubleOption("val2", "A double input.", 3.14); + // Set synonym for -val option + ap.synonym("vals", "val"); + // Add a string option + ap.refOption("name", "A string input.", s); + // Add bool options + ap.refOption("f", "A switch.", b) + .refOption("nohelp", "", nh) + .refOption("gra", "Choice A", g1) + .refOption("grb", "Choice B", g2) + .refOption("grc", "Choice C", g3); + // Bundle -gr* options into a group + ap.optionGroup("gr", "gra") + .optionGroup("gr", "grb") + .optionGroup("gr", "grc"); + // Set the group mandatory + ap.mandatoryGroup("gr"); + // Set the options of the group exclusive (only one option can be given) + ap.onlyOneGroup("gr"); + // Add non-parsed arguments (e.g. input files) + ap.other("infile", "The input file.") .other("..."); + // Perform the parsing process + // (in case of any error it terminates the program) ap.parse(); + // Check each option if it has been given and print its value std::cout << "Parameters of '" << ap.commandName() << "':\n"; - if(ap.given("n")) std::cout << " Value of -n: " << i << std::endl; + std::cout << " Value of -n: " << i << std::endl; if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; + if(ap.given("val2")) { + d = ap["val2"]; + std::cout << " Value of -val2: " << d << std::endl; + } if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; if(ap.given("f")) std::cout << " -f is given\n"; - if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << sil << std::endl; + if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl; if(ap.given("gra")) std::cout << " -gra is given\n"; if(ap.given("grb")) std::cout << " -grb is given\n"; if(ap.given("grc")) std::cout << " -grc is given\n"; - + switch(ap.files().size()) { case 0: std::cout << " No file argument was given.\n"; @@ -80,4 +99,5 @@ for(unsigned int i=0;i &files() { return _file_args; } - - ///Give back the command name (the 0th argument) - const std::string &commandName() { return _command_name; } - void show(std::ostream &os,Opts::iterator i); void show(std::ostream &os,Groups::iterator i); void showHelp(Opts::iterator i); @@ -286,6 +292,9 @@ return parse(); } + ///Give back the command name (the 0th argument) + const std::string &commandName() { return _command_name; } + ///Check if an opion has been given to the command. bool given(std::string op) { @@ -360,10 +369,14 @@ { return RefType(*this, n); } + + ///Give back the non-option type arguments. + + ///Give back a reference to a vector consisting of the program arguments + ///not starting with a '-' character. + std::vector &files() { return _file_args; } }; } - - -#endif // LEMON_MAIN_PARAMS +#endif // LEMON_ARG_PARSER