Commit 5da5d67111506a6661bb791b7469a0da0a696a82
1 parent
48e265dc
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.
Showing
3 changed files
with
21 additions
and
0 deletions
CHANGELOG.md
include/cxxopts.hpp
test/options.cpp
| ... | ... | @@ -216,6 +216,22 @@ TEST_CASE("No positional with extras", "[positional]") |
| 216 | 216 | CHECK(argv[1] == std::string("a")); |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | +TEST_CASE("Positional not valid", "[positional]") { | |
| 220 | + cxxopts::Options options("positional_invalid", "invalid positional argument"); | |
| 221 | + options.add_options() | |
| 222 | + ("long", "a long option", cxxopts::value<std::string>()) | |
| 223 | + ; | |
| 224 | + | |
| 225 | + options.parse_positional("something"); | |
| 226 | + | |
| 227 | + Argv av({"foobar", "bar", "baz"}); | |
| 228 | + | |
| 229 | + char** argv = av.argv(); | |
| 230 | + auto argc = av.argc(); | |
| 231 | + | |
| 232 | + CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception); | |
| 233 | +} | |
| 234 | + | |
| 219 | 235 | TEST_CASE("Empty with implicit value", "[implicit]") |
| 220 | 236 | { |
| 221 | 237 | cxxopts::Options options("empty_implicit", "doesn't handle empty"); | ... | ... |