Support options like -j5. (#286)

* Support option value being attached after the option without a space in between. e.g. -j5
This commit is contained in:
RonxBulld 2021-05-06 06:46:40 +08:00 committed by GitHub
parent 056a6281ac
commit 97a4d5511f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -2295,6 +2295,12 @@ OptionParser::parse(int argc, const char* const* argv)
{
parse_option(value, name, value->value().get_implicit_value());
}
else if (i + 1 < s.size())
{
std::string arg_value = s.substr(i + 1);
parse_option(value, name, arg_value);
break;
}
else
{
//error

View File

@ -779,3 +779,28 @@ TEST_CASE("Const array", "[const]") {
cxxopts::Options options("Empty options", " - test constness");
auto result = options.parse(2, option_list);
}
TEST_CASE("Parameter follow option", "[parameter]") {
cxxopts::Options options("param_follow_opt", " - test parameter follow option without space.");
options.add_options()
("j,job", "Job", cxxopts::value<std::vector<unsigned>>());
Argv av({"implicit",
"-j", "9",
"--job", "7",
"--job=10",
"-j5",
});
auto ** argv = av.argv();
auto argc = av.argc();
auto result = options.parse(argc, argv);
REQUIRE(result.count("job") == 4);
auto job_values = result["job"].as<std::vector<unsigned>>();
CHECK(job_values[0] == 9);
CHECK(job_values[1] == 7);
CHECK(job_values[2] == 10);
CHECK(job_values[3] == 5);
}