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