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>
29 #if defined(BOOST_MSVC)
30 # pragma warning (push)
31 # 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'
35 /** Boost namespace */
37 /** Namespace for the library. */
38 namespace program_options {
40 /** Describes one possible command line/config file option. There are two
41 kinds of properties of an option. First describe it syntactically and
42 are used only to validate input. Second affect interpretation of the
43 option, for example default value for it or function that should be
44 called when the value is finally known. Routines which perform parsing
45 never use second kind of properties \-- they are side effect free.
46 @sa options_description
48 class BOOST_PROGRAM_OPTIONS_DECL option_description {
53 /** Initializes the object with the passed data.
55 Note: it would be nice to make the second parameter auto_ptr,
56 to explicitly pass ownership. Unfortunately, it's often needed to
57 create objects of types derived from 'value_semantic':
58 options_description d;
59 d.add_options()("a", parameter<int>("n")->default_value(1));
60 Here, the static type returned by 'parameter' should be derived
63 Alas, derived->base conversion for auto_ptr does not really work,
65 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2000/n1232.pdf
66 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#84
68 So, we have to use plain old pointers. Besides, users are not
69 expected to use the constructor directly.
72 The 'name' parameter is interpreted by the following rules:
73 - if there's no "," character in 'name', it specifies long name
74 - otherwise, the part before "," specifies long name and the part
77 option_description(const char* name,
78 const value_semantic* s);
80 /** Initializes the class with the passed data.
82 option_description(const char* name,
83 const value_semantic* s,
84 const char* description);
86 virtual ~option_description();
88 enum match_result { no_match, full_match, approximate_match };
90 /** Given 'option', specified in the input source,
91 returns 'true' if 'option' specifies *this.
93 match_result match(const std::string& option, bool approx,
94 bool long_ignore_case, bool short_ignore_case) const;
96 /** Returns the key that should identify the option, in
97 particular in the variables_map class.
98 The 'option' parameter is the option spelling from the
100 If option name contains '*', returns 'option'.
101 If long name was specified, it's the long name, otherwise
102 it's a short name with prepended '-'.
104 const std::string& key(const std::string& option) const;
107 /** Returns the canonical name for the option description to enable the user to
108 recognised a matching option.
109 1) For short options ('-', '/'), returns the short name prefixed.
110 2) For long options ('--' / '-') returns the first long name prefixed
111 3) All other cases, returns the first long name (if present) or the short
114 std::string canonical_display_name(int canonical_option_style = 0) const;
116 const std::string& long_name() const;
118 const std::pair<const std::string*, std::size_t> long_names() const;
120 /// Explanation of this option
121 const std::string& description() const;
123 /// Semantic of option's value
124 shared_ptr<const value_semantic> semantic() const;
126 /// Returns the option name, formatted suitably for usage message.
127 std::string format_name() const;
129 /** Returns the parameter name and properties, formatted suitably for
131 std::string format_parameter() const;
135 option_description& set_names(const char* name);
138 * a one-character "switch" name - with its prefix,
139 * so that this is either empty or has length 2 (e.g. "-c"
141 std::string m_short_name;
144 * one or more names by which this option may be specified
145 * on a command-line or in a config file, which are not
146 * a single-letter switch. The names here are _without_
149 std::vector<std::string> m_long_names;
151 std::string m_description;
153 // shared_ptr is needed to simplify memory management in
154 // copy ctor and destructor.
155 shared_ptr<const value_semantic> m_value_semantic;
158 class options_description;
160 /** Class which provides convenient creation syntax to option_description.
162 class BOOST_PROGRAM_OPTIONS_DECL options_description_easy_init {
164 options_description_easy_init(options_description* owner);
166 options_description_easy_init&
167 operator()(const char* name,
168 const char* description);
170 options_description_easy_init&
171 operator()(const char* name,
172 const value_semantic* s);
174 options_description_easy_init&
175 operator()(const char* name,
176 const value_semantic* s,
177 const char* description);
180 options_description* owner;
184 /** A set of option descriptions. This provides convenient interface for
185 adding new option (the add_options) method, and facilities to search
188 See @ref a_adding_options "here" for option adding interface discussion.
189 @sa option_description
191 class BOOST_PROGRAM_OPTIONS_DECL options_description {
193 static const unsigned m_default_line_length;
195 /** Creates the instance. */
196 options_description(unsigned line_length = m_default_line_length,
197 unsigned min_description_length = m_default_line_length / 2);
198 /** Creates the instance. The 'caption' parameter gives the name of
199 this 'options_description' instance. Primarily useful for output.
200 The 'description_length' specifies the number of columns that
201 should be reserved for the description text; if the option text
202 encroaches into this, then the description will start on the next
205 options_description(const std::string& caption,
206 unsigned line_length = m_default_line_length,
207 unsigned min_description_length = m_default_line_length / 2);
208 /** Adds new variable description. Throws duplicate_variable_error if
209 either short or long name matches that of already present one.
211 void add(shared_ptr<option_description> desc);
212 /** Adds a group of option description. This has the same
213 effect as adding all option_descriptions in 'desc'
214 individually, except that output operator will show
218 options_description& add(const options_description& desc);
220 /** Find the maximum width of the option column, including options
222 unsigned get_option_column_width() const;
225 /** Returns an object of implementation-defined type suitable for adding
226 options to options_description. The returned object will
227 have overloaded operator() with parameter type matching
228 'option_description' constructors. Calling the operator will create
229 new option_description instance and add it.
231 options_description_easy_init add_options();
233 const option_description& find(const std::string& name,
235 bool long_ignore_case = false,
236 bool short_ignore_case = false) const;
238 const option_description* find_nothrow(const std::string& name,
240 bool long_ignore_case = false,
241 bool short_ignore_case = false) const;
244 const std::vector< shared_ptr<option_description> >& options() const;
246 /** Produces a human readable output of 'desc', listing options,
247 their descriptions and allowed parameters. Other options_description
248 instances previously passed to add will be output separately. */
249 friend BOOST_PROGRAM_OPTIONS_DECL std::ostream& operator<<(std::ostream& os,
250 const options_description& desc);
252 /** Outputs 'desc' to the specified stream, calling 'f' to output each
253 option_description element. */
254 void print(std::ostream& os, unsigned width = 0) const;
257 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1800))
258 // prevent warning C4512: assignment operator could not be generated
259 options_description& operator=(const options_description&);
262 typedef std::map<std::string, int>::const_iterator name2index_iterator;
263 typedef std::pair<name2index_iterator, name2index_iterator>
266 //approximation_range find_approximation(const std::string& prefix) const;
268 std::string m_caption;
269 const unsigned m_line_length;
270 const unsigned m_min_description_length;
272 // Data organization is chosen because:
273 // - there could be two names for one option
274 // - option_add_proxy needs to know the last added option
275 std::vector< shared_ptr<option_description> > m_options;
277 // Whether the option comes from one of declared groups.
278 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, BOOST_TESTED_AT(313))
279 // vector<bool> is buggy there, see
280 // http://support.microsoft.com/default.aspx?scid=kb;en-us;837698
281 std::vector<char> belong_to_group;
283 std::vector<bool> belong_to_group;
286 std::vector< shared_ptr<options_description> > groups;
290 /** Class thrown when duplicate option description is found. */
291 class BOOST_PROGRAM_OPTIONS_DECL duplicate_option_error : public error {
293 duplicate_option_error(const std::string& xwhat) : error(xwhat) {}
297 #if defined(BOOST_MSVC)
298 # pragma warning (pop)