Commit 3157d0b9 authored by Sebastian Woelke's avatar Sebastian Woelke

Fix all args after "--" are part of the remainder

parent 0229c9d6
...@@ -236,8 +236,15 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs, ...@@ -236,8 +236,15 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
// we can't `return make_error(...)` from inside `extract`, hence we // we can't `return make_error(...)` from inside `extract`, hence we
// store any occurred error in a temporary variable returned at the end // store any occurred error in a temporary variable returned at the end
std::string error; std::string error;
bool skip_remainder = false;
auto res = extract({ auto res = extract({
[&](const std::string& arg) -> optional<skip_t> { [&](const std::string& arg) -> optional<skip_t> {
if (arg == "--") {
skip_remainder = true;
return none;
}
if (skip_remainder)
return skip();
if (arg.empty() || arg.front() != '-') { if (arg.empty() || arg.front() != '-') {
return skip(); return skip();
} }
...@@ -287,6 +294,12 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs, ...@@ -287,6 +294,12 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
}, },
[&](const std::string& arg1, [&](const std::string& arg1,
const std::string& arg2) -> optional<skip_t> { const std::string& arg2) -> optional<skip_t> {
if (arg1 == "--") {
skip_remainder = true;
return skip();
}
if (skip_remainder)
return skip();
if (arg1.size() < 2 || arg1[0] != '-' || arg1[1] == '-') { if (arg1.size() < 2 || arg1[0] != '-' || arg1[1] == '-') {
return skip(); return skip();
} }
......
...@@ -107,7 +107,7 @@ CAF_TEST(extract2) { ...@@ -107,7 +107,7 @@ CAF_TEST(extract2) {
} }
CAF_TEST(extract_opts) { CAF_TEST(extract_opts) {
auto f = [](std::vector<std::string> xs) { auto f = [](std::vector<std::string> xs, std::vector<std::string> remainder) {
std::string filename; std::string filename;
size_t log_level; size_t log_level;
auto res = message_builder(xs.begin(), xs.end()).extract_opts({ auto res = message_builder(xs.begin(), xs.end()).extract_opts({
...@@ -117,16 +117,33 @@ CAF_TEST(extract_opts) { ...@@ -117,16 +117,33 @@ CAF_TEST(extract_opts) {
{"whatever", "do whatever"} {"whatever", "do whatever"}
}); });
CAF_CHECK_EQUAL(res.opts.count("file"), 1u); CAF_CHECK_EQUAL(res.opts.count("file"), 1u);
CAF_CHECK(res.remainder.empty()); CAF_CHECK(res.remainder.size() == remainder.size());
std::cerr << "res.remainder: ";
for(size_t i= 0; i < res.remainder.size(); ++i) {
std::cerr << res.remainder.get_as<string>(i) << " ";
}
std::cerr << std::endl;
std::cerr << "remainder: ";
for(auto& e : remainder) {
std::cerr << e << " ";
}
std::cerr << std::endl;
CAF_CHECK_EQUAL(filename, "hello.txt"); CAF_CHECK_EQUAL(filename, "hello.txt");
CAF_CHECK_EQUAL(log_level, 5u); CAF_CHECK_EQUAL(log_level, 5u);
}; };
f({"--file=hello.txt", "-l", "5"}); f({"--file=hello.txt", "-l", "5"}, {});
f({"-f", "hello.txt", "--log-level=5"}); f({"-f", "hello.txt", "--log-level=5"}, {});
f({"-f", "hello.txt", "-l", "5"}); f({"-f", "hello.txt", "-l", "5"}, {});
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"}, {});
f({"--file=hello.txt", "-l", "5", "--", "a"}, {"a"});
f({"--file=hello.txt", "-l", "5", "--", "a", "b"}, {"a", "b"});
f({"--file=hello.txt", "-l", "5", "--", "aa", "bb"}, {"aa", "bb"});
f({"--file=hello.txt", "-l", "5", "--", "-a", "--bb"}, {"-a", "--bb"});
f({"--file=hello.txt", "-l", "5", "--", "-a1", "--bb=10"},
{"-a1", "--bb=10"});
f({"--file=hello.txt", "-l", "5", "--", "-a 1", "--b=10"}, {"-a1", "--b=10"});
CAF_MESSAGE("ensure that failed parsing doesn't consume input"); CAF_MESSAGE("ensure that failed parsing doesn't consume input");
auto msg = make_message("-f", "42", "-b", "1337"); auto msg = make_message("-f", "42", "-b", "1337");
auto foo = 0; auto foo = 0;
......
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