1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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 // 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.
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()
79 } catch (ArgParserException &) { return 1; }
81 // Check each option if it has been given and print its value
82 std::cout << "Parameters of '" << ap.commandName() << "':\n";
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")) {
88 std::cout << " Value of -val2: " << d << std::endl;
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";
97 switch(ap.files().size()) {
99 std::cout << " No file argument was given.\n";
102 std::cout << " 1 file argument was given. It is:\n";
106 << ap.files().size() << " file arguments were given. They are:\n";
108 for(unsigned int i=0;i<ap.files().size();++i)
109 std::cout << " '" << ap.files()[i] << "'\n";