Commit 97387a6f authored by Matthias Vallentin's avatar Matthias Vallentin

Do not consume input when extract_opts fails

Resolves #332.
parent 602837e0
...@@ -273,7 +273,7 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs, ...@@ -273,7 +273,7 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
// this short opt comes with a value (no space), e.g., -x2 // this short opt comes with a value (no space), e.g., -x2
if (! i->second->fun(arg.substr(2))) { if (! i->second->fun(arg.substr(2))) {
error = "invalid value for " + i->second->name + ": " + arg; error = "invalid value for " + i->second->name + ": " + arg;
return none; return skip_message();
} }
insert_opt_name(i->second); insert_opt_name(i->second);
return none; return none;
...@@ -290,11 +290,11 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs, ...@@ -290,11 +290,11 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
if (j->second->fun) { if (j->second->fun) {
if (eq_pos == std::string::npos) { if (eq_pos == std::string::npos) {
error = "missing argument to " + arg; error = "missing argument to " + arg;
return none; return skip_message();
} }
if (! j->second->fun(arg.substr(eq_pos + 1))) { if (! j->second->fun(arg.substr(eq_pos + 1))) {
error = "invalid value for " + j->second->name + ": " + arg; error = "invalid value for " + j->second->name + ": " + arg;
return none; return skip_message();
} }
insert_opt_name(j->second); insert_opt_name(j->second);
return none; return none;
...@@ -303,7 +303,7 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs, ...@@ -303,7 +303,7 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
return none; return none;
} }
error = "unknown command line option: " + arg; error = "unknown command line option: " + arg;
return none; return skip_message();
}, },
[&](const std::string& arg1, [&](const std::string& arg1,
const std::string& arg2) -> optional<skip_message_t> { const std::string& arg2) -> optional<skip_message_t> {
...@@ -321,13 +321,13 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs, ...@@ -321,13 +321,13 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
CAF_ASSERT(arg1.size() == 2); CAF_ASSERT(arg1.size() == 2);
if (! i->second->fun(arg2)) { if (! i->second->fun(arg2)) {
error = "invalid value for option " + i->second->name + ": " + arg2; error = "invalid value for option " + i->second->name + ": " + arg2;
return none; return skip_message();
} }
insert_opt_name(i->second); insert_opt_name(i->second);
return none; return none;
} }
error = "unknown command line option: " + arg1; error = "unknown command line option: " + arg1;
return none; return skip_message();
} }
}); });
return {res, std::move(opts), std::move(helpstr), std::move(error)}; return {res, std::move(opts), std::move(helpstr), std::move(error)};
......
...@@ -101,6 +101,24 @@ CAF_TEST(extract_opts) { ...@@ -101,6 +101,24 @@ CAF_TEST(extract_opts) {
f({"-f", "hello.txt", "-l5"}); f({"-f", "hello.txt", "-l5"});
f({"-fhello.txt", "-l", "5"}); f({"-fhello.txt", "-l", "5"});
f({"-l5", "-fhello.txt"}); f({"-l5", "-fhello.txt"});
CAF_MESSAGE("ensure that failed parsing doesn't consume input");
auto msg = make_message("-f", "42", "-b", "1337");
auto foo = 0;
auto bar = 0;
auto r = msg.extract_opts({
{"foo,f", "foo desc", foo}
});
CAF_CHECK(r.opts.count("foo") > 0);
CAF_CHECK(foo == 42);
CAF_CHECK(bar == 0);
CAF_CHECK(! r.error.empty()); // -b is an unknown option
CAF_CHECK(! r.remainder.empty() && r.remainder == make_message("-b", "1337"));
r = r.remainder.extract_opts({
{"bar,b", "bar desc", bar}
});
CAF_CHECK(r.opts.count("bar") > 0);
CAF_CHECK(bar == 1337);
CAF_CHECK(r.error.empty());
} }
CAF_TEST(type_token) { CAF_TEST(type_token) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment