Skip to content

Commit f53b5a6

Browse files
committed
avoid to setup default for env values
1 parent 97c00fb commit f53b5a6

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

include/cxxopts.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class parse_result {
783783
name_hash_map keys_{};
784784
parsed_hash_map values_{};
785785
std::vector<key_value> sequential_{};
786-
/// List of arguments that did not match any defined option.
786+
/// List of arguments that did not match to any defined option.
787787
std::vector<std::string> unmatched_{};
788788
/// Number of consument command line arguments.
789789
size_t consumed_arguments_{0};

src/cxxopts.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ parse_result::count(const std::string& o) const {
695695
}
696696

697697
bool
698-
parse_result::has(const std::string& o) const {
699-
return count(o) != 0;
698+
parse_result::has(const std::string& opt) const {
699+
return count(opt) != 0;
700700
}
701701

702702
const option_value&
@@ -859,25 +859,29 @@ class options::option_parser {
859859
++current;
860860
}
861861

862+
// Setup default or env values.
862863
for (auto& opt : options_) {
863864
auto& detail = opt.second;
864-
const auto& value = detail->value();
865-
866865
auto& store = parsed_[detail->hash()];
866+
const auto& value = detail->value();
867867

868-
if (value->has_default()) {
869-
if (!store.count() && !store.has_default()) {
870-
parse_default(detail);
871-
}
872-
} else {
873-
parse_no_value(detail);
868+
// Skip options with parsed values.
869+
if (store.count() || store.has_default()) {
870+
continue;
874871
}
875-
876-
if (value->has_env() && !store.count()){
872+
// Try to setup env value.
873+
if (value->has_env()) {
877874
if (const char* env = std::getenv(value->get_env_var().c_str())) {
878875
store.parse(detail, std::string(env));
876+
continue;
879877
}
880878
}
879+
// Try to setup default value.
880+
if (value->has_default()) {
881+
store.parse_default(detail);
882+
} else {
883+
store.parse_no_value(detail);
884+
}
881885
}
882886

883887
parse_result::name_hash_map keys;

0 commit comments

Comments
 (0)