Commit d8e5811e authored by Dominik Charousset's avatar Dominik Charousset

Fix `message::extract_opts` for opts w/o arguments

parent db51f7e9
......@@ -179,6 +179,7 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs) const {
auto i = shorts.find(arg);
if (i != shorts.end()) {
if (i->second->fun) {
// this short opt expects two arguments
return skip_message();
}
insert_opt_name(i->second);
......@@ -212,7 +213,13 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs) const {
return skip_message();
}
auto i = shorts.find(arg1);
if (i != shorts.end() && i->second->fun) {
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
return skip_message();
}
if (!i->second->fun(arg2)) {
std::cerr << "invalid value for option "
<< i->second->name << ": " << arg2 << std::endl;
......@@ -226,14 +233,14 @@ message::cli_res message::extract_opts(std::vector<cli_arg> xs) const {
}
});
size_t name_width = 0;
for (auto& ca : xs) {
for (auto& x : xs) {
// name field contains either only "--<long_name>" or
// "-<short name> [--<long name>]" depending on whether or not
// a ',' appears in the name
auto nw = ca.name.find(',') == std::string::npos
? ca.name.size() + 2 // "--<name>"
: ca.name.size() + 5; // "-X [--<name>]" (minus trailing ",X")
if (ca.fun) {
auto nw = x.name.find(',') == std::string::npos
? x.name.size() + 2 // "--<name>"
: x.name.size() + 5; // "-X [--<name>]" (minus trailing ",X")
if (x.fun) {
nw += 4; // trailing " arg"
}
name_width = std::max(name_width, nw);
......
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