demo/arg_parser_demo.cc
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 30 Jul 2008 12:07:48 +0100
changeset 246 7c67988fca07
parent 204 77d56a21c3ab
child 311 c887e703b566
permissions -rw-r--r--
Update README file.
     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-2008
     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, const 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   // Perform the parsing process
    69   // (in case of any error it terminates the program)
    70   ap.parse();
    71 
    72   // Check each option if it has been given and print its value
    73   std::cout << "Parameters of '" << ap.commandName() << "':\n";
    74 
    75   std::cout << "  Value of -n: " << i << std::endl;
    76   if(ap.given("val")) std::cout << "  Value of -val: " << d << std::endl;
    77   if(ap.given("val2")) {
    78     d = ap["val2"];
    79     std::cout << "  Value of -val2: " << d << std::endl;
    80   }
    81   if(ap.given("name")) std::cout << "  Value of -name: " << s << std::endl;
    82   if(ap.given("f")) std::cout << "  -f is given\n";
    83   if(ap.given("nohelp")) std::cout << "  Value of -nohelp: " << nh << std::endl;
    84   if(ap.given("gra")) std::cout << "  -gra is given\n";
    85   if(ap.given("grb")) std::cout << "  -grb is given\n";
    86   if(ap.given("grc")) std::cout << "  -grc is given\n";
    87 
    88   switch(ap.files().size()) {
    89   case 0:
    90     std::cout << "  No file argument was given.\n";
    91     break;
    92   case 1:
    93     std::cout << "  1 file argument was given. It is:\n";
    94     break;
    95   default:
    96     std::cout << "  "
    97               << ap.files().size() << " file arguments were given. They are:\n";
    98   }
    99   for(unsigned int i=0;i<ap.files().size();++i)
   100     std::cout << "    '" << ap.files()[i] << "'\n";
   101 
   102   return 0;
   103 }