Skip to content

Commit

Permalink
Allow invalid short option syntax
Browse files Browse the repository at this point in the history
Fixes jarro2783#171. Allows invalid syntax for short options to be ignored.
  • Loading branch information
jarro2783 committed May 27, 2019
1 parent cef280f commit d58271c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/cxxopts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit d58271c

Please sign in to comment.