Fix duplicate default option
Fixes #197. Don't parse default options twice when there is a short and long option.
This commit is contained in:
parent
6e31c227e2
commit
fce82fb035
@ -9,6 +9,7 @@ options. The project adheres to semantic versioning.
|
||||
|
||||
* Only search for a C++ compiler in CMakeLists.txt.
|
||||
* Allow for exceptions to be disabled.
|
||||
* Fix duplicate default options when there is a short and long option.
|
||||
|
||||
## 2.2
|
||||
|
||||
|
@ -1086,15 +1086,23 @@ namespace cxxopts
|
||||
parse_default(std::shared_ptr<const OptionDetails> details)
|
||||
{
|
||||
ensure_value(details);
|
||||
m_default = true;
|
||||
m_value->parse();
|
||||
}
|
||||
|
||||
size_t
|
||||
count() const
|
||||
count() const noexcept
|
||||
{
|
||||
return m_count;
|
||||
}
|
||||
|
||||
// TODO: maybe default options should count towards the number of arguments
|
||||
bool
|
||||
has_default() const noexcept
|
||||
{
|
||||
return m_default;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T&
|
||||
as() const
|
||||
@ -1126,6 +1134,7 @@ namespace cxxopts
|
||||
|
||||
std::shared_ptr<Value> m_value;
|
||||
size_t m_count = 0;
|
||||
bool m_default = false;
|
||||
};
|
||||
|
||||
class KeyValue
|
||||
@ -1989,7 +1998,7 @@ ParseResult::parse(int& argc, char**& argv)
|
||||
|
||||
auto& store = m_results[detail];
|
||||
|
||||
if(!store.count() && value.has_default()){
|
||||
if(value.has_default() && !store.count() && !store.has_default()){
|
||||
parse_default(detail);
|
||||
}
|
||||
}
|
||||
|
@ -315,8 +315,10 @@ TEST_CASE("Default values", "[default]")
|
||||
{
|
||||
cxxopts::Options options("defaults", "has defaults");
|
||||
options.add_options()
|
||||
("default", "Has implicit", cxxopts::value<int>()
|
||||
->default_value("42"));
|
||||
("default", "Has implicit", cxxopts::value<int>()->default_value("42"))
|
||||
("v,vector", "Default vector", cxxopts::value<std::vector<int>>()
|
||||
->default_value("1,4"))
|
||||
;
|
||||
|
||||
SECTION("Sets defaults") {
|
||||
Argv av({"implicit"});
|
||||
@ -327,6 +329,11 @@ TEST_CASE("Default values", "[default]")
|
||||
auto result = options.parse(argc, argv);
|
||||
CHECK(result.count("default") == 0);
|
||||
CHECK(result["default"].as<int>() == 42);
|
||||
|
||||
auto& v = result["vector"].as<std::vector<int>>();
|
||||
REQUIRE(v.size() == 2);
|
||||
CHECK(v[0] == 1);
|
||||
CHECK(v[1] == 4);
|
||||
}
|
||||
|
||||
SECTION("When values provided") {
|
||||
|
Loading…
Reference in New Issue
Block a user