Fix option matching
This commit is contained in:
parent
cd65c459ec
commit
b528324107
@ -19,6 +19,7 @@ options. The project adheres to semantic versioning.
|
|||||||
* Fix a warning about possible loss of data.
|
* Fix a warning about possible loss of data.
|
||||||
* Fix version numbering in CMakeLists.txt
|
* Fix version numbering in CMakeLists.txt
|
||||||
* Remove unused declaration of the undefined `ParseResult::get_option`.
|
* Remove unused declaration of the undefined `ParseResult::get_option`.
|
||||||
|
* Throw on invalid option syntax when beginning with a `-`.
|
||||||
|
|
||||||
## 2.1.1
|
## 2.1.1
|
||||||
|
|
||||||
|
|||||||
@ -368,6 +368,15 @@ namespace cxxopts
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class option_syntax_exception : public OptionParseException {
|
||||||
|
public:
|
||||||
|
option_syntax_exception(const std::string& text)
|
||||||
|
: OptionParseException(u8"Argument " + LQUOTE + text + RQUOTE +
|
||||||
|
u8" starts with a - but has incorrect syntax")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class option_not_exists_exception : public OptionParseException
|
class option_not_exists_exception : public OptionParseException
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -1701,6 +1710,11 @@ ParseResult::parse(int& argc, char**& argv)
|
|||||||
{
|
{
|
||||||
//not a flag
|
//not a flag
|
||||||
|
|
||||||
|
// but if it starts with a `-`, then it's an error
|
||||||
|
if (argv[current][0] == '-') {
|
||||||
|
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
|
||||||
//ignored
|
//ignored
|
||||||
if (consume_positional(argv[current]))
|
if (consume_positional(argv[current]))
|
||||||
|
|||||||
@ -111,7 +111,7 @@ TEST_CASE("Short options", "[options]")
|
|||||||
CHECK(result.count("a") == 1);
|
CHECK(result.count("a") == 1);
|
||||||
CHECK(result["a"].as<std::string>() == "value");
|
CHECK(result["a"].as<std::string>() == "value");
|
||||||
|
|
||||||
REQUIRE_THROWS_AS(options.add_options()("", "nothing option"),
|
REQUIRE_THROWS_AS(options.add_options()("", "nothing option"),
|
||||||
cxxopts::invalid_option_format_error);
|
cxxopts::invalid_option_format_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -513,7 +513,7 @@ TEST_CASE("Unrecognised options", "[options]") {
|
|||||||
"--long",
|
"--long",
|
||||||
"-su",
|
"-su",
|
||||||
"--another_unknown",
|
"--another_unknown",
|
||||||
});
|
});
|
||||||
|
|
||||||
char** argv = av.argv();
|
char** argv = av.argv();
|
||||||
auto argc = av.argc();
|
auto argc = av.argc();
|
||||||
@ -529,3 +529,19 @@ TEST_CASE("Unrecognised options", "[options]") {
|
|||||||
CHECK_THAT(argv[1], Catch::Equals("--unknown"));
|
CHECK_THAT(argv[1], Catch::Equals("--unknown"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Invalid option syntax", "[options]") {
|
||||||
|
cxxopts::Options options("invalid_syntax", " - test invalid syntax");
|
||||||
|
|
||||||
|
Argv av({
|
||||||
|
"invalid_syntax",
|
||||||
|
"--a",
|
||||||
|
});
|
||||||
|
|
||||||
|
char** argv = av.argv();
|
||||||
|
auto argc = av.argc();
|
||||||
|
|
||||||
|
SECTION("Default behaviour") {
|
||||||
|
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user