diff options
author | RonxBulld <526677628@qq.com> | 2021-05-06 01:46:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-06 01:46:40 +0300 |
commit | 97a4d5511ffefc81a97abd3d67b37f66e57655ee (patch) | |
tree | 8191d5b7b9b00feac49ba07efa7f69fcca20aec8 | |
parent | 056a6281ace938b50948648bb4cdb8b52a659ec2 (diff) |
Support options like `-j5`. (#286)
* Support option value being attached after the option without a space in between. e.g. -j5
-rw-r--r-- | include/cxxopts.hpp | 6 | ||||
-rw-r--r-- | test/options.cpp | 25 |
2 files changed, 31 insertions, 0 deletions
diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 80089be..1862c8b 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -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 diff --git a/test/options.cpp b/test/options.cpp index 8bc6f49..75fea46 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -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); +} |