diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index 06d30fd4..9864c2c2 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -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(m_default_value); + } } if (m_default_value.has_value()) { var = std::any_cast(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(m_implicit_value)](const auto & /*unused*/) { var = val; return var; }); return *this; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2d36ec01..1866a6cc 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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 diff --git a/test/test_issue_413.cpp b/test/test_issue_413.cpp new file mode 100644 index 00000000..4da5bf8d --- /dev/null +++ b/test/test_issue_413.cpp @@ -0,0 +1,36 @@ +#ifdef WITH_MODULE +import argparse; +#else +#include +#endif +#include + +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); +}