From d28188f4d542b07de9bad3e89aa53d8bf1a53a09 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Fri, 27 Oct 2023 12:34:57 -0500 Subject: [PATCH] Closes #247 --- include/argparse/argparse.hpp | 1 + test/test_default_value.cpp | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index 9c71697..52e8aeb 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -427,6 +427,7 @@ public: } template Argument &default_value(T &&value) { + m_num_args_range = NArgsRange{0, m_num_args_range.get_max()}; m_default_value_repr = details::repr(value); if constexpr (std::is_convertible_v) { diff --git a/test/test_default_value.cpp b/test/test_default_value.cpp index 1a28168..4805f5c 100644 --- a/test/test_default_value.cpp +++ b/test/test_default_value.cpp @@ -23,3 +23,58 @@ TEST_CASE("Use a 'string' default value" * test_suite("default_value")) { REQUIRE(program.get("--arg") == std::string("string object")); } } + +TEST_CASE("Use a default value with flag arguments" * test_suite("default_value")) { + + argparse::ArgumentParser program("test"); + + program.add_argument("-inc_chr", "--include_chromes") + .help(std::string{"only process the anchor whose one of the end is contained on the specified " + "chromatin, used ',' to split."}) + .default_value("all"); + + program.add_argument("-l").default_value(false).implicit_value(true); + program.add_argument("-o").default_value(false).implicit_value(true); + + program.add_argument("filename"); + + SUBCASE("Leading optional argument with default_value") { + REQUIRE_NOTHROW(program.parse_args({"test", "-inc_chr", "-lo", "my.log"})); + REQUIRE(program.get("-inc_chr") == std::string{"all"}); + } + + SUBCASE("Trailing optional argument with default_value") { + REQUIRE_NOTHROW(program.parse_args({"test", "-lo", "my.log", "-inc_chr"})); + REQUIRE(program.get("-inc_chr") == std::string{"all"}); + } +} + +TEST_CASE("Position of the argument with default value") { + argparse::ArgumentParser program("test"); + program.add_argument("-g").default_value("the_default_value"); + program.add_argument("-s"); + + SUBCASE("Arg with default value not passed") { + REQUIRE_NOTHROW(program.parse_args({"test", "-s", "./src"})); + REQUIRE(program.get("-g") == std::string("the_default_value")); + REQUIRE(program.get("-s") == std::string("./src")); + } + + SUBCASE("Arg with default value passed last") { + REQUIRE_NOTHROW(program.parse_args({"test", "-s", "./src", "-g"})); + REQUIRE(program.get("-g") == std::string("the_default_value")); + REQUIRE(program.get("-s") == std::string("./src")); + } + + SUBCASE("Arg with default value passed before last") { + REQUIRE_NOTHROW(program.parse_args({"test", "-g", "-s", "./src"})); + REQUIRE(program.get("-g") == std::string("the_default_value")); + REQUIRE(program.get("-s") == std::string("./src")); + } + + SUBCASE("Arg with default value replaces the value if given") { + REQUIRE_NOTHROW(program.parse_args({"test", "-g", "a_different_value", "-s", "./src"})); + REQUIRE(program.get("-g") == std::string("a_different_value")); + REQUIRE(program.get("-s") == std::string("./src")); + } +} \ No newline at end of file