Skip to content

Commit 7729850

Browse files
committed
Make options with implicit value use next token.
This commit reverts: - 88dea3c. "Stop options with implicit value from consuming separate tokens." - 0c01e9a. "Add testing for implicit_values and non-consuming of separate tokens." These commits from 2014 made options with implicit_value set only consult value in the same token. The problem is that now implicit_value, a sematic properly, forces a particular syntax, which proved to be confusing.
1 parent 5dc3255 commit 7729850

File tree

3 files changed

+10
-56
lines changed

3 files changed

+10
-56
lines changed

include/boost/program_options/value_semantic.hpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ namespace boost { namespace program_options {
3838
should be present on the command line. */
3939
virtual unsigned max_tokens() const = 0;
4040

41-
/** Returns true if the option should only take adjacent token,
42-
not one from further command-line arguments.
43-
*/
44-
virtual bool adjacent_tokens_only() const = 0;
45-
4641
/** Returns true if values from different sources should be composed.
4742
Otherwise, value from the first source is used and values from
4843
other sources are discarded.
@@ -53,7 +48,7 @@ namespace boost { namespace program_options {
5348
5449
*/
5550
virtual bool is_required() const = 0;
56-
51+
5752
/** Parses a group of tokens that specify a value of option.
5853
Stores the result in 'value_store', using whatever representation
5954
is desired. May be be called several times if value of the same
@@ -139,7 +134,6 @@ namespace boost { namespace program_options {
139134

140135
unsigned min_tokens() const;
141136
unsigned max_tokens() const;
142-
bool adjacent_tokens_only() const { return false; }
143137

144138
bool is_composing() const { return false; }
145139

@@ -331,8 +325,6 @@ namespace boost { namespace program_options {
331325
}
332326
}
333327

334-
bool adjacent_tokens_only() const { return !m_implicit_value.empty(); }
335-
336328
bool is_required() const { return m_required; }
337329

338330
/** Creates an instance of the 'validator' class and calls

src/cmdline.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,6 @@ namespace boost { namespace program_options { namespace detail {
313313
if (!xd)
314314
continue;
315315

316-
if (xd->semantic()->adjacent_tokens_only())
317-
continue;
318-
319316
unsigned min_tokens = xd->semantic()->min_tokens();
320317
unsigned max_tokens = xd->semantic()->max_tokens();
321318
if (min_tokens < max_tokens && opt.value.size() < max_tokens)

test/cmdline_test.cpp

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ struct test_case {
6363
The "boost::program_options" in parameter type is needed because CW9
6464
has std::detail and it causes an ambiguity.
6565
*/
66-
void apply_syntax(options_description& desc,
67-
positional_options_description & m_positional,
66+
void apply_syntax(options_description& desc,
6867
const char* syntax)
6968
{
7069

@@ -78,8 +77,8 @@ void apply_syntax(options_description& desc,
7877
v = value<string>();
7978
s.resize(s.size()-1);
8079
} else if (*(s.end()-1) == '?') {
81-
v = value<string>()->implicit_value("bar");
82-
m_positional.add("positional", -1);
80+
//v = value<string>()->implicit();
81+
v = value<string>();
8382
s.resize(s.size()-1);
8483
} else if (*(s.end()-1) == '*') {
8584
v = value<vector<string> >()->multitoken();
@@ -114,14 +113,12 @@ void test_cmdline(const char* syntax,
114113
}
115114
}
116115
options_description desc;
117-
positional_options_description m_positional;
118-
apply_syntax(desc, m_positional, syntax);
116+
apply_syntax(desc, syntax);
119117

120118
cmdline cmd(xinput);
121119
cmd.style(style);
122120
cmd.set_options_description(desc);
123-
if(m_positional.max_total_count())
124-
cmd.set_positional_options(m_positional);
121+
125122

126123
string result;
127124
int status = 0;
@@ -133,9 +130,7 @@ void test_cmdline(const char* syntax,
133130
{
134131
option opt = options[j];
135132

136-
if (opt.position_key != -1
137-
&& (m_positional.max_total_count() == 0 || (size_t)opt.position_key >= m_positional.max_total_count()
138-
|| m_positional.name_for_position(opt.position_key) != "positional")) {
133+
if (opt.position_key != -1) {
139134
if (!result.empty())
140135
result += " ";
141136
result += opt.value[0];
@@ -233,7 +228,7 @@ void test_long_options()
233228
{"--giz", s_success, "Giz:"},
234229
{0, 0, 0}
235230
};
236-
test_cmdline("foo bar= Giz", style, test_cases4);
231+
test_cmdline("foo bar= baz? Giz", style, test_cases4);
237232
}
238233

239234
void test_short_options()
@@ -353,15 +348,15 @@ void test_disguised_long()
353348
{"-bee=x -by", s_success, "bee:x bee:y"},
354349
{0, 0, 0}
355350
};
356-
test_cmdline("foo,f goo,g= bee,b=", style, test_cases1);
351+
test_cmdline("foo,f goo,g= bee,b?", style, test_cases1);
357352

358353
style = cmdline::style_t(style | allow_slash_for_short);
359354
test_case test_cases2[] = {
360355
{"/foo -f", s_success, "foo: foo:"},
361356
{"/goo=x", s_success, "goo:x"},
362357
{0, 0, 0}
363358
};
364-
test_cmdline("foo,f goo,g=", style, test_cases2);
359+
test_cmdline("foo,f goo,g= bee,b?", style, test_cases2);
365360
}
366361

367362
void test_guessing()
@@ -612,35 +607,6 @@ void test_unregistered()
612607
// It's not clear yet, so I'm leaving the decision till later.
613608
}
614609

615-
void test_implicit_value()
616-
{
617-
using namespace command_line_style;
618-
cmdline::style_t style;
619-
620-
style = cmdline::style_t(
621-
allow_long | long_allow_adjacent
622-
);
623-
624-
test_case test_cases1[] = {
625-
{"--foo bar", s_success, "foo: positional:bar"},
626-
{"--foo=bar foobar", s_success, "foo:bar positional:foobar"},
627-
{0, 0, 0}
628-
};
629-
630-
test_cmdline("positional= foo?", style, test_cases1);
631-
632-
style = cmdline::style_t(
633-
allow_short | allow_dash_for_short
634-
| short_allow_adjacent);
635-
636-
test_case test_cases2[] = {
637-
{"-f bar", s_success, "-f: positional:bar"},
638-
{"-fbar foobar", s_success, "-f:bar positional:foobar"},
639-
{0, 0, 0}
640-
};
641-
test_cmdline("positional= ,f?", style, test_cases2);
642-
}
643-
644610
int main(int /*ac*/, char** /*av*/)
645611
{
646612
test_long_options();
@@ -653,7 +619,6 @@ int main(int /*ac*/, char** /*av*/)
653619
test_additional_parser();
654620
test_style_parser();
655621
test_unregistered();
656-
test_implicit_value();
657622

658623
return 0;
659624
}

0 commit comments

Comments
 (0)