argparse/test/test_is_used.cpp
Sean Robinson 3efd045ea9 Add ArgumentParser.is_used to discern user-supplied values from defaults
.present returns std::nullopt if the optional argument is not given by the
user -- as long as a .default_value is not defined.  With a .default_value,
.present cannot be used to determine if a value is user-provided or the
default.

.is_used fills that role and only returns true if the argument was passed
by the user.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-04-07 14:09:10 -07:00

23 lines
717 B
C++

#include <doctest.hpp>
#include <argparse/argparse.hpp>
using doctest::test_suite;
TEST_CASE("User-supplied argument" * test_suite("is_used")) {
argparse::ArgumentParser program("test");
program.add_argument("--dir")
.default_value(std::string("/"));
program.parse_args({ "test", "--dir", "/home/user" });
REQUIRE(program.get("--dir") == "/home/user");
REQUIRE(program.is_used("--dir") == true);
}
TEST_CASE("Not user-supplied argument" * test_suite("is_used")) {
argparse::ArgumentParser program("test");
program.add_argument("--dir")
.default_value(std::string("/"));
program.parse_args({ "test" });
REQUIRE(program.get("--dir") == "/");
REQUIRE(program.is_used("--dir") == false);
}