demo/arg_parser_demo.cc
author Alpar Juttner <alpar@cs.elte.hu>
Tue, 06 Aug 2013 12:28:37 +0200
changeset 1079 5958cc5c0a98
parent 842 c2ff0a365245
permissions -rw-r--r--
Merge further fixes #470
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@877
     5
 * Copyright (C) 2003-2010
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
alpar@842
    68
  // Throw an exception when problems occurs. The default behavior is to
alpar@842
    69
  // exit(1) on these cases, but this makes Valgrind falsely warn
alpar@842
    70
  // about memory leaks.
alpar@842
    71
  ap.throwOnProblems();
alpar@877
    72
kpeter@204
    73
  // Perform the parsing process
kpeter@204
    74
  // (in case of any error it terminates the program)
alpar@842
    75
  // The try {} construct is necessary only if the ap.trowOnProblems()
alpar@842
    76
  // setting is in use.
alpar@842
    77
  try {
alpar@842
    78
    ap.parse();
alpar@842
    79
  } catch (ArgParserException &) { return 1; }
alpar@85
    80
kpeter@204
    81
  // Check each option if it has been given and print its value
alpar@85
    82
  std::cout << "Parameters of '" << ap.commandName() << "':\n";
alpar@85
    83
kpeter@204
    84
  std::cout << "  Value of -n: " << i << std::endl;
alpar@85
    85
  if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
kpeter@204
    86
  if(ap.given("val2")) {
kpeter@204
    87
    d = ap["val2"];
kpeter@204
    88
    std::cout << "  Value of -val2: " << d << std::endl;
kpeter@204
    89
  }
alpar@85
    90
  if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
alpar@85
    91
  if(ap.given("f")) std::cout << "  -f is given\n";
kpeter@204
    92
  if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << nh << std::endl;
kpeter@88
    93
  if(ap.given("gra")) std::cout << "  -gra is given\n";
kpeter@88
    94
  if(ap.given("grb")) std::cout << "  -grb is given\n";
kpeter@88
    95
  if(ap.given("grc")) std::cout << "  -grc is given\n";
alpar@209
    96
alpar@85
    97
  switch(ap.files().size()) {
alpar@85
    98
  case 0:
alpar@85
    99
    std::cout << "  No file argument was given.\n";
alpar@85
   100
    break;
alpar@85
   101
  case 1:
alpar@85
   102
    std::cout << "  1 file argument was given. It is:\n";
alpar@85
   103
    break;
alpar@85
   104
  default:
alpar@85
   105
    std::cout << "  "
alpar@209
   106
              << ap.files().size() << " file arguments were given. They are:\n";
alpar@85
   107
  }
alpar@85
   108
  for(unsigned int i=0;i<ap.files().size();++i)
alpar@85
   109
    std::cout << "    '" << ap.files()[i] << "'\n";
alpar@209
   110
kpeter@204
   111
  return 0;
alpar@85
   112
}