1 // g2o - General Graph Optimization 2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard 3 // All rights reserved. 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright notice, 10 // this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above copyright 12 // notice, this list of conditions and the following disclaimer in the 13 // documentation and/or other materials provided with the distribution. 14 // 15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 #ifndef G2O_COMMAND_ARGS_H 28 #define G2O_COMMAND_ARGS_H 29 30 #include <string> 31 #include <vector> 32 #include <iostream> 33 34 #include "g2o_stuff_api.h" 35 36 namespace g2o { 37 38 /** 39 * \brief Command line parsing of argc and argv. 40 * 41 * Parse the command line to get the program options. Additionally, 42 * we can store the config in a file and reload a parameter set from 43 * this file. 44 */ 45 class G2O_STUFF_API CommandArgs 46 { 47 public: 48 struct CommandArgument 49 { 50 std::string name; 51 std::string description; 52 int type; 53 void* data; 54 bool parsed; 55 bool optional; CommandArgumentCommandArgument56 CommandArgument() : name(""), description(""), type(0), data(0), parsed(false), optional(false) 57 {} 58 }; 59 public: 60 virtual ~CommandArgs(); 61 62 /** 63 * parse the command line for the requested parameters. 64 * @param argc the number of params 65 * @param argv the value array 66 * @param exitOnError call exit() if the parsing fails 67 * @return true, if parsing was correct 68 */ 69 bool parseArgs(int argc, char** argv, bool exitOnError = true); 70 71 /** add a bool parameter, if found on the command line, will toggle defValue */ 72 void param(const std::string& name, bool& p, bool defValue, const std::string& desc); 73 /** add a int parameter */ 74 void param(const std::string& name, int& p, int defValue, const std::string& desc); 75 /** add a float parameter */ 76 void param(const std::string& name, float& p, float defValue, const std::string& desc); 77 /** add a float parameter */ 78 void param(const std::string& name, double& p, double defValue, const std::string& desc); 79 /** add a string parameter */ 80 void param(const std::string& name, std::string& p, const std::string& defValue, const std::string& desc); 81 /** add an int vector parameter */ 82 void param(const std::string& name, std::vector<int>& p, const std::vector<int>& defValue, const std::string& desc); 83 /** add an vector of doubles as a parameter */ 84 void param(const std::string& name, std::vector<double>& p, const std::vector<double>& defValue, const std::string& desc); 85 /** add a param wich is specified as a plain argument */ 86 void paramLeftOver(const std::string& name, std::string& p, const std::string& defValue, const std::string& desc, bool optional = false); 87 88 /** 89 * print the value of all params to an ostream 90 */ 91 void printParams(std::ostream& os); 92 93 //! return the banner string getBanner()94 const std::string& getBanner() const { return _banner; } 95 void setBanner(const std::string& banner); 96 97 /** 98 * print the help 99 */ 100 void printHelp(std::ostream& os); 101 102 /** 103 * returns true, if the param was parsed via the command line 104 */ 105 bool parsedParam(const std::string& paramFlag) const; 106 107 protected: 108 std::vector<CommandArgument> _args; 109 std::vector<CommandArgument> _leftOvers; 110 std::vector<CommandArgument> _leftOversOptional; 111 std::string _banner; 112 std::string _progName; 113 114 const char* type2str(int t) const; 115 void str2arg(const std::string& input, CommandArgument& ca) const; 116 std::string arg2str(const CommandArgument& ca) const; 117 }; 118 119 } // end namespace 120 121 #endif 122