Commit 4a5e68b6 authored by Matthias Vallentin's avatar Matthias Vallentin

Fix bug in option parser with single option

Closes #295.
parent 124b2926
......@@ -226,6 +226,7 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
insert_opt_name(i->second);
return none;
}
// no value given, try two-argument form below
return skip_message();
}
insert_opt_name(i->second);
......@@ -257,14 +258,16 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs,
if (arg1.size() < 2 || arg1[0] != '-' || arg1[1] == '-') {
return skip_message();
}
auto i = shorts.find(arg1);
auto i = shorts.find(arg1.substr(0, 2));
if (i != shorts.end()) {
if (!i->second->fun) {
// this short opt does not expect an argument,
// tell extract to try this option again with one
// less argument
if (i->second->fun) {
if (arg1.size() > 2) {
// this short opt comes with a value (no space), e.g., -x2, so we
// have to parse it with the one-argument form above
return skip_message();
}
CAF_ASSERT(arg1.size() == 2);
}
if (!i->second->fun(arg2)) {
error = "invalid value for option " + i->second->name + ": " + arg2;
return none;
......
......@@ -95,17 +95,24 @@ CAF_TEST(extract3) {
CAF_TEST(extract_opts) {
auto f = [](std::vector<std::string> xs) {
std::string filename;
size_t log_level;
auto res = message_builder(xs.begin(), xs.end()).extract_opts({
{"version,v", "print version"},
{"log-level,l", "set the log level", log_level},
{"file,f", "set output file", filename},
{"whatever", "do whatever"}
});
CAF_CHECK_EQUAL(res.opts.count("file"), 1);
CAF_CHECK_EQUAL(to_string(res.remainder), to_string(message{}));
CAF_CHECK_EQUAL(filename, "hello.txt");
CAF_CHECK_EQUAL(log_level, 5);
};
f({"--file=hello.txt"});
f({"-f", "hello.txt"});
f({"--file=hello.txt", "-l", "5"});
f({"-f", "hello.txt", "--log-level=5"});
f({"-f", "hello.txt", "-l", "5"});
f({"-f", "hello.txt", "-l5"});
f({"-fhello.txt", "-l", "5"});
f({"-l5", "-fhello.txt"});
}
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