Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,18 @@ class Argument {
auto &store_into(bool &var) {
if ((!m_default_value.has_value()) && (!m_implicit_value.has_value())) {
flag();
} else {
if (!m_implicit_value.has_value()) {
m_implicit_value = !std::any_cast<bool>(m_default_value);
}
}
if (m_default_value.has_value()) {
var = std::any_cast<bool>(m_default_value);
}
action([&var](const auto & /*unused*/) {
var = true;

// If implicit value is defined after store_into, the wrong value might be set here.
// This is alright as long as it is described well in the manual.
action([&var, val = std::any_cast<bool>(m_implicit_value)](const auto & /*unused*/) { var = val;
return var;
});
return *this;
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
test_parse_known_args.cpp
test_equals_form.cpp
test_prefix_chars.cpp
test_issue_413.cpp
)
set_source_files_properties(main.cpp
PROPERTIES
Expand Down
36 changes: 36 additions & 0 deletions test/test_issue_413.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifdef WITH_MODULE
import argparse;
#else
#include <argparse/argparse.hpp>
#endif
#include <doctest.hpp>

using doctest::test_suite;

TEST_CASE("Regression test for issue 413" * test_suite("implicit_values")) {
argparse::ArgumentParser program;
bool nicearg = false;

program.add_argument("--deactivate")
.help("Disable BFS-based detector ordering and use geometric orientation")
.default_value(true)
.implicit_value(false)
.store_into(nicearg);

program.parse_args({"test"});
REQUIRE(nicearg == true);
}

TEST_CASE("Regression test for issue 413 [2]" * test_suite("implicit_values")) {
argparse::ArgumentParser program;
bool nicearg = true;

program.add_argument("--deactivate")
.help("Disable BFS-based detector ordering and use geometric orientation")
.default_value(true)
.implicit_value(false)
.store_into(nicearg);

program.parse_args({"test", "--deactivate"});
REQUIRE(nicearg == false);
}