diff options
author | Baojun Wang <wangbj@gmail.com> | 2019-09-09 19:55:57 +0300 |
---|---|---|
committer | Jarryd Beck <jarro.2783@gmail.com> | 2021-10-09 09:02:27 +0300 |
commit | 4b7fccb5f2bca82041f6d71026c3133a873c6de5 (patch) | |
tree | df755826e2dd625c4b312db7208db4fcb666443b | |
parent | 1ff0da64019b93ffd8b7b63072ad8f33463f79c4 (diff) |
Allow empty string to be valid positional arguments
Fixes #204.
-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 af87540..24c5a8e 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -974,6 +974,12 @@ namespace cxxopts void parse_value(const std::string& text, std::vector<T>& value) { + if (text.empty()) { + T v; + parse_value(text, v); + value.emplace_back(std::move(v)); + return; + } std::stringstream in(text); std::string token; while(!in.eof() && std::getline(in, token, CXXOPTS_VECTOR_DELIMITER)) { diff --git a/test/options.cpp b/test/options.cpp index 75fea46..7c6d5f7 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -231,6 +231,31 @@ TEST_CASE("Positional not valid", "[positional]") { CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&); } +TEST_CASE("Positional with empty arguments", "[positional]") { + cxxopts::Options options("positional_with_empty_arguments", "positional with empty argument"); + options.add_options() + ("long", "a long option", cxxopts::value<std::string>()) + ("program", "program to run", cxxopts::value<std::string>()) + ("programArgs", "program arguments", cxxopts::value<std::vector<std::string>>()) + ; + + options.parse_positional("program", "programArgs"); + + Argv av({"foobar", "--long", "long_value", "--", "someProgram", "ab", "-c", "d", "--ef", "gh", "--ijk=lm", "n", "", "o", }); + std::vector<std::string> expected({"ab", "-c", "d", "--ef", "gh", "--ijk=lm", "n", "", "o", }); + + char** argv = av.argv(); + auto argc = av.argc(); + + auto result = options.parse(argc, argv); + auto actual = result["programArgs"].as<std::vector<std::string>>(); + + REQUIRE(result.count("program") == 1); + REQUIRE(result["program"].as<std::string>() == "someProgram"); + REQUIRE(result.count("programArgs") == expected.size()); + REQUIRE(actual == expected); +} + TEST_CASE("Empty with implicit value", "[implicit]") { cxxopts::Options options("empty_implicit", "doesn't handle empty"); |