Doc improvements related to ArgParser
authorPeter Kovacs <kpeter@inf.elte.hu>
Sat, 12 Jul 2008 10:21:44 +0200
changeset 20477d56a21c3ab
parent 202 a5ee729dc1e1
child 205 436fe75092b7
Doc improvements related to ArgParser
demo/arg_parser_demo.cc
lemon/arg_parser.h
     1.1 --- a/demo/arg_parser_demo.cc	Thu Jul 10 16:13:50 2008 +0200
     1.2 +++ b/demo/arg_parser_demo.cc	Sat Jul 12 10:21:44 2008 +0200
     1.3 @@ -29,43 +29,62 @@
     1.4  using namespace lemon;
     1.5  int main(int argc, const char **argv)
     1.6  {
     1.7 -  ArgParser ap(argc,argv);
     1.8 +  // Initialize the argument parser
     1.9 +  ArgParser ap(argc, argv);
    1.10    int i;
    1.11    std::string s;
    1.12 -  double d;
    1.13 -  bool b,sil;
    1.14 -  bool g1,g2,g3;
    1.15 -  ap.refOption("n", "An integer input.", i, true)
    1.16 -    .refOption("val", "A double input.", d)
    1.17 -    .doubleOption("val2", "A double input.", d)
    1.18 -    .synonym("vals","val")
    1.19 -    .refOption("name", "A string input.", s)
    1.20 -    .refOption("f", "A switch.", b)
    1.21 -    .refOption("nohelp", "", sil)
    1.22 -    .refOption("gra","Choice A",g1)
    1.23 -    .refOption("grb","Choice B",g2)
    1.24 -    .refOption("grc","Choice C",g3)
    1.25 -    .optionGroup("gr","gra")
    1.26 -    .optionGroup("gr","grb")
    1.27 -    .optionGroup("gr","grc")
    1.28 -    .mandatoryGroup("gr")
    1.29 -    .onlyOneGroup("gr")
    1.30 -    .other("infile","The input file.")
    1.31 +  double d = 1.0;
    1.32 +  bool b, nh;
    1.33 +  bool g1, g2, g3;
    1.34 +
    1.35 +  // Add a mandatory integer option with storage reference
    1.36 +  ap.refOption("n", "An integer input.", i, true);
    1.37 +  // Add a double option with storage reference (the default value is 1.0)
    1.38 +  ap.refOption("val", "A double input.", d);
    1.39 +  // Add a double option without storage reference (the default value is 3.14)
    1.40 +  ap.doubleOption("val2", "A double input.", 3.14);
    1.41 +  // Set synonym for -val option
    1.42 +  ap.synonym("vals", "val");
    1.43 +  // Add a string option
    1.44 +  ap.refOption("name", "A string input.", s);
    1.45 +  // Add bool options
    1.46 +  ap.refOption("f", "A switch.", b)
    1.47 +    .refOption("nohelp", "", nh)
    1.48 +    .refOption("gra", "Choice A", g1)
    1.49 +    .refOption("grb", "Choice B", g2)
    1.50 +    .refOption("grc", "Choice C", g3);
    1.51 +  // Bundle -gr* options into a group
    1.52 +  ap.optionGroup("gr", "gra")
    1.53 +    .optionGroup("gr", "grb")
    1.54 +    .optionGroup("gr", "grc");
    1.55 +  // Set the group mandatory
    1.56 +  ap.mandatoryGroup("gr");
    1.57 +  // Set the options of the group exclusive (only one option can be given)
    1.58 +  ap.onlyOneGroup("gr");
    1.59 +  // Add non-parsed arguments (e.g. input files)
    1.60 +  ap.other("infile", "The input file.")
    1.61      .other("...");
    1.62    
    1.63 +  // Perform the parsing process
    1.64 +  // (in case of any error it terminates the program)
    1.65    ap.parse();
    1.66  
    1.67 +  // Check each option if it has been given and print its value
    1.68    std::cout << "Parameters of '" << ap.commandName() << "':\n";
    1.69  
    1.70 -  if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
    1.71 +  std::cout << "  Value of -n: " << i << std::endl;
    1.72    if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
    1.73 +  if(ap.given("val2")) {
    1.74 +    d = ap["val2"];
    1.75 +    std::cout << "  Value of -val2: " << d << std::endl;
    1.76 +  }
    1.77    if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
    1.78    if(ap.given("f")) std::cout << "  -f is given\n";
    1.79 -  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
    1.80 +  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << nh << std::endl;
    1.81    if(ap.given("gra")) std::cout << "  -gra is given\n";
    1.82    if(ap.given("grb")) std::cout << "  -grb is given\n";
    1.83    if(ap.given("grc")) std::cout << "  -grc is given\n";
    1.84 -                                     
    1.85 +  
    1.86    switch(ap.files().size()) {
    1.87    case 0:
    1.88      std::cout << "  No file argument was given.\n";
    1.89 @@ -80,4 +99,5 @@
    1.90    for(unsigned int i=0;i<ap.files().size();++i)
    1.91      std::cout << "    '" << ap.files()[i] << "'\n";
    1.92    
    1.93 +  return 0;
    1.94  }
     2.1 --- a/lemon/arg_parser.h	Thu Jul 10 16:13:50 2008 +0200
     2.2 +++ b/lemon/arg_parser.h	Sat Jul 12 10:21:44 2008 +0200
     2.3 @@ -118,13 +118,19 @@
     2.4      
     2.5    public:
     2.6  
     2.7 -    ///\e
     2.8 +    ///Constructor
     2.9      ArgParser(int argc, const char **argv);
    2.10  
    2.11      ~ArgParser();
    2.12  
    2.13 +    ///\name Options
    2.14 +    ///
    2.15 +
    2.16 +    ///@{
    2.17 +
    2.18      ///Add a new integer type option
    2.19  
    2.20 +    ///Add a new integer type option.
    2.21      ///\param name The name of the option. The leading '-' must be omitted.
    2.22      ///\param help A help string.
    2.23      ///\param value A default value for the option.
    2.24 @@ -135,6 +141,7 @@
    2.25  
    2.26      ///Add a new floating point type option
    2.27  
    2.28 +    ///Add a new floating point type option.
    2.29      ///\param name The name of the option. The leading '-' must be omitted.
    2.30      ///\param help A help string.
    2.31      ///\param value A default value for the option.
    2.32 @@ -145,6 +152,7 @@
    2.33  
    2.34      ///Add a new bool type option
    2.35  
    2.36 +    ///Add a new bool type option.
    2.37      ///\param name The name of the option. The leading '-' must be omitted.
    2.38      ///\param help A help string.
    2.39      ///\param value A default value for the option.
    2.40 @@ -156,6 +164,7 @@
    2.41  
    2.42      ///Add a new string type option
    2.43  
    2.44 +    ///Add a new string type option.
    2.45      ///\param name The name of the option. The leading '-' must be omitted.
    2.46      ///\param help A help string.
    2.47      ///\param value A default value for the option.
    2.48 @@ -164,7 +173,17 @@
    2.49  		      const std::string &help,
    2.50  		      std::string value="", bool obl=false);
    2.51  
    2.52 -    ///\name Options with external storage
    2.53 +    ///Give help string for non-parsed arguments.
    2.54 +
    2.55 +    ///With this function you can give help string for non-parsed arguments.
    2.56 +    ///The parameter \c name will be printed in the short usage line, while
    2.57 +    ///\c help gives a more detailed description.
    2.58 +    ArgParser &other(const std::string &name,
    2.59 +		     const std::string &help="");
    2.60 +    
    2.61 +    ///@}
    2.62 +
    2.63 +    ///\name Options with External Storage
    2.64      ///Using this functions, the value of the option will be directly written
    2.65      ///into a variable once the option appears in the command line.
    2.66  
    2.67 @@ -172,6 +191,7 @@
    2.68  
    2.69      ///Add a new integer type option with a storage reference
    2.70  
    2.71 +    ///Add a new integer type option with a storage reference.
    2.72      ///\param name The name of the option. The leading '-' must be omitted.
    2.73      ///\param help A help string.
    2.74      ///\param obl Indicate if the option is mandatory.
    2.75 @@ -182,6 +202,7 @@
    2.76  
    2.77      ///Add a new floating type option with a storage reference
    2.78  
    2.79 +    ///Add a new floating type option with a storage reference.
    2.80      ///\param name The name of the option. The leading '-' must be omitted.
    2.81      ///\param help A help string.
    2.82      ///\param obl Indicate if the option is mandatory.
    2.83 @@ -192,6 +213,7 @@
    2.84  
    2.85      ///Add a new bool type option with a storage reference
    2.86  
    2.87 +    ///Add a new bool type option with a storage reference.
    2.88      ///\param name The name of the option. The leading '-' must be omitted.
    2.89      ///\param help A help string.
    2.90      ///\param obl Indicate if the option is mandatory.
    2.91 @@ -203,6 +225,7 @@
    2.92  
    2.93      ///Add a new string type option with a storage reference
    2.94  
    2.95 +    ///Add a new string type option with a storage reference.
    2.96      ///\param name The name of the option. The leading '-' must be omitted.
    2.97      ///\param help A help string.
    2.98      ///\param obl Indicate if the option is mandatory.
    2.99 @@ -218,7 +241,7 @@
   2.100      
   2.101      ///@{
   2.102  
   2.103 -    ///Boundle some options into a group
   2.104 +    ///Bundle some options into a group
   2.105  
   2.106      /// You can group some option by calling this function repeatedly for each
   2.107      /// option to be grouped with the same groupname.
   2.108 @@ -230,7 +253,7 @@
   2.109      ///Make the members of a group exclusive
   2.110  
   2.111      ///If you call this function for a group, than at most one of them can be
   2.112 -    ///given at the same time
   2.113 +    ///given at the same time.
   2.114      ArgParser &onlyOneGroup(const std::string &group);
   2.115    
   2.116      ///Make a group mandatory
   2.117 @@ -248,23 +271,6 @@
   2.118      
   2.119      ///@}
   2.120  
   2.121 -    ///Give help string for non-parsed arguments.
   2.122 -
   2.123 -    ///With this function you can give help string for non-parsed arguments.
   2.124 -    ///The parameter \c name will be printed in the short usage line, while
   2.125 -    ///\c help gives a more detailed description.
   2.126 -    ArgParser &other(const std::string &name,
   2.127 -		     const std::string &help="");
   2.128 -    
   2.129 -    ///Give back the non-option type arguments.
   2.130 -
   2.131 -    ///Give back a reference to a vector consisting of the program arguments
   2.132 -    ///not starting with a '-' character.
   2.133 -    std::vector<std::string> &files() { return _file_args; }
   2.134 -
   2.135 -    ///Give back the command name (the 0th argument)
   2.136 -    const std::string &commandName() { return _command_name; }
   2.137 -
   2.138      void show(std::ostream &os,Opts::iterator i);
   2.139      void show(std::ostream &os,Groups::iterator i);
   2.140      void showHelp(Opts::iterator i);
   2.141 @@ -286,6 +292,9 @@
   2.142        return parse();
   2.143      }
   2.144      
   2.145 +    ///Give back the command name (the 0th argument)
   2.146 +    const std::string &commandName() { return _command_name; }
   2.147 +
   2.148      ///Check if an opion has been given to the command.
   2.149      bool given(std::string op) 
   2.150      {
   2.151 @@ -360,10 +369,14 @@
   2.152      {
   2.153        return RefType(*this, n);
   2.154      }    
   2.155 +
   2.156 +    ///Give back the non-option type arguments.
   2.157 +
   2.158 +    ///Give back a reference to a vector consisting of the program arguments
   2.159 +    ///not starting with a '-' character.
   2.160 +    std::vector<std::string> &files() { return _file_args; }
   2.161   
   2.162    };
   2.163  }
   2.164  
   2.165 -    
   2.166 -
   2.167 -#endif // LEMON_MAIN_PARAMS
   2.168 +#endif // LEMON_ARG_PARSER