1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
 
     3  * This file is a part of LEMON, a generic C++ optimization library.
 
     5  * Copyright (C) 2003-2009
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     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.
 
    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
 
    21 ///\brief Argument parser demo
 
    23 /// This example shows how the argument parser can be used.
 
    25 /// \include arg_parser_demo.cc
 
    27 #include <lemon/arg_parser.h>
 
    29 using namespace lemon;
 
    30 int main(int argc, char **argv)
 
    32   // Initialize the argument parser
 
    33   ArgParser ap(argc, argv);
 
    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);
 
    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.")
 
    68   // Perform the parsing process
 
    69   // (in case of any error it terminates the program)
 
    72   // Check each option if it has been given and print its value
 
    73   std::cout << "Parameters of '" << ap.commandName() << "':\n";
 
    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")) {
 
    79     std::cout << "  Value of -val2: " << d << std::endl;
 
    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";
 
    88   switch(ap.files().size()) {
 
    90     std::cout << "  No file argument was given.\n";
 
    93     std::cout << "  1 file argument was given. It is:\n";
 
    97               << ap.files().size() << " file arguments were given. They are:\n";
 
    99   for(unsigned int i=0;i<ap.files().size();++i)
 
   100     std::cout << "    '" << ap.files()[i] << "'\n";