demo/arg_parser_demo.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 22 Jan 2008 16:03:41 +0000
changeset 86 eba5222bb6f5
child 88 18444049848b
permissions -rw-r--r--
Minor doc improvements
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
///
alpar@85
    23
/// This example shows how can the argument parser used.
alpar@85
    24
///
alpar@85
    25
/// \include arg_parser.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;
alpar@85
    38
  ap.refOption("n", "an integer input", i, true)
alpar@85
    39
    .refOption("val", "a double input", d)
alpar@85
    40
    .synonym("vals","val")
alpar@85
    41
    .refOption("name", "a string input", s)
alpar@85
    42
    .refOption("f", "a switch", b)
alpar@85
    43
    .refOption("nohelp", "", sil)
alpar@85
    44
    .refOption("gra","Choise A",g1)
alpar@85
    45
    .refOption("grb","Choise B",g2)
alpar@85
    46
    .refOption("grc","Choise C",g3)
alpar@85
    47
    .optionGroup("gr","gra")
alpar@85
    48
    .optionGroup("gr","grb")
alpar@85
    49
    .optionGroup("gr","grc")
alpar@85
    50
    .mandatoryGroup("gr")
alpar@85
    51
    .onlyOneGroup("gr")
alpar@85
    52
    .other("infile","The input file")
alpar@85
    53
    .other("...");
alpar@85
    54
  
alpar@85
    55
  ap.parse();
alpar@85
    56
alpar@85
    57
  std::cout << "Parameters of '" << ap.commandName() << "':\n";
alpar@85
    58
alpar@85
    59
  if(ap.given("n")) std::cout << "  Value of -n: " << i << std::endl;
alpar@85
    60
  if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
alpar@85
    61
  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
alpar@85
    62
  if(ap.given("f")) std::cout << "  -f is given\n";
alpar@85
    63
  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << sil << std::endl;
alpar@85
    64
alpar@85
    65
  switch(ap.files().size()) {
alpar@85
    66
  case 0:
alpar@85
    67
    std::cout << "  No file argument was given.\n";
alpar@85
    68
    break;
alpar@85
    69
  case 1:
alpar@85
    70
    std::cout << "  1 file argument was given. It is:\n";
alpar@85
    71
    break;
alpar@85
    72
  default:
alpar@85
    73
    std::cout << "  "
alpar@85
    74
	      << ap.files().size() << " file arguments were given. They are:\n";
alpar@85
    75
  }
alpar@85
    76
  for(unsigned int i=0;i<ap.files().size();++i)
alpar@85
    77
    std::cout << "    '" << ap.files()[i] << "'\n";
alpar@85
    78
  
alpar@85
    79
}