demo/arg_parser_demo.cc
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Thu, 11 Feb 2010 16:55:54 +0000
changeset 824 974c48bda29e
parent 311 c887e703b566
child 842 c2ff0a365245
permissions -rw-r--r--
Add more information on Makefile variables (#316)
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@85
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@85
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
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;
ladanyi@311
    30
int main(int argc, char **argv)
alpar@85
    31
{
kpeter@204
    32
  // Initialize the argument parser
kpeter@204
    33
  ArgParser ap(argc, argv);
alpar@85
    34
  int i;
alpar@85
    35
  std::string s;
kpeter@204
    36
  double d = 1.0;
kpeter@204
    37
  bool b, nh;
kpeter@204
    38
  bool g1, g2, g3;
kpeter@204
    39
kpeter@204
    40
  // Add a mandatory integer option with storage reference
kpeter@204
    41
  ap.refOption("n", "An integer input.", i, true);
kpeter@204
    42
  // Add a double option with storage reference (the default value is 1.0)
kpeter@204
    43
  ap.refOption("val", "A double input.", d);
kpeter@204
    44
  // Add a double option without storage reference (the default value is 3.14)
kpeter@204
    45
  ap.doubleOption("val2", "A double input.", 3.14);
kpeter@204
    46
  // Set synonym for -val option
kpeter@204
    47
  ap.synonym("vals", "val");
kpeter@204
    48
  // Add a string option
kpeter@204
    49
  ap.refOption("name", "A string input.", s);
kpeter@204
    50
  // Add bool options
kpeter@204
    51
  ap.refOption("f", "A switch.", b)
kpeter@204
    52
    .refOption("nohelp", "", nh)
kpeter@204
    53
    .refOption("gra", "Choice A", g1)
kpeter@204
    54
    .refOption("grb", "Choice B", g2)
kpeter@204
    55
    .refOption("grc", "Choice C", g3);
kpeter@204
    56
  // Bundle -gr* options into a group
kpeter@204
    57
  ap.optionGroup("gr", "gra")
kpeter@204
    58
    .optionGroup("gr", "grb")
kpeter@204
    59
    .optionGroup("gr", "grc");
kpeter@204
    60
  // Set the group mandatory
kpeter@204
    61
  ap.mandatoryGroup("gr");
kpeter@204
    62
  // Set the options of the group exclusive (only one option can be given)
kpeter@204
    63
  ap.onlyOneGroup("gr");
kpeter@204
    64
  // Add non-parsed arguments (e.g. input files)
kpeter@204
    65
  ap.other("infile", "The input file.")
alpar@85
    66
    .other("...");
alpar@209
    67
kpeter@204
    68
  // Perform the parsing process
kpeter@204
    69
  // (in case of any error it terminates the program)
alpar@85
    70
  ap.parse();
alpar@85
    71
kpeter@204
    72
  // Check each option if it has been given and print its value
alpar@85
    73
  std::cout << "Parameters of '" << ap.commandName() << "':\n";
alpar@85
    74
kpeter@204
    75
  std::cout << "  Value of -n: " << i << std::endl;
alpar@85
    76
  if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
kpeter@204
    77
  if(ap.given("val2")) {
kpeter@204
    78
    d = ap["val2"];
kpeter@204
    79
    std::cout << "  Value of -val2: " << d << std::endl;
kpeter@204
    80
  }
alpar@85
    81
  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
alpar@85
    82
  if(ap.given("f")) std::cout << "  -f is given\n";
kpeter@204
    83
  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << nh << std::endl;
kpeter@88
    84
  if(ap.given("gra")) std::cout << "  -gra is given\n";
kpeter@88
    85
  if(ap.given("grb")) std::cout << "  -grb is given\n";
kpeter@88
    86
  if(ap.given("grc")) std::cout << "  -grc is given\n";
alpar@209
    87
alpar@85
    88
  switch(ap.files().size()) {
alpar@85
    89
  case 0:
alpar@85
    90
    std::cout << "  No file argument was given.\n";
alpar@85
    91
    break;
alpar@85
    92
  case 1:
alpar@85
    93
    std::cout << "  1 file argument was given. It is:\n";
alpar@85
    94
    break;
alpar@85
    95
  default:
alpar@85
    96
    std::cout << "  "
alpar@209
    97
              << ap.files().size() << " file arguments were given. They are:\n";
alpar@85
    98
  }
alpar@85
    99
  for(unsigned int i=0;i<ap.files().size();++i)
alpar@85
   100
    std::cout << "    '" << ap.files()[i] << "'\n";
alpar@209
   101
kpeter@204
   102
  return 0;
alpar@85
   103
}