demo/arg_parser_demo.cc
changeset 207 574b963d0275
parent 137 483bc6ed7292
child 209 765619b7cbb2
equal deleted inserted replaced
3:965f94bf587f 4:bbf0ebcf7131
    27 #include <lemon/arg_parser.h>
    27 #include <lemon/arg_parser.h>
    28 
    28 
    29 using namespace lemon;
    29 using namespace lemon;
    30 int main(int argc, const char **argv)
    30 int main(int argc, const char **argv)
    31 {
    31 {
    32   ArgParser ap(argc,argv);
    32   // Initialize the argument parser
       
    33   ArgParser ap(argc, argv);
    33   int i;
    34   int i;
    34   std::string s;
    35   std::string s;
    35   double d;
    36   double d = 1.0;
    36   bool b,sil;
    37   bool b, nh;
    37   bool g1,g2,g3;
    38   bool g1, g2, g3;
    38   ap.refOption("n", "An integer input.", i, true)
    39 
    39     .refOption("val", "A double input.", d)
    40   // Add a mandatory integer option with storage reference
    40     .doubleOption("val2", "A double input.", d)
    41   ap.refOption("n", "An integer input.", i, true);
    41     .synonym("vals","val")
    42   // Add a double option with storage reference (the default value is 1.0)
    42     .refOption("name", "A string input.", s)
    43   ap.refOption("val", "A double input.", d);
    43     .refOption("f", "A switch.", b)
    44   // Add a double option without storage reference (the default value is 3.14)
    44     .refOption("nohelp", "", sil)
    45   ap.doubleOption("val2", "A double input.", 3.14);
    45     .refOption("gra","Choice A",g1)
    46   // Set synonym for -val option
    46     .refOption("grb","Choice B",g2)
    47   ap.synonym("vals", "val");
    47     .refOption("grc","Choice C",g3)
    48   // Add a string option
    48     .optionGroup("gr","gra")
    49   ap.refOption("name", "A string input.", s);
    49     .optionGroup("gr","grb")
    50   // Add bool options
    50     .optionGroup("gr","grc")
    51   ap.refOption("f", "A switch.", b)
    51     .mandatoryGroup("gr")
    52     .refOption("nohelp", "", nh)
    52     .onlyOneGroup("gr")
    53     .refOption("gra", "Choice A", g1)
    53     .other("infile","The input file.")
    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.")
    54     .other("...");
    66     .other("...");
    55   
    67   
       
    68   // Perform the parsing process
       
    69   // (in case of any error it terminates the program)
    56   ap.parse();
    70   ap.parse();
    57 
    71 
       
    72   // Check each option if it has been given and print its value
    58   std::cout << "Parameters of '" << ap.commandName() << "':\n";
    73   std::cout << "Parameters of '" << ap.commandName() << "':\n";
    59 
    74 
    60   if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
    75   std::cout << "  Value of -n: " << i << std::endl;
    61   if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
    76   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   }
    62   if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
    81   if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
    63   if(ap.given("f")) std::cout << "  -f is given\n";
    82   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;
    65   if(ap.given("gra")) std::cout << "  -gra is given\n";
    84   if(ap.given("gra")) std::cout << "  -gra is given\n";
    66   if(ap.given("grb")) std::cout << "  -grb is given\n";
    85   if(ap.given("grb")) std::cout << "  -grb is given\n";
    67   if(ap.given("grc")) std::cout << "  -grc is given\n";
    86   if(ap.given("grc")) std::cout << "  -grc is given\n";
    68                                      
    87   
    69   switch(ap.files().size()) {
    88   switch(ap.files().size()) {
    70   case 0:
    89   case 0:
    71     std::cout << "  No file argument was given.\n";
    90     std::cout << "  No file argument was given.\n";
    72     break;
    91     break;
    73   case 1:
    92   case 1:
    78 	      << ap.files().size() << " file arguments were given. They are:\n";
    97 	      << ap.files().size() << " file arguments were given. They are:\n";
    79   }
    98   }
    80   for(unsigned int i=0;i<ap.files().size();++i)
    99   for(unsigned int i=0;i<ap.files().size();++i)
    81     std::cout << "    '" << ap.files()[i] << "'\n";
   100     std::cout << "    '" << ap.files()[i] << "'\n";
    82   
   101   
       
   102   return 0;
    83 }
   103 }