/Src/Dependencies/Boost/boost/program_options/options_description.hpp

http://hadesmem.googlecode.com/ · C++ Header · 264 lines · 109 code · 51 blank · 104 comment · 0 complexity · 619354a1d601b28da55b701c9ea8103d MD5 · raw file

  1. // Copyright Vladimir Prus 2002-2004.
  2. // Copyright Bertolt Mildner 2004.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_OPTION_DESCRIPTION_VP_2003_05_19
  7. #define BOOST_OPTION_DESCRIPTION_VP_2003_05_19
  8. #include <boost/program_options/config.hpp>
  9. #include <boost/program_options/errors.hpp>
  10. #include <boost/program_options/value_semantic.hpp>
  11. #include <boost/function.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <boost/detail/workaround.hpp>
  14. #include <boost/any.hpp>
  15. #include <string>
  16. #include <vector>
  17. #include <set>
  18. #include <map>
  19. #include <stdexcept>
  20. #include <iosfwd>
  21. #if defined(BOOST_MSVC)
  22. # pragma warning (push)
  23. # pragma warning (disable:4251) // class 'boost::shared_ptr<T>' needs to have dll-interface to be used by clients of class 'boost::program_options::option_description'
  24. #endif
  25. /** Boost namespace */
  26. namespace boost {
  27. /** Namespace for the library. */
  28. namespace program_options {
  29. /** Describes one possible command line/config file option. There are two
  30. kinds of properties of an option. First describe it syntactically and
  31. are used only to validate input. Second affect interpretation of the
  32. option, for example default value for it or function that should be
  33. called when the value is finally known. Routines which perform parsing
  34. never use second kind of properties -- they are side effect free.
  35. @sa options_description
  36. */
  37. class BOOST_PROGRAM_OPTIONS_DECL option_description {
  38. public:
  39. option_description();
  40. /** Initializes the object with the passed data.
  41. Note: it would be nice to make the second parameter auto_ptr,
  42. to explicitly pass ownership. Unfortunately, it's often needed to
  43. create objects of types derived from 'value_semantic':
  44. options_description d;
  45. d.add_options()("a", parameter<int>("n")->default_value(1));
  46. Here, the static type returned by 'parameter' should be derived
  47. from value_semantic.
  48. Alas, derived->base conversion for auto_ptr does not really work,
  49. see
  50. http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2000/n1232.pdf
  51. http://std.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#84
  52. So, we have to use plain old pointers. Besides, users are not
  53. expected to use the constructor directly.
  54. The 'name' parameter is interpreted by the following rules:
  55. - if there's no "," character in 'name', it specifies long name
  56. - otherwise, the part before "," specifies long name and the part
  57. after -- short name.
  58. */
  59. option_description(const char* name,
  60. const value_semantic* s);
  61. /** Initializes the class with the passed data.
  62. */
  63. option_description(const char* name,
  64. const value_semantic* s,
  65. const char* description);
  66. virtual ~option_description();
  67. enum match_result { no_match, full_match, approximate_match };
  68. /** Given 'option', specified in the input source,
  69. returns 'true' if 'option' specifies *this.
  70. */
  71. match_result match(const std::string& option, bool approx,
  72. bool long_ignore_case, bool short_ignore_case) const;
  73. /** Returns the key that should identify the option, in
  74. particular in the variables_map class.
  75. The 'option' parameter is the option spelling from the
  76. input source.
  77. If option name contains '*', returns 'option'.
  78. If long name was specified, it's the long name, otherwise
  79. it's a short name with prepended '-'.
  80. */
  81. const std::string& key(const std::string& option) const;
  82. const std::string& long_name() const;
  83. /// Explanation of this option
  84. const std::string& description() const;
  85. /// Semantic of option's value
  86. shared_ptr<const value_semantic> semantic() const;
  87. /// Returns the option name, formatted suitably for usage message.
  88. std::string format_name() const;
  89. /** Returns the parameter name and properties, formatted suitably for
  90. usage message. */
  91. std::string format_parameter() const;
  92. private:
  93. option_description& set_name(const char* name);
  94. std::string m_short_name, m_long_name, m_description;
  95. // shared_ptr is needed to simplify memory management in
  96. // copy ctor and destructor.
  97. shared_ptr<const value_semantic> m_value_semantic;
  98. };
  99. class options_description;
  100. /** Class which provides convenient creation syntax to option_description.
  101. */
  102. class BOOST_PROGRAM_OPTIONS_DECL options_description_easy_init {
  103. public:
  104. options_description_easy_init(options_description* owner);
  105. options_description_easy_init&
  106. operator()(const char* name,
  107. const char* description);
  108. options_description_easy_init&
  109. operator()(const char* name,
  110. const value_semantic* s);
  111. options_description_easy_init&
  112. operator()(const char* name,
  113. const value_semantic* s,
  114. const char* description);
  115. private:
  116. options_description* owner;
  117. };
  118. /** A set of option descriptions. This provides convenient interface for
  119. adding new option (the add_options) method, and facilities to search
  120. for options by name.
  121. See @ref a_adding_options "here" for option adding interface discussion.
  122. @sa option_description
  123. */
  124. class BOOST_PROGRAM_OPTIONS_DECL options_description {
  125. public:
  126. static const unsigned m_default_line_length;
  127. /** Creates the instance. */
  128. options_description(unsigned line_length = m_default_line_length,
  129. unsigned min_description_length = m_default_line_length / 2);
  130. /** Creates the instance. The 'caption' parameter gives the name of
  131. this 'options_description' instance. Primarily useful for output.
  132. The 'description_length' specifies the number of columns that
  133. should be reserved for the description text; if the option text
  134. encroaches into this, then the description will start on the next
  135. line.
  136. */
  137. options_description(const std::string& caption,
  138. unsigned line_length = m_default_line_length,
  139. unsigned min_description_length = m_default_line_length / 2);
  140. /** Adds new variable description. Throws duplicate_variable_error if
  141. either short or long name matches that of already present one.
  142. */
  143. void add(shared_ptr<option_description> desc);
  144. /** Adds a group of option description. This has the same
  145. effect as adding all option_descriptions in 'desc'
  146. individually, except that output operator will show
  147. a separate group.
  148. Returns *this.
  149. */
  150. options_description& add(const options_description& desc);
  151. public:
  152. /** Returns an object of implementation-defined type suitable for adding
  153. options to options_description. The returned object will
  154. have overloaded operator() with parameter type matching
  155. 'option_description' constructors. Calling the operator will create
  156. new option_description instance and add it.
  157. */
  158. options_description_easy_init add_options();
  159. const option_description& find(const std::string& name,
  160. bool approx,
  161. bool long_ignore_case = false,
  162. bool short_ignore_case = false) const;
  163. const option_description* find_nothrow(const std::string& name,
  164. bool approx,
  165. bool long_ignore_case = false,
  166. bool short_ignore_case = false) const;
  167. const std::vector< shared_ptr<option_description> >& options() const;
  168. /** Produces a human readable output of 'desc', listing options,
  169. their descriptions and allowed parameters. Other options_description
  170. instances previously passed to add will be output separately. */
  171. friend BOOST_PROGRAM_OPTIONS_DECL std::ostream& operator<<(std::ostream& os,
  172. const options_description& desc);
  173. /** Outputs 'desc' to the specified stream, calling 'f' to output each
  174. option_description element. */
  175. void print(std::ostream& os) const;
  176. private:
  177. typedef std::map<std::string, int>::const_iterator name2index_iterator;
  178. typedef std::pair<name2index_iterator, name2index_iterator>
  179. approximation_range;
  180. //approximation_range find_approximation(const std::string& prefix) const;
  181. std::string m_caption;
  182. const unsigned m_line_length;
  183. const unsigned m_min_description_length;
  184. // Data organization is chosen because:
  185. // - there could be two names for one option
  186. // - option_add_proxy needs to know the last added option
  187. std::vector< shared_ptr<option_description> > m_options;
  188. // Whether the option comes from one of declared groups.
  189. #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, BOOST_TESTED_AT(313))
  190. // vector<bool> is buggy there, see
  191. // http://support.microsoft.com/default.aspx?scid=kb;en-us;837698
  192. std::vector<char> belong_to_group;
  193. #else
  194. std::vector<bool> belong_to_group;
  195. #endif
  196. std::vector< shared_ptr<options_description> > groups;
  197. };
  198. /** Class thrown when duplicate option description is found. */
  199. class BOOST_PROGRAM_OPTIONS_DECL duplicate_option_error : public error {
  200. public:
  201. duplicate_option_error(const std::string& xwhat) : error(xwhat) {}
  202. };
  203. }}
  204. #if defined(BOOST_MSVC)
  205. # pragma warning (pop)
  206. #endif
  207. #endif