Allow invalid short option syntax

Fixes #171. Allows invalid syntax for short options to be ignored.
This commit is contained in:
Jarryd Beck 2019-05-28 08:15:23 +10:00
parent cef280fad3
commit d58271c5fd
2 changed files with 30 additions and 1 deletions

View File

@ -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

View File

@ -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");