diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index a19d8bbb..c4493689 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -1728,7 +1728,9 @@ ParseResult::parse(int& argc, char**& argv) // but if it starts with a `-`, then it's an error if (argv[current][0] == '-' && argv[current][1] != '\0') { - throw option_syntax_exception(argv[current]); + if (!m_allow_unrecognised) { + throw option_syntax_exception(argv[current]); + } } //if true is returned here then it was consumed, otherwise it is diff --git a/test/options.cpp b/test/options.cpp index 45fc33e6..0a1c9a39 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -549,6 +549,33 @@ TEST_CASE("Unrecognised options", "[options]") { } } +TEST_CASE("Allow bad short syntax", "[options]") { + cxxopts::Options options("unknown_options", " - test unknown options"); + + options.add_options() + ("long", "a long option") + ("s,short", "a short option"); + + Argv av({ + "unknown_options", + "-some_bad_short", + }); + + char** argv = av.argv(); + auto argc = av.argc(); + + SECTION("Default behaviour") { + CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&); + } + + SECTION("After allowing unrecognised options") { + options.allow_unrecognised_options(); + CHECK_NOTHROW(options.parse(argc, argv)); + REQUIRE(argc == 2); + CHECK_THAT(argv[1], Catch::Equals("-some_bad_short")); + } +} + TEST_CASE("Invalid option syntax", "[options]") { cxxopts::Options options("invalid_syntax", " - test invalid syntax");