Commit 97a4d5511ffefc81a97abd3d67b37f66e57655ee

Authored by RonxBulld
Committed by GitHub
1 parent 056a6281

Support options like `-j5`. (#286)

* Support option value being attached after the option without a space in between. e.g. -j5
include/cxxopts.hpp
... ... @@ -2295,6 +2295,12 @@ OptionParser::parse(int argc, const char* const* argv)
2295 2295 {
2296 2296 parse_option(value, name, value->value().get_implicit_value());
2297 2297 }
  2298 + else if (i + 1 < s.size())
  2299 + {
  2300 + std::string arg_value = s.substr(i + 1);
  2301 + parse_option(value, name, arg_value);
  2302 + break;
  2303 + }
2298 2304 else
2299 2305 {
2300 2306 //error
... ...
test/options.cpp
... ... @@ -779,3 +779,28 @@ TEST_CASE(&quot;Const array&quot;, &quot;[const]&quot;) {
779 779 cxxopts::Options options("Empty options", " - test constness");
780 780 auto result = options.parse(2, option_list);
781 781 }
  782 +
  783 +TEST_CASE("Parameter follow option", "[parameter]") {
  784 + cxxopts::Options options("param_follow_opt", " - test parameter follow option without space.");
  785 + options.add_options()
  786 + ("j,job", "Job", cxxopts::value<std::vector<unsigned>>());
  787 + Argv av({"implicit",
  788 + "-j", "9",
  789 + "--job", "7",
  790 + "--job=10",
  791 + "-j5",
  792 + });
  793 +
  794 + auto ** argv = av.argv();
  795 + auto argc = av.argc();
  796 +
  797 + auto result = options.parse(argc, argv);
  798 +
  799 + REQUIRE(result.count("job") == 4);
  800 +
  801 + auto job_values = result["job"].as<std::vector<unsigned>>();
  802 + CHECK(job_values[0] == 9);
  803 + CHECK(job_values[1] == 7);
  804 + CHECK(job_values[2] == 10);
  805 + CHECK(job_values[3] == 5);
  806 +}
... ...