demo/arg_parser_demo.cc
changeset 87 81e138275860
child 88 18444049848b
equal deleted inserted replaced
-1:000000000000 0:66be6efb23d5
       
     1 /* -*- C++ -*-
       
     2  *
       
     3  * This file is a part of LEMON, a generic C++ optimization library
       
     4  *
       
     5  * Copyright (C) 2003-2008
       
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
       
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
       
     8  *
       
     9  * Permission to use, modify and distribute this software is granted
       
    10  * provided that this copyright notice appears in all copies. For
       
    11  * precise terms see the accompanying LICENSE file.
       
    12  *
       
    13  * This software is provided "AS IS" with no warranty of any kind,
       
    14  * express or implied, and with no claim as to its suitability for any
       
    15  * purpose.
       
    16  *
       
    17  */
       
    18 
       
    19 ///\ingroup demos
       
    20 ///\file
       
    21 ///\brief Argument parser demo
       
    22 ///
       
    23 /// This example shows how can the argument parser used.
       
    24 ///
       
    25 /// \include arg_parser.cc
       
    26 
       
    27 #include <lemon/arg_parser.h>
       
    28 
       
    29 using namespace lemon;
       
    30 int main(int argc, const char **argv)
       
    31 {
       
    32   ArgParser ap(argc,argv);
       
    33   int i;
       
    34   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     .synonym("vals","val")
       
    41     .refOption("name", "a string input", s)
       
    42     .refOption("f", "a switch", b)
       
    43     .refOption("nohelp", "", sil)
       
    44     .refOption("gra","Choise A",g1)
       
    45     .refOption("grb","Choise B",g2)
       
    46     .refOption("grc","Choise C",g3)
       
    47     .optionGroup("gr","gra")
       
    48     .optionGroup("gr","grb")
       
    49     .optionGroup("gr","grc")
       
    50     .mandatoryGroup("gr")
       
    51     .onlyOneGroup("gr")
       
    52     .other("infile","The input file")
       
    53     .other("...");
       
    54   
       
    55   ap.parse();
       
    56 
       
    57   std::cout << "Parameters of '" << ap.commandName() << "':\n";
       
    58 
       
    59   if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
       
    60   if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
       
    61   if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
       
    62   if(ap.given("f")) std::cout << "  -f is given\n";
       
    63   if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
       
    64 
       
    65   switch(ap.files().size()) {
       
    66   case 0:
       
    67     std::cout << "  No file argument was given.\n";
       
    68     break;
       
    69   case 1:
       
    70     std::cout << "  1 file argument was given. It is:\n";
       
    71     break;
       
    72   default:
       
    73     std::cout << "  "
       
    74 	      << ap.files().size() << " file arguments were given. They are:\n";
       
    75   }
       
    76   for(unsigned int i=0;i<ap.files().size();++i)
       
    77     std::cout << "    '" << ap.files()[i] << "'\n";
       
    78   
       
    79 }