Skip to content

Commit

Permalink
Merge pull request #8 from pavelosipov/improve_error_reporting
Browse files Browse the repository at this point in the history
Improve error handling for unrecognized arguments
  • Loading branch information
pfultz2 authored Aug 5, 2021
2 parents 7db6a05 + 1a0ff79 commit 15799be
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions include/args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,14 @@ struct context
this->add(std::move(arg));
}

bool is_known_flag(const std::string& flag) const noexcept
{
return lookup.find(flag) != lookup.end();
}

argument& operator[](const std::string& flag)
{
if (lookup.find(flag) == lookup.end())
if (!is_known_flag(flag))
{
throw std::runtime_error("unknown flag: " + flag);
}
Expand All @@ -365,7 +370,7 @@ struct context

const argument& operator[](const std::string& flag) const
{
if (lookup.find(flag) == lookup.end())
if (!is_known_flag(flag))
{
throw std::runtime_error("unknown flag: " + flag);
}
Expand Down Expand Up @@ -627,7 +632,14 @@ void parse(T& cmd, std::deque<std::string> a, Ts&&... xs)
ctx.subcommands[x].run(drop(a), cmd, xs...);
break;
}
else if (ctx[""].write(x)) return;
else if (ctx.is_known_flag(""))
{
if (ctx[""].write(x)) return;
}
else
{
throw std::runtime_error("unrecognized arg: " + x);
}
}
}
ctx.post_process();
Expand Down

0 comments on commit 15799be

Please sign in to comment.