Allow empty string to be valid positional arguments

Fixes #204.
This commit is contained in:
Baojun Wang 2019-09-09 12:55:57 -04:00 committed by Jarryd Beck
parent 1ff0da6401
commit 4b7fccb5f2
2 changed files with 31 additions and 0 deletions

View File

@ -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)) {

View File

@ -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");