llcommandlineparser.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @file llcommandlineparser.h
  3. * @brief LLCommandLineParser class declaration
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewergpl$
  6. *
  7. * Copyright (c) 2007-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLCOMMANDLINEPARSER_H
  33. #define LL_LLCOMMANDLINEPARSER_H
  34. #include "boost/function/function1.hpp"
  35. #include "boost/program_options.hpp"
  36. #include "llstring.h"
  37. // LLCommandLineParser handles defining and parsing the command line.
  38. class LLCommandLineParser
  39. {
  40. protected:
  41. LOG_CLASS(LLCommandLineParser);
  42. public:
  43. typedef std::vector<std::string> token_vector_t;
  44. // Adds a value-less option to the command line description.
  45. // 'option_name' is the long name of the cmd-line option and 'description'
  46. // is the text description of the option usage.
  47. void addOptionDesc(const std::string& option_name,
  48. boost::function1<void, const token_vector_t&> notify_callback = 0,
  49. unsigned int num_tokens = 0,
  50. const std::string& description = LLStringUtil::null,
  51. const std::string& short_name = LLStringUtil::null,
  52. bool composing = false, bool positional = false,
  53. bool last_option = false);
  54. // Parses the command line given by argc/argv.
  55. bool parseCommandLine(int argc, char **argv);
  56. // Parses the command line contained by the given file.
  57. bool parseCommandLineString(const std::string& str);
  58. // Parses the command line contained by the given file.
  59. bool parseCommandLineFile(const std::basic_istream<char>& file);
  60. // Calls the callbacks associated with option descriptions; use this to
  61. // handle the results of parsing.
  62. void notify();
  63. // Prints a description of the configured options to the given ostream.
  64. // Useful for displaying usage info.
  65. std::ostream& printOptionsDesc(std::ostream& os) const;
  66. // Use these to retrieve get the values set for an option.
  67. bool hasOption(const std::string& name) const;
  68. // Returns an empty value if the option is not set.
  69. const token_vector_t& getOption(const std::string& name) const;
  70. // Prints the list of configured options.
  71. void printOptions() const;
  72. // Gets the error message, if it exists.
  73. LL_INLINE const std::string& getErrorMessage() const { return mErrorMsg; }
  74. // parser_func takes an input string, and should return a name/value pair
  75. // as the result.
  76. typedef boost::function1<std::pair<std::string,
  77. std::string>, const std::string&> parser_func;
  78. // Adds a custom parser func to the parser.
  79. // Use this method to add a custom parser for parsing values that the
  80. // simple parser may not handle. It will be applied to each parameter
  81. // before the default parser gets a chance.
  82. LL_INLINE void setCustomParser(parser_func f) { mExtraParser = f; }
  83. private:
  84. bool parseAndStoreResults(boost::program_options::command_line_parser& clp);
  85. private:
  86. std::string mErrorMsg;
  87. parser_func mExtraParser;
  88. };
  89. LL_INLINE std::ostream& operator<<(std::ostream& out, const LLCommandLineParser& clp)
  90. {
  91. return clp.printOptionsDesc(out);
  92. }
  93. #endif // LL_LLCOMMANDLINEPARSER_H