Skip to content

Commit 2d0c627

Browse files
committed
Full merge from trunk at revision 41356 of entire boost-root tree.
[SVN r41369]
1 parent 2cc29f6 commit 2d0c627

File tree

8 files changed

+80
-16
lines changed

8 files changed

+80
-16
lines changed

include/boost/program_options.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

6+
// See www.boost.org/libs/program_options for documentation.
7+
68
#ifndef PROGRAM_OPTIONS_VP_2003_05_19
79
#define PROGRAM_OPTIONS_VP_2003_05_19
810

include/boost/program_options/detail/config_file.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ namespace boost { namespace program_options { namespace detail {
7070
public:
7171
common_config_file_iterator() { found_eof(); }
7272
common_config_file_iterator(
73-
const std::set<std::string>& allowed_options);
73+
const std::set<std::string>& allowed_options,
74+
bool allow_unregistered = false);
7475

7576
virtual ~common_config_file_iterator() {}
7677

@@ -103,6 +104,7 @@ namespace boost { namespace program_options { namespace detail {
103104
// Invariant: no element is prefix of other element.
104105
std::set<std::string> allowed_prefixes;
105106
std::string m_prefix;
107+
bool m_allow_unregistered;
106108
};
107109

108110
template<class charT>
@@ -116,7 +118,8 @@ namespace boost { namespace program_options { namespace detail {
116118
/** Creates a config file parser for the specified stream.
117119
*/
118120
basic_config_file_iterator(std::basic_istream<charT>& is,
119-
const std::set<std::string>& allowed_options);
121+
const std::set<std::string>& allowed_options,
122+
bool allow_unregistered = false);
120123

121124
private: // base overrides
122125

@@ -139,8 +142,9 @@ namespace boost { namespace program_options { namespace detail {
139142
template<class charT>
140143
basic_config_file_iterator<charT>::
141144
basic_config_file_iterator(std::basic_istream<charT>& is,
142-
const std::set<std::string>& allowed_options)
143-
: common_config_file_iterator(allowed_options)
145+
const std::set<std::string>& allowed_options,
146+
bool allow_unregistered)
147+
: common_config_file_iterator(allowed_options, allow_unregistered)
144148
{
145149
this->is.reset(&is, null_deleter());
146150
get();

include/boost/program_options/detail/value_semantic.hpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ namespace boost { namespace program_options {
1616
std::string
1717
typed_value<T, charT>::name() const
1818
{
19-
if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
19+
if (!m_implicit_value.empty() && !m_implicit_value_as_text.empty()) {
20+
std::string msg = "[=arg(=" + m_implicit_value_as_text + ")]";
21+
if (!m_default_value.empty() && !m_default_value_as_text.empty())
22+
msg += " (=" + m_default_value_as_text + ")";
23+
return msg;
24+
}
25+
else if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
2026
return arg + " (=" + m_default_value_as_text + ")";
2127
} else {
2228
return arg;
@@ -134,7 +140,14 @@ namespace boost { namespace program_options {
134140
for (unsigned i = 0; i < s.size(); ++i)
135141
{
136142
try {
137-
tv->push_back(boost::lexical_cast<T>(s[i]));
143+
/* We call validate so that if user provided
144+
a validator for class T, we use it even
145+
when parsing vector<T>. */
146+
boost::any a;
147+
std::vector<std::basic_string<charT> > v;
148+
v.push_back(s[i]);
149+
validate(a, v, (T*)0, 0);
150+
tv->push_back(boost::any_cast<T>(a));
138151
}
139152
catch(const bad_lexical_cast& /*e*/) {
140153
boost::throw_exception(invalid_option_value(s[i]));
@@ -148,7 +161,13 @@ namespace boost { namespace program_options {
148161
xparse(boost::any& value_store,
149162
const std::vector<std::basic_string<charT> >& new_tokens) const
150163
{
151-
validate(value_store, new_tokens, (T*)0, 0);
164+
// If no tokens were given, and the option accepts an implicit
165+
// value, then assign the implicit value as the stored value;
166+
// otherwise, validate the user-provided token(s).
167+
if (new_tokens.empty() && !m_implicit_value.empty())
168+
value_store = m_implicit_value;
169+
else
170+
validate(value_store, new_tokens, (T*)0, 0);
152171
}
153172

154173
template<class T>

include/boost/program_options/options_description.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ namespace program_options {
155155
*/
156156
class BOOST_PROGRAM_OPTIONS_DECL options_description {
157157
public:
158-
static const unsigned m_default_line_length = 80;
158+
static const unsigned m_default_line_length;
159159

160160
/** Creates the instance. */
161161
options_description(unsigned line_length = m_default_line_length);

include/boost/program_options/parsers.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ namespace boost { namespace program_options {
153153
BOOST_PROGRAM_OPTIONS_DECL
154154
#endif
155155
basic_parsed_options<charT>
156-
parse_config_file(std::basic_istream<charT>&, const options_description&);
156+
parse_config_file(std::basic_istream<charT>&, const options_description&,
157+
bool allow_unregistered = false);
157158

158159
/** Controls if the 'collect_unregistered' function should
159160
include positional options, or not. */

include/boost/program_options/positional_options.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace boost { namespace program_options {
4141
add(const char* name, int max_count);
4242

4343
/** Returns the maximum number of positional options that can
44-
be present. Can return (numeric_limits<unsigned>::max)() to
44+
be present. Can return numeric_limits<unsigned>::max() to
4545
indicate unlimited number. */
4646
unsigned max_total_count() const;
4747

include/boost/program_options/value_semantic.hpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,38 @@ namespace boost { namespace program_options {
204204
return this;
205205
}
206206

207+
/** Specifies an implicit value, which will be used
208+
if the option is given, but without an adjacent value.
209+
Using this implies that an explicit value is optional, but if
210+
given, must be strictly adjacent to the option, i.e.: '-ovalue'
211+
or '--option=value'. Giving '-o' or '--option' will cause the
212+
implicit value to be applied.
213+
*/
214+
typed_value* implicit_value(const T &v)
215+
{
216+
m_implicit_value = boost::any(v);
217+
m_implicit_value_as_text =
218+
boost::lexical_cast<std::string>(v);
219+
return this;
220+
}
221+
222+
/** Specifies an implicit value, which will be used
223+
if the option is given, but without an adjacent value.
224+
Using this implies that an explicit value is optional, but if
225+
given, must be strictly adjacent to the option, i.e.: '-ovalue'
226+
or '--option=value'. Giving '-o' or '--option' will cause the
227+
implicit value to be applied.
228+
Unlike the above overload, the type 'T' need not provide
229+
operator<< for ostream, but textual representation of default
230+
value must be provided by the user.
231+
*/
232+
typed_value* implicit_value(const T &v, const std::string& textual)
233+
{
234+
m_implicit_value = boost::any(v);
235+
m_implicit_value_as_text = textual;
236+
return this;
237+
}
238+
207239
/** Specifies a function to be called when the final value
208240
is determined. */
209241
typed_value* notifier(function1<void, const T&> f)
@@ -243,7 +275,7 @@ namespace boost { namespace program_options {
243275

244276
unsigned min_tokens() const
245277
{
246-
if (m_zero_tokens) {
278+
if (m_zero_tokens || !m_implicit_value.empty()) {
247279
return 0;
248280
} else {
249281
return 1;
@@ -301,6 +333,8 @@ namespace boost { namespace program_options {
301333
// as boost::optional to avoid unnecessary instantiations.
302334
boost::any m_default_value;
303335
std::string m_default_value_as_text;
336+
boost::any m_implicit_value;
337+
std::string m_implicit_value_as_text;
304338
bool m_composing, m_implicit, m_multitoken, m_zero_tokens;
305339
boost::function1<void, const T&> m_notifier;
306340
};

include/boost/program_options/variables_map.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ namespace boost { namespace program_options {
3030
If 'm' already has a non-defaulted value of an option, that value
3131
is not changed, even if 'options' specify some value.
3232
*/
33-
BOOST_PROGRAM_OPTIONS_DECL void store(const basic_parsed_options<char>& options, variables_map& m,
33+
BOOST_PROGRAM_OPTIONS_DECL
34+
void store(const basic_parsed_options<char>& options, variables_map& m,
3435
bool utf8 = false);
3536

3637
/** Stores in 'm' all options that are defined in 'options'.
3738
If 'm' already has a non-defaulted value of an option, that value
3839
is not changed, even if 'options' specify some value.
3940
This is wide character variant.
4041
*/
41-
BOOST_PROGRAM_OPTIONS_DECL void store(const basic_parsed_options<wchar_t>& options,
42+
BOOST_PROGRAM_OPTIONS_DECL
43+
void store(const basic_parsed_options<wchar_t>& options,
4244
variables_map& m);
4345

4446

@@ -87,9 +89,10 @@ namespace boost { namespace program_options {
8789
// be easily accessible, so we need to store semantic here.
8890
shared_ptr<const value_semantic> m_value_semantic;
8991

90-
friend void store(const basic_parsed_options<char>& options,
92+
friend BOOST_PROGRAM_OPTIONS_DECL
93+
void store(const basic_parsed_options<char>& options,
9194
variables_map& m, bool);
92-
friend void notify(variables_map& m);
95+
friend BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);
9396
};
9497

9598
/** Implements string->string mapping with convenient value casting
@@ -154,7 +157,8 @@ namespace boost { namespace program_options {
154157
be changed by subsequence assignments. */
155158
std::set<std::string> m_final;
156159

157-
friend void store(const basic_parsed_options<char>& options,
160+
friend BOOST_PROGRAM_OPTIONS_DECL
161+
void store(const basic_parsed_options<char>& options,
158162
variables_map& xm,
159163
bool utf8);
160164
};

0 commit comments

Comments
 (0)