Throw exception on invalid positional argument

Fixes #169. It seems reasonable to throw an exception when an attempt is
made to parse into positional parameters that don't exist.
This commit is contained in:
Jarryd Beck 2019-05-01 18:34:29 +10:00
parent 48e265dc4b
commit 5da5d67111
3 changed files with 21 additions and 0 deletions

View File

@ -13,6 +13,7 @@ options. The project adheres to semantic versioning.
### Added ### Added
* Iterator inputs to `parse_positional`. * Iterator inputs to `parse_positional`.
* Throw an exception if the option in `parse_positional` doesn't exist.
### Bug Fixes ### Bug Fixes

View File

@ -1659,6 +1659,10 @@ ParseResult::consume_positional(std::string a)
return true; return true;
} }
} }
else
{
throw option_not_exists_exception(*m_next_positional);
}
++m_next_positional; ++m_next_positional;
} }

View File

@ -216,6 +216,22 @@ TEST_CASE("No positional with extras", "[positional]")
CHECK(argv[1] == std::string("a")); CHECK(argv[1] == std::string("a"));
} }
TEST_CASE("Positional not valid", "[positional]") {
cxxopts::Options options("positional_invalid", "invalid positional argument");
options.add_options()
("long", "a long option", cxxopts::value<std::string>())
;
options.parse_positional("something");
Argv av({"foobar", "bar", "baz"});
char** argv = av.argv();
auto argc = av.argc();
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception);
}
TEST_CASE("Empty with implicit value", "[implicit]") TEST_CASE("Empty with implicit value", "[implicit]")
{ {
cxxopts::Options options("empty_implicit", "doesn't handle empty"); cxxopts::Options options("empty_implicit", "doesn't handle empty");