[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[85] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[85] | 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 | /// |
---|
[88] | 23 | /// This example shows how the argument parser can be used. |
---|
[85] | 24 | /// |
---|
[88] | 25 | /// \include arg_parser_demo.cc |
---|
[85] | 26 | |
---|
| 27 | #include <lemon/arg_parser.h> |
---|
| 28 | |
---|
| 29 | using namespace lemon; |
---|
| 30 | int main(int argc, const char **argv) |
---|
| 31 | { |
---|
[204] | 32 | // Initialize the argument parser |
---|
| 33 | ArgParser ap(argc, argv); |
---|
[85] | 34 | int i; |
---|
| 35 | std::string s; |
---|
[204] | 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.") |
---|
[85] | 66 | .other("..."); |
---|
[209] | 67 | |
---|
[204] | 68 | // Perform the parsing process |
---|
| 69 | // (in case of any error it terminates the program) |
---|
[85] | 70 | ap.parse(); |
---|
| 71 | |
---|
[204] | 72 | // Check each option if it has been given and print its value |
---|
[85] | 73 | std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
---|
| 74 | |
---|
[204] | 75 | std::cout << " Value of -n: " << i << std::endl; |
---|
[85] | 76 | if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; |
---|
[204] | 77 | if(ap.given("val2")) { |
---|
| 78 | d = ap["val2"]; |
---|
| 79 | std::cout << " Value of -val2: " << d << std::endl; |
---|
| 80 | } |
---|
[85] | 81 | if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; |
---|
| 82 | if(ap.given("f")) std::cout << " -f is given\n"; |
---|
[204] | 83 | if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl; |
---|
[88] | 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"; |
---|
[209] | 87 | |
---|
[85] | 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 << " " |
---|
[209] | 97 | << ap.files().size() << " file arguments were given. They are:\n"; |
---|
[85] | 98 | } |
---|
| 99 | for(unsigned int i=0;i<ap.files().size();++i) |
---|
| 100 | std::cout << " '" << ap.files()[i] << "'\n"; |
---|
[209] | 101 | |
---|
[204] | 102 | return 0; |
---|
[85] | 103 | } |
---|