[Lemon-commits] Peter Kovacs: Doc improvements related to ArgParser
Lemon HG
hg at lemon.cs.elte.hu
Sat Jul 12 10:52:15 CEST 2008
details: http://lemon.cs.elte.hu/hg/lemon/rev/77d56a21c3ab
changeset: 204:77d56a21c3ab
user: Peter Kovacs <kpeter [at] inf.elte.hu>
date: Sat Jul 12 10:21:44 2008 +0200
description:
Doc improvements related to ArgParser
diffstat:
2 files changed, 80 insertions(+), 47 deletions(-)
demo/arg_parser_demo.cc | 66 ++++++++++++++++++++++++++++++-----------------
lemon/arg_parser.h | 61 ++++++++++++++++++++++++++-----------------
diffs (264 lines):
diff -r a5ee729dc1e1 -r 77d56a21c3ab demo/arg_parser_demo.cc
--- a/demo/arg_parser_demo.cc Thu Jul 10 16:13:50 2008 +0200
+++ b/demo/arg_parser_demo.cc Sat Jul 12 10:21:44 2008 +0200
@@ -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<ap.files().size();++i)
std::cout << " '" << ap.files()[i] << "'\n";
+ return 0;
}
diff -r a5ee729dc1e1 -r 77d56a21c3ab lemon/arg_parser.h
--- a/lemon/arg_parser.h Thu Jul 10 16:13:50 2008 +0200
+++ b/lemon/arg_parser.h Sat Jul 12 10:21:44 2008 +0200
@@ -118,13 +118,19 @@
public:
- ///\e
+ ///Constructor
ArgParser(int argc, const char **argv);
~ArgParser();
+ ///\name Options
+ ///
+
+ ///@{
+
///Add a new integer type option
+ ///Add a new integer type option.
///\param name The name of the option. The leading '-' must be omitted.
///\param help A help string.
///\param value A default value for the option.
@@ -135,6 +141,7 @@
///Add a new floating point type option
+ ///Add a new floating point type option.
///\param name The name of the option. The leading '-' must be omitted.
///\param help A help string.
///\param value A default value for the option.
@@ -145,6 +152,7 @@
///Add a new bool type option
+ ///Add a new bool type option.
///\param name The name of the option. The leading '-' must be omitted.
///\param help A help string.
///\param value A default value for the option.
@@ -156,6 +164,7 @@
///Add a new string type option
+ ///Add a new string type option.
///\param name The name of the option. The leading '-' must be omitted.
///\param help A help string.
///\param value A default value for the option.
@@ -164,7 +173,17 @@
const std::string &help,
std::string value="", bool obl=false);
- ///\name Options with external storage
+ ///Give help string for non-parsed arguments.
+
+ ///With this function you can give help string for non-parsed arguments.
+ ///The parameter \c name will be printed in the short usage line, while
+ ///\c help gives a more detailed description.
+ ArgParser &other(const std::string &name,
+ const std::string &help="");
+
+ ///@}
+
+ ///\name Options with External Storage
///Using this functions, the value of the option will be directly written
///into a variable once the option appears in the command line.
@@ -172,6 +191,7 @@
///Add a new integer type option with a storage reference
+ ///Add a new integer type option with a storage reference.
///\param name The name of the option. The leading '-' must be omitted.
///\param help A help string.
///\param obl Indicate if the option is mandatory.
@@ -182,6 +202,7 @@
///Add a new floating type option with a storage reference
+ ///Add a new floating type option with a storage reference.
///\param name The name of the option. The leading '-' must be omitted.
///\param help A help string.
///\param obl Indicate if the option is mandatory.
@@ -192,6 +213,7 @@
///Add a new bool type option with a storage reference
+ ///Add a new bool type option with a storage reference.
///\param name The name of the option. The leading '-' must be omitted.
///\param help A help string.
///\param obl Indicate if the option is mandatory.
@@ -203,6 +225,7 @@
///Add a new string type option with a storage reference
+ ///Add a new string type option with a storage reference.
///\param name The name of the option. The leading '-' must be omitted.
///\param help A help string.
///\param obl Indicate if the option is mandatory.
@@ -218,7 +241,7 @@
///@{
- ///Boundle some options into a group
+ ///Bundle some options into a group
/// You can group some option by calling this function repeatedly for each
/// option to be grouped with the same groupname.
@@ -230,7 +253,7 @@
///Make the members of a group exclusive
///If you call this function for a group, than at most one of them can be
- ///given at the same time
+ ///given at the same time.
ArgParser &onlyOneGroup(const std::string &group);
///Make a group mandatory
@@ -247,23 +270,6 @@
const std::string &opt);
///@}
-
- ///Give help string for non-parsed arguments.
-
- ///With this function you can give help string for non-parsed arguments.
- ///The parameter \c name will be printed in the short usage line, while
- ///\c help gives a more detailed description.
- ArgParser &other(const std::string &name,
- const std::string &help="");
-
- ///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<std::string> &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);
@@ -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<std::string> &files() { return _file_args; }
};
}
-
-
-#endif // LEMON_MAIN_PARAMS
+#endif // LEMON_ARG_PARSER
More information about the Lemon-commits
mailing list