demo/arg_parser_demo.cc
changeset 204 77d56a21c3ab
parent 137 483bc6ed7292
child 209 765619b7cbb2
     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  }