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)
8 #ifndef BOOST_OPTION_DESCRIPTION_VP_2003_05_19
9 #define BOOST_OPTION_DESCRIPTION_VP_2003_05_19
11 #include <boost/program_options/config.hpp>
12 #include <boost/program_options/errors.hpp>
13 #include <boost/program_options/value_semantic.hpp>
15 #include <boost/function.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/detail/workaround.hpp>
18 #include <boost/any.hpp>
28 #if defined(BOOST_MSVC)
29 # pragma warning (push)
30 # 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'
34 /** Boost namespace */
36 /** Namespace for the library. */
37 namespace program_options {
39 /** Describes one possible command line/config file option. There are two
40 kinds of properties of an option. First describe it syntactically and
41 are used only to validate input. Second affect interpretation of the
42 option, for example default value for it or function that should be
43 called when the value is finally known. Routines which perform parsing
44 never use second kind of properties \-- they are side effect free.
45 @sa options_description
47 class BOOST_PROGRAM_OPTIONS_DECL option_description {
52 /** Initializes the object with the passed data.
54 Note: it would be nice to make the second parameter auto_ptr,
55 to explicitly pass ownership. Unfortunately, it's often needed to
56 create objects of types derived from 'value_semantic':
57 options_description d;
58 d.add_options()("a", parameter<int>("n")->default_value(1));
59 Here, the static type returned by 'parameter' should be derived
62 Alas, derived->base conversion for auto_ptr does not really work,
64 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2000/n1232.pdf
65 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#84
67 So, we have to use plain old pointers. Besides, users are not
68 expected to use the constructor directly.
71 The 'name' parameter is interpreted by the following rules:
72 - if there's no "," character in 'name', it specifies long name
73 - otherwise, the part before "," specifies long name and the part
76 option_description(const char* name,
77 const value_semantic* s);
79 /** Initializes the class with the passed data.
81 option_description(const char* name,
82 const value_semantic* s,
83 const char* description);
85 virtual ~option_description();
87 enum match_result { no_match, full_match, approximate_match };
89 /** Given 'option', specified in the input source,
90 returns 'true' if 'option' specifies *this.
92 match_result match(const std::string& option, bool approx,
93 bool long_ignore_case, bool short_ignore_case) const;
95 /** Returns the key that should identify the option, in
96 particular in the variables_map class.
97 The 'option' parameter is the option spelling from the
99 If option name contains '*', returns 'option'.
100 If long name was specified, it's the long name, otherwise
101 it's a short name with prepended '-'.
103 const std::string& key(const std::string& option) const;
106 /** Returns the canonical name for the option description to enable the user to
107 recognised a matching option.
108 1) For short options ('-', '/'), returns the short name prefixed.
109 2) For long options ('--' / '-') returns the long name prefixed
110 3) All other cases, returns the long name (if present) or the short name,
113 std::string canonical_display_name(int canonical_option_style = 0) const;
115 const std::string& long_name() const;
117 /// Explanation of this option
118 const std::string& description() const;
120 /// Semantic of option's value
121 shared_ptr<const value_semantic> semantic() const;
123 /// Returns the option name, formatted suitably for usage message.
124 std::string format_name() const;
126 /** Returns the parameter name and properties, formatted suitably for
128 std::string format_parameter() const;
132 option_description& set_name(const char* name);
134 std::string m_short_name, m_long_name, m_description;
135 // shared_ptr is needed to simplify memory management in
136 // copy ctor and destructor.
137 shared_ptr<const value_semantic> m_value_semantic;
140 class options_description;
142 /** Class which provides convenient creation syntax to option_description.
144 class BOOST_PROGRAM_OPTIONS_DECL options_description_easy_init {
146 options_description_easy_init(options_description* owner);
148 options_description_easy_init&
149 operator()(const char* name,
150 const char* description);
152 options_description_easy_init&
153 operator()(const char* name,
154 const value_semantic* s);
156 options_description_easy_init&
157 operator()(const char* name,
158 const value_semantic* s,
159 const char* description);
162 options_description* owner;
166 /** A set of option descriptions. This provides convenient interface for
167 adding new option (the add_options) method, and facilities to search
170 See @ref a_adding_options "here" for option adding interface discussion.
171 @sa option_description
173 class BOOST_PROGRAM_OPTIONS_DECL options_description {
175 static const unsigned m_default_line_length;
177 /** Creates the instance. */
178 options_description(unsigned line_length = m_default_line_length,
179 unsigned min_description_length = m_default_line_length / 2);
180 /** Creates the instance. The 'caption' parameter gives the name of
181 this 'options_description' instance. Primarily useful for output.
182 The 'description_length' specifies the number of columns that
183 should be reserved for the description text; if the option text
184 encroaches into this, then the description will start on the next
187 options_description(const std::string& caption,
188 unsigned line_length = m_default_line_length,
189 unsigned min_description_length = m_default_line_length / 2);
190 /** Adds new variable description. Throws duplicate_variable_error if
191 either short or long name matches that of already present one.
193 void add(shared_ptr<option_description> desc);
194 /** Adds a group of option description. This has the same
195 effect as adding all option_descriptions in 'desc'
196 individually, except that output operator will show
200 options_description& add(const options_description& desc);
202 /** Find the maximum width of the option column, including options
204 unsigned get_option_column_width() const;
207 /** Returns an object of implementation-defined type suitable for adding
208 options to options_description. The returned object will
209 have overloaded operator() with parameter type matching
210 'option_description' constructors. Calling the operator will create
211 new option_description instance and add it.
213 options_description_easy_init add_options();
215 const option_description& find(const std::string& name,
217 bool long_ignore_case = false,
218 bool short_ignore_case = false) const;
220 const option_description* find_nothrow(const std::string& name,
222 bool long_ignore_case = false,
223 bool short_ignore_case = false) const;
226 const std::vector< shared_ptr<option_description> >& options() const;
228 /** Produces a human readable output of 'desc', listing options,
229 their descriptions and allowed parameters. Other options_description
230 instances previously passed to add will be output separately. */
231 friend BOOST_PROGRAM_OPTIONS_DECL std::ostream& operator<<(std::ostream& os,
232 const options_description& desc);
234 /** Outputs 'desc' to the specified stream, calling 'f' to output each
235 option_description element. */
236 void print(std::ostream& os, unsigned width = 0) const;
239 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1800))
240 // prevent warning C4512: assignment operator could not be generated
241 options_description& operator=(const options_description&);
244 typedef std::map<std::string, int>::const_iterator name2index_iterator;
245 typedef std::pair<name2index_iterator, name2index_iterator>
248 //approximation_range find_approximation(const std::string& prefix) const;
250 std::string m_caption;
251 const unsigned m_line_length;
252 const unsigned m_min_description_length;
254 // Data organization is chosen because:
255 // - there could be two names for one option
256 // - option_add_proxy needs to know the last added option
257 std::vector< shared_ptr<option_description> > m_options;
259 // Whether the option comes from one of declared groups.
260 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, BOOST_TESTED_AT(313))
261 // vector<bool> is buggy there, see
262 // http://support.microsoft.com/default.aspx?scid=kb;en-us;837698
263 std::vector<char> belong_to_group;
265 std::vector<bool> belong_to_group;
268 std::vector< shared_ptr<options_description> > groups;
272 /** Class thrown when duplicate option description is found. */
273 class BOOST_PROGRAM_OPTIONS_DECL duplicate_option_error : public error {
275 duplicate_option_error(const std::string& xwhat) : error(xwhat) {}
279 #if defined(BOOST_MSVC)
280 # pragma warning (pop)