Allow invalid short option syntax
Fixes #171. Allows invalid syntax for short options to be ignored.
This commit is contained in:
parent
cef280fad3
commit
d58271c5fd
@ -1728,7 +1728,9 @@ ParseResult::parse(int& argc, char**& argv)
|
|||||||
|
|
||||||
// but if it starts with a `-`, then it's an error
|
// but if it starts with a `-`, then it's an error
|
||||||
if (argv[current][0] == '-' && argv[current][1] != '\0') {
|
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
|
//if true is returned here then it was consumed, otherwise it is
|
||||||
|
@ -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]") {
|
TEST_CASE("Invalid option syntax", "[options]") {
|
||||||
cxxopts::Options options("invalid_syntax", " - test invalid syntax");
|
cxxopts::Options options("invalid_syntax", " - test invalid syntax");
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user