Fix option matching

This commit is contained in:
Jarryd Beck 2018-11-07 18:50:43 +11:00
parent cd65c459ec
commit b528324107
3 changed files with 33 additions and 2 deletions

View File

@ -19,6 +19,7 @@ options. The project adheres to semantic versioning.
* Fix a warning about possible loss of data.
* Fix version numbering in CMakeLists.txt
* Remove unused declaration of the undefined `ParseResult::get_option`.
* Throw on invalid option syntax when beginning with a `-`.
## 2.1.1

View File

@ -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
{
public:
@ -1701,6 +1710,11 @@ ParseResult::parse(int& argc, char**& argv)
{
//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
//ignored
if (consume_positional(argv[current]))

View File

@ -529,3 +529,19 @@ TEST_CASE("Unrecognised options", "[options]") {
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);
}
}