Commit d58271c5fd4d3cd43ea55fd5923243d9171fbd55

Authored by Jarryd Beck
1 parent cef280fa

Allow invalid short option syntax

Fixes #171. Allows invalid syntax for short options to be ignored.
include/cxxopts.hpp
... ... @@ -1728,7 +1728,9 @@ ParseResult::parse(int& argc, char**& argv)
1728 1728  
1729 1729 // but if it starts with a `-`, then it's an error
1730 1730 if (argv[current][0] == '-' && argv[current][1] != '\0') {
1731   - throw option_syntax_exception(argv[current]);
  1731 + if (!m_allow_unrecognised) {
  1732 + throw option_syntax_exception(argv[current]);
  1733 + }
1732 1734 }
1733 1735  
1734 1736 //if true is returned here then it was consumed, otherwise it is
... ...
test/options.cpp
... ... @@ -549,6 +549,33 @@ TEST_CASE("Unrecognised options", "[options]") {
549 549 }
550 550 }
551 551  
  552 +TEST_CASE("Allow bad short syntax", "[options]") {
  553 + cxxopts::Options options("unknown_options", " - test unknown options");
  554 +
  555 + options.add_options()
  556 + ("long", "a long option")
  557 + ("s,short", "a short option");
  558 +
  559 + Argv av({
  560 + "unknown_options",
  561 + "-some_bad_short",
  562 + });
  563 +
  564 + char** argv = av.argv();
  565 + auto argc = av.argc();
  566 +
  567 + SECTION("Default behaviour") {
  568 + CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
  569 + }
  570 +
  571 + SECTION("After allowing unrecognised options") {
  572 + options.allow_unrecognised_options();
  573 + CHECK_NOTHROW(options.parse(argc, argv));
  574 + REQUIRE(argc == 2);
  575 + CHECK_THAT(argv[1], Catch::Equals("-some_bad_short"));
  576 + }
  577 +}
  578 +
552 579 TEST_CASE("Invalid option syntax", "[options]") {
553 580 cxxopts::Options options("invalid_syntax", " - test invalid syntax");
554 581  
... ...