Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve arguments() showing empty keys when exsiting short-only option #318

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions include/cxxopts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,13 @@ namespace cxxopts
return m_long;
}

CXXOPTS_NODISCARD
const std::string&
essential_name() const
{
return m_long.empty() ? m_short : m_long;
}

size_t
hash() const
{
Expand Down Expand Up @@ -2150,7 +2157,7 @@ OptionParser::parse_default(const std::shared_ptr<OptionDetails>& details)
// TODO: remove the duplicate code here
auto& store = m_parsed[details->hash()];
store.parse_default(details);
m_defaults.emplace_back(details->long_name(), details->value().get_default_value());
m_defaults.emplace_back(details->essential_name(), details->value().get_default_value());
}

inline
Expand All @@ -2174,7 +2181,7 @@ OptionParser::parse_option
auto& result = m_parsed[hash];
result.parse(value, arg);

m_sequential.emplace_back(value->long_name(), arg);
m_sequential.emplace_back(value->essential_name(), arg);
}

inline
Expand Down
56 changes: 56 additions & 0 deletions test/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ TEST_CASE("Short options", "[options]")
CHECK(result.count("a") == 1);
CHECK(result["a"].as<std::string>() == "value");

auto& arguments = result.arguments();
REQUIRE(arguments.size() == 1);
CHECK(arguments[0].key() == "a");
CHECK(arguments[0].value() == "value");

REQUIRE_THROWS_AS(options.add_options()("", "nothing option"),
cxxopts::invalid_option_format_error&);
}
Expand Down Expand Up @@ -830,3 +835,54 @@ TEST_CASE("Parameter follow option", "[parameter]") {
CHECK(job_values[2] == 10);
CHECK(job_values[3] == 5);
}

TEST_CASE("Iterator", "[iterator]") {
cxxopts::Options options("tester", " - test iterating over parse result");

options.add_options()
("long", "a long option")
("s,short", "a short option")
("a", "a short-only option")
("value", "an option with a value", cxxopts::value<std::string>())
("default", "an option with default value", cxxopts::value<int>()->default_value("42"))
("nothing", "won't exist", cxxopts::value<std::string>())
;

Argv argv({
"tester",
"--long",
"-s",
"-a",
"--value",
"value",
});

auto** actual_argv = argv.argv();
auto argc = argv.argc();

auto result = options.parse(argc, actual_argv);

auto iter = result.begin();

REQUIRE(iter != result.end());
CHECK(iter->key() == "long");
CHECK(iter->value() == "true");

REQUIRE(++iter != result.end());
CHECK(iter->key() == "short");
CHECK(iter->value() == "true");

REQUIRE(++iter != result.end());
CHECK(iter->key() == "a");
CHECK(iter->value() == "true");

REQUIRE(++iter != result.end());
CHECK(iter->key() == "value");
CHECK(iter->value() == "value");

REQUIRE(++iter != result.end());
CHECK(iter->key() == "default");
CHECK(iter->value() == "42");

REQUIRE(++iter == result.end());
}