COIN-OR::LEMON - Graph Library

Ticket #104: arg_parser_doc_1767a4db2b8c.patch

File arg_parser_doc_1767a4db2b8c.patch, 9.0 KB (added by Peter Kovacs, 11 years ago)

Doc improvements

  • demo/arg_parser_demo.cc

    # HG changeset patch
    # User Peter Kovacs <kpeter@inf.elte.hu>
    # Date 1215820053 -7200
    # Node ID 1767a4db2b8cf3c2dec95c5d2a5f0500b6e2d17e
    # Parent  a5ee729dc1e10eb45f0925d2956766671f1c3d63
    Doc improvements related to ArgParser
    
    diff -r a5ee729dc1e1 -r 1767a4db2b8c demo/arg_parser_demo.cc
    a b  
    2929using namespace lemon;
    3030int main(int argc, const char **argv)
    3131{
    32   ArgParser ap(argc,argv);
     32  // Initialize the argument parser
     33  ArgParser ap(argc, argv);
    3334  int i;
    3435  std::string s;
    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.")
     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.")
    5466    .other("...");
    55  
     67
     68  // Perform the parsing process
     69  // (in case of any error it terminates the program)
    5670  ap.parse();
    5771
     72  // Check each option if it has been given and print its value
    5873  std::cout << "Parameters of '" << ap.commandName() << "':\n";
    5974
    60   if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
     75  std::cout << "  Value of -n: " << i << std::endl;
    6176  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  }
    6281  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
    6382  if(ap.given("f")) std::cout << "  -f is given\n";
    64   if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
     83  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << nh << std::endl;
    6584  if(ap.given("gra")) std::cout << "  -gra is given\n";
    6685  if(ap.given("grb")) std::cout << "  -grb is given\n";
    6786  if(ap.given("grc")) std::cout << "  -grc is given\n";
    68                                      
     87
    6988  switch(ap.files().size()) {
    7089  case 0:
    7190    std::cout << "  No file argument was given.\n";
     
    7998  }
    8099  for(unsigned int i=0;i<ap.files().size();++i)
    81100    std::cout << "    '" << ap.files()[i] << "'\n";
    82  
     101
     102  return 0;
    83103}
  • lemon/arg_parser.h

    diff -r a5ee729dc1e1 -r 1767a4db2b8c lemon/arg_parser.h
    a b  
    118118   
    119119  public:
    120120
    121     ///\e
     121    ///Constructor
    122122    ArgParser(int argc, const char **argv);
    123123
    124124    ~ArgParser();
    125125
     126    ///\name Options
     127    ///
     128
     129    ///@{
     130
    126131    ///Add a new integer type option
    127132
     133    ///Add a new integer type option.
    128134    ///\param name The name of the option. The leading '-' must be omitted.
    129135    ///\param help A help string.
    130136    ///\param value A default value for the option.
     
    135141
    136142    ///Add a new floating point type option
    137143
     144    ///Add a new floating point type option.
    138145    ///\param name The name of the option. The leading '-' must be omitted.
    139146    ///\param help A help string.
    140147    ///\param value A default value for the option.
     
    145152
    146153    ///Add a new bool type option
    147154
     155    ///Add a new bool type option.
    148156    ///\param name The name of the option. The leading '-' must be omitted.
    149157    ///\param help A help string.
    150158    ///\param value A default value for the option.
     
    156164
    157165    ///Add a new string type option
    158166
     167    ///Add a new string type option.
    159168    ///\param name The name of the option. The leading '-' must be omitted.
    160169    ///\param help A help string.
    161170    ///\param value A default value for the option.
     
    164173                      const std::string &help,
    165174                      std::string value="", bool obl=false);
    166175
    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
    168187    ///Using this functions, the value of the option will be directly written
    169188    ///into a variable once the option appears in the command line.
    170189
     
    172191
    173192    ///Add a new integer type option with a storage reference
    174193
     194    ///Add a new integer type option with a storage reference.
    175195    ///\param name The name of the option. The leading '-' must be omitted.
    176196    ///\param help A help string.
    177197    ///\param obl Indicate if the option is mandatory.
     
    182202
    183203    ///Add a new floating type option with a storage reference
    184204
     205    ///Add a new floating type option with a storage reference.
    185206    ///\param name The name of the option. The leading '-' must be omitted.
    186207    ///\param help A help string.
    187208    ///\param obl Indicate if the option is mandatory.
     
    192213
    193214    ///Add a new bool type option with a storage reference
    194215
     216    ///Add a new bool type option with a storage reference.
    195217    ///\param name The name of the option. The leading '-' must be omitted.
    196218    ///\param help A help string.
    197219    ///\param obl Indicate if the option is mandatory.
     
    203225
    204226    ///Add a new string type option with a storage reference
    205227
     228    ///Add a new string type option with a storage reference.
    206229    ///\param name The name of the option. The leading '-' must be omitted.
    207230    ///\param help A help string.
    208231    ///\param obl Indicate if the option is mandatory.
     
    218241   
    219242    ///@{
    220243
    221     ///Boundle some options into a group
     244    ///Bundle some options into a group
    222245
    223246    /// You can group some option by calling this function repeatedly for each
    224247    /// option to be grouped with the same groupname.
     
    230253    ///Make the members of a group exclusive
    231254
    232255    ///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.
    234257    ArgParser &onlyOneGroup(const std::string &group);
    235258 
    236259    ///Make a group mandatory
     
    247270                           const std::string &opt);
    248271   
    249272    ///@}
    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; }
    267273
    268274    void show(std::ostream &os,Opts::iterator i);
    269275    void show(std::ostream &os,Groups::iterator i);
     
    286292      return parse();
    287293    }
    288294   
     295    ///Give back the command name (the 0th argument)
     296    const std::string &commandName() { return _command_name; }
     297
    289298    ///Check if an opion has been given to the command.
    290299    bool given(std::string op)
    291300    {
     
    360369    {
    361370      return RefType(*this, n);
    362371    }   
     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; }
    363378 
    364379  };
    365380}
    366381
    367    
    368 
    369 #endif // LEMON_MAIN_PARAMS
     382#endif // LEMON_ARG_PARSER