alpar@85: /* -*- C++ -*- alpar@85: * alpar@85: * This file is a part of LEMON, a generic C++ optimization library alpar@85: * alpar@85: * Copyright (C) 2003-2008 alpar@85: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@85: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@85: * alpar@85: * Permission to use, modify and distribute this software is granted alpar@85: * provided that this copyright notice appears in all copies. For alpar@85: * precise terms see the accompanying LICENSE file. alpar@85: * alpar@85: * This software is provided "AS IS" with no warranty of any kind, alpar@85: * express or implied, and with no claim as to its suitability for any alpar@85: * purpose. alpar@85: * alpar@85: */ alpar@85: alpar@85: ///\ingroup demos alpar@85: ///\file alpar@85: ///\brief Argument parser demo alpar@85: /// kpeter@88: /// This example shows how the argument parser can be used. alpar@85: /// kpeter@88: /// \include arg_parser_demo.cc alpar@85: alpar@85: #include alpar@85: alpar@85: using namespace lemon; alpar@85: int main(int argc, const char **argv) alpar@85: { kpeter@204: // Initialize the argument parser kpeter@204: ArgParser ap(argc, argv); alpar@85: int i; alpar@85: std::string s; kpeter@204: double d = 1.0; kpeter@204: bool b, nh; kpeter@204: bool g1, g2, g3; kpeter@204: kpeter@204: // Add a mandatory integer option with storage reference kpeter@204: ap.refOption("n", "An integer input.", i, true); kpeter@204: // Add a double option with storage reference (the default value is 1.0) kpeter@204: ap.refOption("val", "A double input.", d); kpeter@204: // Add a double option without storage reference (the default value is 3.14) kpeter@204: ap.doubleOption("val2", "A double input.", 3.14); kpeter@204: // Set synonym for -val option kpeter@204: ap.synonym("vals", "val"); kpeter@204: // Add a string option kpeter@204: ap.refOption("name", "A string input.", s); kpeter@204: // Add bool options kpeter@204: ap.refOption("f", "A switch.", b) kpeter@204: .refOption("nohelp", "", nh) kpeter@204: .refOption("gra", "Choice A", g1) kpeter@204: .refOption("grb", "Choice B", g2) kpeter@204: .refOption("grc", "Choice C", g3); kpeter@204: // Bundle -gr* options into a group kpeter@204: ap.optionGroup("gr", "gra") kpeter@204: .optionGroup("gr", "grb") kpeter@204: .optionGroup("gr", "grc"); kpeter@204: // Set the group mandatory kpeter@204: ap.mandatoryGroup("gr"); kpeter@204: // Set the options of the group exclusive (only one option can be given) kpeter@204: ap.onlyOneGroup("gr"); kpeter@204: // Add non-parsed arguments (e.g. input files) kpeter@204: ap.other("infile", "The input file.") alpar@85: .other("..."); alpar@85: kpeter@204: // Perform the parsing process kpeter@204: // (in case of any error it terminates the program) alpar@85: ap.parse(); alpar@85: kpeter@204: // Check each option if it has been given and print its value alpar@85: std::cout << "Parameters of '" << ap.commandName() << "':\n"; alpar@85: kpeter@204: std::cout << " Value of -n: " << i << std::endl; alpar@85: if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl; kpeter@204: if(ap.given("val2")) { kpeter@204: d = ap["val2"]; kpeter@204: std::cout << " Value of -val2: " << d << std::endl; kpeter@204: } alpar@85: if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl; alpar@85: if(ap.given("f")) std::cout << " -f is given\n"; kpeter@204: if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl; kpeter@88: if(ap.given("gra")) std::cout << " -gra is given\n"; kpeter@88: if(ap.given("grb")) std::cout << " -grb is given\n"; kpeter@88: if(ap.given("grc")) std::cout << " -grc is given\n"; kpeter@204: alpar@85: switch(ap.files().size()) { alpar@85: case 0: alpar@85: std::cout << " No file argument was given.\n"; alpar@85: break; alpar@85: case 1: alpar@85: std::cout << " 1 file argument was given. It is:\n"; alpar@85: break; alpar@85: default: alpar@85: std::cout << " " alpar@85: << ap.files().size() << " file arguments were given. They are:\n"; alpar@85: } alpar@85: for(unsigned int i=0;i