demo/arg_parser_demo.cc
changeset 89 e6452a49192c
parent 85 3453d20a82cd
child 128 7cd965d2257f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/demo/arg_parser_demo.cc	Mon Mar 17 11:03:35 2008 +0000
     1.3 @@ -0,0 +1,82 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +///\ingroup demos
    1.23 +///\file
    1.24 +///\brief Argument parser demo
    1.25 +///
    1.26 +/// This example shows how the argument parser can be used.
    1.27 +///
    1.28 +/// \include arg_parser_demo.cc
    1.29 +
    1.30 +#include <lemon/arg_parser.h>
    1.31 +
    1.32 +using namespace lemon;
    1.33 +int main(int argc, const char **argv)
    1.34 +{
    1.35 +  ArgParser ap(argc,argv);
    1.36 +  int i;
    1.37 +  std::string s;
    1.38 +  double d;
    1.39 +  bool b,sil;
    1.40 +  bool g1,g2,g3;
    1.41 +  ap.refOption("n", "An integer input.", i, true)
    1.42 +    .refOption("val", "A double input.", d)
    1.43 +    .synonym("vals","val")
    1.44 +    .refOption("name", "A string input.", s)
    1.45 +    .refOption("f", "A switch.", b)
    1.46 +    .refOption("nohelp", "", sil)
    1.47 +    .refOption("gra","Choice A",g1)
    1.48 +    .refOption("grb","Choice B",g2)
    1.49 +    .refOption("grc","Choice C",g3)
    1.50 +    .optionGroup("gr","gra")
    1.51 +    .optionGroup("gr","grb")
    1.52 +    .optionGroup("gr","grc")
    1.53 +    .mandatoryGroup("gr")
    1.54 +    .onlyOneGroup("gr")
    1.55 +    .other("infile","The input file.")
    1.56 +    .other("...");
    1.57 +  
    1.58 +  ap.parse();
    1.59 +
    1.60 +  std::cout << "Parameters of '" << ap.commandName() << "':\n";
    1.61 +
    1.62 +  if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
    1.63 +  if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
    1.64 +  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
    1.65 +  if(ap.given("f")) std::cout << "  -f is given\n";
    1.66 +  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
    1.67 +  if(ap.given("gra")) std::cout << "  -gra is given\n";
    1.68 +  if(ap.given("grb")) std::cout << "  -grb is given\n";
    1.69 +  if(ap.given("grc")) std::cout << "  -grc is given\n";
    1.70 +                                     
    1.71 +  switch(ap.files().size()) {
    1.72 +  case 0:
    1.73 +    std::cout << "  No file argument was given.\n";
    1.74 +    break;
    1.75 +  case 1:
    1.76 +    std::cout << "  1 file argument was given. It is:\n";
    1.77 +    break;
    1.78 +  default:
    1.79 +    std::cout << "  "
    1.80 +	      << ap.files().size() << " file arguments were given. They are:\n";
    1.81 +  }
    1.82 +  for(unsigned int i=0;i<ap.files().size();++i)
    1.83 +    std::cout << "    '" << ap.files()[i] << "'\n";
    1.84 +  
    1.85 +}