demo/arg_parser_demo.cc
author Balazs Dezso <deba@inf.elte.hu>
Tue, 27 May 2008 16:01:20 +0200
changeset 162 33247f6fff16
parent 128 7cd965d2257f
child 204 77d56a21c3ab
permissions -rw-r--r--
Section reader for DigraphReader
alpar@85
     1
/* -*- C++ -*-
alpar@85
     2
 *
alpar@85
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@85
     4
 *
alpar@85
     5
 * Copyright (C) 2003-2008
alpar@85
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@85
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@85
     8
 *
alpar@85
     9
 * Permission to use, modify and distribute this software is granted
alpar@85
    10
 * provided that this copyright notice appears in all copies. For
alpar@85
    11
 * precise terms see the accompanying LICENSE file.
alpar@85
    12
 *
alpar@85
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@85
    14
 * express or implied, and with no claim as to its suitability for any
alpar@85
    15
 * purpose.
alpar@85
    16
 *
alpar@85
    17
 */
alpar@85
    18
alpar@85
    19
///\ingroup demos
alpar@85
    20
///\file
alpar@85
    21
///\brief Argument parser demo
alpar@85
    22
///
kpeter@88
    23
/// This example shows how the argument parser can be used.
alpar@85
    24
///
kpeter@88
    25
/// \include arg_parser_demo.cc
alpar@85
    26
alpar@85
    27
#include <lemon/arg_parser.h>
alpar@85
    28
alpar@85
    29
using namespace lemon;
alpar@85
    30
int main(int argc, const char **argv)
alpar@85
    31
{
alpar@85
    32
  ArgParser ap(argc,argv);
alpar@85
    33
  int i;
alpar@85
    34
  std::string s;
alpar@85
    35
  double d;
alpar@85
    36
  bool b,sil;
alpar@85
    37
  bool g1,g2,g3;
kpeter@88
    38
  ap.refOption("n", "An integer input.", i, true)
kpeter@88
    39
    .refOption("val", "A double input.", d)
alpar@128
    40
    .doubleOption("val2", "A double input.", d)
alpar@85
    41
    .synonym("vals","val")
kpeter@88
    42
    .refOption("name", "A string input.", s)
kpeter@88
    43
    .refOption("f", "A switch.", b)
alpar@85
    44
    .refOption("nohelp", "", sil)
kpeter@88
    45
    .refOption("gra","Choice A",g1)
kpeter@88
    46
    .refOption("grb","Choice B",g2)
kpeter@88
    47
    .refOption("grc","Choice C",g3)
alpar@85
    48
    .optionGroup("gr","gra")
alpar@137
    49
    .optionGroup("gr","grb")
alpar@85
    50
    .optionGroup("gr","grc")
alpar@85
    51
    .mandatoryGroup("gr")
alpar@85
    52
    .onlyOneGroup("gr")
kpeter@88
    53
    .other("infile","The input file.")
alpar@85
    54
    .other("...");
alpar@85
    55
  
alpar@85
    56
  ap.parse();
alpar@85
    57
alpar@85
    58
  std::cout << "Parameters of '" << ap.commandName() << "':\n";
alpar@85
    59
alpar@85
    60
  if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
alpar@85
    61
  if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
alpar@85
    62
  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
alpar@85
    63
  if(ap.given("f")) std::cout << "  -f is given\n";
alpar@85
    64
  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
kpeter@88
    65
  if(ap.given("gra")) std::cout << "  -gra is given\n";
kpeter@88
    66
  if(ap.given("grb")) std::cout << "  -grb is given\n";
kpeter@88
    67
  if(ap.given("grc")) std::cout << "  -grc is given\n";
kpeter@88
    68
                                     
alpar@85
    69
  switch(ap.files().size()) {
alpar@85
    70
  case 0:
alpar@85
    71
    std::cout << "  No file argument was given.\n";
alpar@85
    72
    break;
alpar@85
    73
  case 1:
alpar@85
    74
    std::cout << "  1 file argument was given. It is:\n";
alpar@85
    75
    break;
alpar@85
    76
  default:
alpar@85
    77
    std::cout << "  "
alpar@85
    78
	      << ap.files().size() << " file arguments were given. They are:\n";
alpar@85
    79
  }
alpar@85
    80
  for(unsigned int i=0;i<ap.files().size();++i)
alpar@85
    81
    std::cout << "    '" << ap.files()[i] << "'\n";
alpar@85
    82
  
alpar@85
    83
}