demo/arg_parser_demo.cc
author Balazs Dezso <deba@inf.elte.hu>
Tue, 25 Mar 2008 16:36:44 +0100
changeset 108 889d0c289d19
parent 85 3453d20a82cd
child 128 7cd965d2257f
permissions -rw-r--r--
Reworking assertions and moving to distinict file
     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 the argument parser can be used.
    24 ///
    25 /// \include arg_parser_demo.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","Choice A",g1)
    45     .refOption("grb","Choice B",g2)
    46     .refOption("grc","Choice 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   if(ap.given("gra")) std::cout << "  -gra is given\n";
    65   if(ap.given("grb")) std::cout << "  -grb is given\n";
    66   if(ap.given("grc")) std::cout << "  -grc is given\n";
    67                                      
    68   switch(ap.files().size()) {
    69   case 0:
    70     std::cout << "  No file argument was given.\n";
    71     break;
    72   case 1:
    73     std::cout << "  1 file argument was given. It is:\n";
    74     break;
    75   default:
    76     std::cout << "  "
    77 	      << ap.files().size() << " file arguments were given. They are:\n";
    78   }
    79   for(unsigned int i=0;i<ap.files().size();++i)
    80     std::cout << "    '" << ap.files()[i] << "'\n";
    81   
    82 }