COIN-OR::LEMON - Graph Library

Changeset 204:77d56a21c3ab in lemon-1.2 for demo/arg_parser_demo.cc


Ignore:
Timestamp:
07/12/08 10:21:44 (16 years ago)
Author:
Peter Kovacs <kpeter@…>
Branch:
default
Phase:
public
Message:

Doc improvements related to ArgParser?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • demo/arg_parser_demo.cc

    r137 r204  
    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("...");
    5567 
     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:
     
    81100    std::cout << "    '" << ap.files()[i] << "'\n";
    82101 
     102  return 0;
    83103}
Note: See TracChangeset for help on using the changeset viewer.