/indra/newview/llcommandlineparser.h
C++ Header | 161 lines | 52 code | 20 blank | 89 comment | 0 complexity | 40776b9fd6ce9ba4a782ef7fc7e3ffb8 MD5 | raw file
Possible License(s): LGPL-2.1
1/** 2 * @file llcommandlineparser.h 3 * @brief LLCommandLineParser class declaration 4 * 5 * $LicenseInfo:firstyear=2007&license=viewerlgpl$ 6 * Second Life Viewer Source Code 7 * Copyright (C) 2010, Linden Research, Inc. 8 * 9 * This library is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; 12 * version 2.1 of the License only. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 * 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA 24 * $/LicenseInfo$ 25 */ 26 27#ifndef LL_LLCOMMANDLINEPARSER_H 28#define LL_LLCOMMANDLINEPARSER_H 29 30#include <boost/function/function1.hpp> 31 32// *NOTE:Mani The following is a forward decl of 33// boost::program_options::command_line_parser 34// "yay" c++! 35namespace boost 36{ 37 namespace program_options 38 { 39 template <class charT> class basic_command_line_parser; 40 typedef basic_command_line_parser<char> command_line_parser; 41 } 42} 43 44 45/** 46 * @class LLCommandLineParser 47 * @brief Handle defining and parsing the command line. 48 */ 49class LLCommandLineParser 50{ 51public: 52 typedef std::vector< std::string > token_vector_t; 53 54 /** 55 * @brief Add a value-less option to the command line description. 56 * @param option_name The long name of the cmd-line option. 57 * @param description The text description of the option usage. 58 */ 59 void addOptionDesc( 60 const std::string& option_name, 61 boost::function1<void, const token_vector_t&> notify_callback = 0, 62 unsigned int num_tokens = 0, 63 const std::string& description = LLStringUtil::null, 64 const std::string& short_name = LLStringUtil::null, 65 bool composing = false, 66 bool positional = false, 67 bool last_option = false); 68 69 /** 70 * @brief Parse the command line given by argc/argv. 71 */ 72 bool parseCommandLine(int argc, char **argv); 73 74 /** 75 * @brief Parse the command line contained by the given file. 76 */ 77 bool parseCommandLineString(const std::string& str); 78 79 /** 80 * @brief Parse the command line contained by the given file. 81 */ 82 bool parseCommandLineFile(const std::basic_istream< char >& file); 83 84 /** 85 * @brief Call callbacks associated with option descriptions. 86 * 87 * Use this to handle the results of parsing. 88 */ 89 void notify(); 90 91 /** @brief Print a description of the configured options. 92 * 93 * Use this to print a description of options to the 94 * given ostream. Useful for displaying usage info. 95 */ 96 std::ostream& printOptionsDesc(std::ostream& os) const; 97 98 /** @brief Manual option setting accessors. 99 * 100 * Use these to retrieve get the values set for an option. 101 * getOption will return an empty value if the option isn't 102 * set. 103 */ 104 bool hasOption(const std::string& name) const; 105 const token_vector_t& getOption(const std::string& name) const; 106 107 /** @brief Print the list of configured options. 108 */ 109 void printOptions() const; 110 111 /** @bried Get the error message, if it exists. 112 */ 113 const std::string& getErrorMessage() const { return mErrorMsg; } 114 115 /** @brief Add a custom parser func to the parser. 116 * 117 * Use this method to add a custom parser for parsing values 118 * the the simple parser may not handle. 119 * it will be applied to each parameter before the 120 * default parser gets a chance. 121 * The parser_func takes an input string, and should return a 122 * name/value pair as the result. 123 */ 124 typedef boost::function1<std::pair<std::string, std::string>, const std::string&> parser_func; 125 void setCustomParser(parser_func f) { mExtraParser = f; } 126 127private: 128 bool parseAndStoreResults(boost::program_options::command_line_parser& clp); 129 std::string mErrorMsg; 130 parser_func mExtraParser; 131}; 132 133inline std::ostream& operator<<(std::ostream& out, const LLCommandLineParser& clp) 134{ 135 return clp.printOptionsDesc(out); 136} 137 138class LLControlGroup; 139 140/** 141 * @class LLControlGroupCLP 142 * @brief Uses the CLP to configure an LLControlGroup 143 * 144 * 145 */ 146class LLControlGroupCLP : public LLCommandLineParser 147{ 148public: 149 /** 150 * @brief Configure the command line parser according the given config file. 151 * 152 * @param config_filename The name of the XML based LLSD config file. 153 * @param clp A reference to the command line parser object to configure. 154 * 155 * *FIX:Mani Specify config file format. 156 */ 157 void configure(const std::string& config_filename, 158 LLControlGroup* controlGroup); 159}; 160 161#endif // LL_LLCOMMANDLINEPARSER_H