demo/arg_parser_demo.cc
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 15 May 2015 10:16:48 +0200
changeset 1145 1de908281369
parent 842 c2ff0a365245
permissions -rw-r--r--
Update Doxyfile.in

- Remove obsolete (as of Doxygen version 1.8.9) config parameters
- Switch SHORT_NAMES off
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2010
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 ///\ingroup demos
    20 ///\file
    21 ///\brief Argument parser demo
    22 ///
    23 /// This example shows how the argument parser can be used.
    24 ///
    25 /// \include arg_parser_demo.cc
    26 
    27 #include <lemon/arg_parser.h>
    28 
    29 using namespace lemon;
    30 int main(int argc, char **argv)
    31 {
    32   // Initialize the argument parser
    33   ArgParser ap(argc, argv);
    34   int i;
    35   std::string s;
    36   double d = 1.0;
    37   bool b, nh;
    38   bool g1, g2, g3;
    39 
    40   // Add a mandatory integer option with storage reference
    41   ap.refOption("n", "An integer input.", i, true);
    42   // Add a double option with storage reference (the default value is 1.0)
    43   ap.refOption("val", "A double input.", d);
    44   // Add a double option without storage reference (the default value is 3.14)
    45   ap.doubleOption("val2", "A double input.", 3.14);
    46   // Set synonym for -val option
    47   ap.synonym("vals", "val");
    48   // Add a string option
    49   ap.refOption("name", "A string input.", s);
    50   // Add bool options
    51   ap.refOption("f", "A switch.", b)
    52     .refOption("nohelp", "", nh)
    53     .refOption("gra", "Choice A", g1)
    54     .refOption("grb", "Choice B", g2)
    55     .refOption("grc", "Choice C", g3);
    56   // Bundle -gr* options into a group
    57   ap.optionGroup("gr", "gra")
    58     .optionGroup("gr", "grb")
    59     .optionGroup("gr", "grc");
    60   // Set the group mandatory
    61   ap.mandatoryGroup("gr");
    62   // Set the options of the group exclusive (only one option can be given)
    63   ap.onlyOneGroup("gr");
    64   // Add non-parsed arguments (e.g. input files)
    65   ap.other("infile", "The input file.")
    66     .other("...");
    67 
    68   // Throw an exception when problems occurs. The default behavior is to
    69   // exit(1) on these cases, but this makes Valgrind falsely warn
    70   // about memory leaks.
    71   ap.throwOnProblems();
    72 
    73   // Perform the parsing process
    74   // (in case of any error it terminates the program)
    75   // The try {} construct is necessary only if the ap.trowOnProblems()
    76   // setting is in use.
    77   try {
    78     ap.parse();
    79   } catch (ArgParserException &) { return 1; }
    80 
    81   // Check each option if it has been given and print its value
    82   std::cout << "Parameters of '" << ap.commandName() << "':\n";
    83 
    84   std::cout << "  Value of -n: " << i << std::endl;
    85   if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
    86   if(ap.given("val2")) {
    87     d = ap["val2"];
    88     std::cout << "  Value of -val2: " << d << std::endl;
    89   }
    90   if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
    91   if(ap.given("f")) std::cout << "  -f is given\n";
    92   if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << nh << std::endl;
    93   if(ap.given("gra")) std::cout << "  -gra is given\n";
    94   if(ap.given("grb")) std::cout << "  -grb is given\n";
    95   if(ap.given("grc")) std::cout << "  -grc is given\n";
    96 
    97   switch(ap.files().size()) {
    98   case 0:
    99     std::cout << "  No file argument was given.\n";
   100     break;
   101   case 1:
   102     std::cout << "  1 file argument was given. It is:\n";
   103     break;
   104   default:
   105     std::cout << "  "
   106               << ap.files().size() << " file arguments were given. They are:\n";
   107   }
   108   for(unsigned int i=0;i<ap.files().size();++i)
   109     std::cout << "    '" << ap.files()[i] << "'\n";
   110 
   111   return 0;
   112 }