demo/arg_parser_demo.cc
author Alpar Juttner <alpar@cs.elte.hu>
Sat, 29 Mar 2008 15:49:50 +0000
changeset 111 e47fb28cda5e
parent 85 3453d20a82cd
child 128 7cd965d2257f
permissions -rw-r--r--
Fix ignoring Emacs backup files
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@85
    40
    .synonym("vals","val")
kpeter@88
    41
    .refOption("name", "A string input.", s)
kpeter@88
    42
    .refOption("f", "A switch.", b)
alpar@85
    43
    .refOption("nohelp", "", sil)
kpeter@88
    44
    .refOption("gra","Choice A",g1)
kpeter@88
    45
    .refOption("grb","Choice B",g2)
kpeter@88
    46
    .refOption("grc","Choice 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")
kpeter@88
    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;
kpeter@88
    64
  if(ap.given("gra")) std::cout << "  -gra is given\n";
kpeter@88
    65
  if(ap.given("grb")) std::cout << "  -grb is given\n";
kpeter@88
    66
  if(ap.given("grc")) std::cout << "  -grc is given\n";
kpeter@88
    67
                                     
alpar@85
    68
  switch(ap.files().size()) {
alpar@85
    69
  case 0:
alpar@85
    70
    std::cout << "  No file argument was given.\n";
alpar@85
    71
    break;
alpar@85
    72
  case 1:
alpar@85
    73
    std::cout << "  1 file argument was given. It is:\n";
alpar@85
    74
    break;
alpar@85
    75
  default:
alpar@85
    76
    std::cout << "  "
alpar@85
    77
	      << ap.files().size() << " file arguments were given. They are:\n";
alpar@85
    78
  }
alpar@85
    79
  for(unsigned int i=0;i<ap.files().size();++i)
alpar@85
    80
    std::cout << "    '" << ap.files()[i] << "'\n";
alpar@85
    81
  
alpar@85
    82
}