/indra/newview/llcommandlineparser.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 161 lines · 52 code · 20 blank · 89 comment · 0 complexity · 40776b9fd6ce9ba4a782ef7fc7e3ffb8 MD5 · raw file

  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. #ifndef LL_LLCOMMANDLINEPARSER_H
  27. #define LL_LLCOMMANDLINEPARSER_H
  28. #include <boost/function/function1.hpp>
  29. // *NOTE:Mani The following is a forward decl of
  30. // boost::program_options::command_line_parser
  31. // "yay" c++!
  32. namespace boost
  33. {
  34. namespace program_options
  35. {
  36. template <class charT> class basic_command_line_parser;
  37. typedef basic_command_line_parser<char> command_line_parser;
  38. }
  39. }
  40. /**
  41. * @class LLCommandLineParser
  42. * @brief Handle defining and parsing the command line.
  43. */
  44. class LLCommandLineParser
  45. {
  46. public:
  47. typedef std::vector< std::string > token_vector_t;
  48. /**
  49. * @brief Add a value-less option to the command line description.
  50. * @param option_name The long name of the cmd-line option.
  51. * @param description The text description of the option usage.
  52. */
  53. void addOptionDesc(
  54. const std::string& option_name,
  55. boost::function1<void, const token_vector_t&> notify_callback = 0,
  56. unsigned int num_tokens = 0,
  57. const std::string& description = LLStringUtil::null,
  58. const std::string& short_name = LLStringUtil::null,
  59. bool composing = false,
  60. bool positional = false,
  61. bool last_option = false);
  62. /**
  63. * @brief Parse the command line given by argc/argv.
  64. */
  65. bool parseCommandLine(int argc, char **argv);
  66. /**
  67. * @brief Parse the command line contained by the given file.
  68. */
  69. bool parseCommandLineString(const std::string& str);
  70. /**
  71. * @brief Parse the command line contained by the given file.
  72. */
  73. bool parseCommandLineFile(const std::basic_istream< char >& file);
  74. /**
  75. * @brief Call callbacks associated with option descriptions.
  76. *
  77. * Use this to handle the results of parsing.
  78. */
  79. void notify();
  80. /** @brief Print a description of the configured options.
  81. *
  82. * Use this to print a description of options to the
  83. * given ostream. Useful for displaying usage info.
  84. */
  85. std::ostream& printOptionsDesc(std::ostream& os) const;
  86. /** @brief Manual option setting accessors.
  87. *
  88. * Use these to retrieve get the values set for an option.
  89. * getOption will return an empty value if the option isn't
  90. * set.
  91. */
  92. bool hasOption(const std::string& name) const;
  93. const token_vector_t& getOption(const std::string& name) const;
  94. /** @brief Print the list of configured options.
  95. */
  96. void printOptions() const;
  97. /** @bried Get the error message, if it exists.
  98. */
  99. const std::string& getErrorMessage() const { return mErrorMsg; }
  100. /** @brief Add a custom parser func to the parser.
  101. *
  102. * Use this method to add a custom parser for parsing values
  103. * the the simple parser may not handle.
  104. * it will be applied to each parameter before the
  105. * default parser gets a chance.
  106. * The parser_func takes an input string, and should return a
  107. * name/value pair as the result.
  108. */
  109. typedef boost::function1<std::pair<std::string, std::string>, const std::string&> parser_func;
  110. void setCustomParser(parser_func f) { mExtraParser = f; }
  111. private:
  112. bool parseAndStoreResults(boost::program_options::command_line_parser& clp);
  113. std::string mErrorMsg;
  114. parser_func mExtraParser;
  115. };
  116. inline std::ostream& operator<<(std::ostream& out, const LLCommandLineParser& clp)
  117. {
  118. return clp.printOptionsDesc(out);
  119. }
  120. class LLControlGroup;
  121. /**
  122. * @class LLControlGroupCLP
  123. * @brief Uses the CLP to configure an LLControlGroup
  124. *
  125. *
  126. */
  127. class LLControlGroupCLP : public LLCommandLineParser
  128. {
  129. public:
  130. /**
  131. * @brief Configure the command line parser according the given config file.
  132. *
  133. * @param config_filename The name of the XML based LLSD config file.
  134. * @param clp A reference to the command line parser object to configure.
  135. *
  136. * *FIX:Mani Specify config file format.
  137. */
  138. void configure(const std::string& config_filename,
  139. LLControlGroup* controlGroup);
  140. };
  141. #endif // LL_LLCOMMANDLINEPARSER_H