Skip to content

Commit 0c01e9a

Browse files
mjdeckervprus
authored andcommitted
Add testing for implicit_values and non-consuming of separate tokens.
1 parent 88dea3c commit 0c01e9a

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

test/cmdline_test.cpp

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ 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,
66+
void apply_syntax(options_description& desc,
67+
positional_options_description & m_positional,
6768
const char* syntax)
6869
{
6970

@@ -77,8 +78,8 @@ void apply_syntax(options_description& desc,
7778
v = value<string>();
7879
s.resize(s.size()-1);
7980
} else if (*(s.end()-1) == '?') {
80-
//v = value<string>()->implicit();
81-
v = value<string>();
81+
v = value<string>()->implicit_value("bar");
82+
m_positional.add("positional", -1);
8283
s.resize(s.size()-1);
8384
} else if (*(s.end()-1) == '*') {
8485
v = value<vector<string> >()->multitoken();
@@ -113,12 +114,14 @@ void test_cmdline(const char* syntax,
113114
}
114115
}
115116
options_description desc;
116-
apply_syntax(desc, syntax);
117+
positional_options_description m_positional;
118+
apply_syntax(desc, m_positional, syntax);
117119

118120
cmdline cmd(xinput);
119121
cmd.style(style);
120122
cmd.set_options_description(desc);
121-
123+
if(m_positional.max_total_count())
124+
cmd.set_positional_options(m_positional);
122125

123126
string result;
124127
int status = 0;
@@ -130,7 +133,9 @@ void test_cmdline(const char* syntax,
130133
{
131134
option opt = options[j];
132135

133-
if (opt.position_key != -1) {
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")) {
134139
if (!result.empty())
135140
result += " ";
136141
result += opt.value[0];
@@ -228,7 +233,7 @@ void test_long_options()
228233
{"--giz", s_success, "Giz:"},
229234
{0, 0, 0}
230235
};
231-
test_cmdline("foo bar= baz? Giz", style, test_cases4);
236+
test_cmdline("foo bar= Giz", style, test_cases4);
232237
}
233238

234239
void test_short_options()
@@ -348,15 +353,15 @@ void test_disguised_long()
348353
{"-bee=x -by", s_success, "bee:x bee:y"},
349354
{0, 0, 0}
350355
};
351-
test_cmdline("foo,f goo,g= bee,b?", style, test_cases1);
356+
test_cmdline("foo,f goo,g= bee,b=", style, test_cases1);
352357

353358
style = cmdline::style_t(style | allow_slash_for_short);
354359
test_case test_cases2[] = {
355360
{"/foo -f", s_success, "foo: foo:"},
356361
{"/goo=x", s_success, "goo:x"},
357362
{0, 0, 0}
358363
};
359-
test_cmdline("foo,f goo,g= bee,b?", style, test_cases2);
364+
test_cmdline("foo,f goo,g=", style, test_cases2);
360365
}
361366

362367
void test_guessing()
@@ -607,6 +612,35 @@ void test_unregistered()
607612
// It's not clear yet, so I'm leaving the decision till later.
608613
}
609614

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+
610644
int main(int /*ac*/, char** /*av*/)
611645
{
612646
test_long_options();
@@ -619,6 +653,7 @@ int main(int /*ac*/, char** /*av*/)
619653
test_additional_parser();
620654
test_style_parser();
621655
test_unregistered();
656+
test_implicit_value();
622657

623658
return 0;
624659
}

0 commit comments

Comments
 (0)