diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index d0ce31da..49e15c87 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -408,6 +408,10 @@ class Argument { return *this; } + Argument &default_value(const char *value) { + return default_value(std::string(value)); + } + Argument &required() { m_is_required = true; return *this; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 84599ad1..a903f09f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -33,6 +33,7 @@ file(GLOB ARGPARSE_TEST_SOURCES test_container_arguments.cpp test_const_correct.cpp test_default_args.cpp + test_default_value.cpp test_get.cpp test_help.cpp test_invalid_arguments.cpp diff --git a/test/test_default_value.cpp b/test/test_default_value.cpp new file mode 100644 index 00000000..15271f96 --- /dev/null +++ b/test/test_default_value.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +using doctest::test_suite; + +TEST_CASE("Use a 'string' default value" * test_suite("default_value")) { + argparse::ArgumentParser program("test"); + + SUBCASE("Use a const char[] default value") { + program.add_argument("--arg").default_value("array of char"); + REQUIRE_NOTHROW(program.parse_args({"test"})); + REQUIRE(program.get("--arg") == std::string("array of char")); + } + + SUBCASE("Use a std::string default value") { + program.add_argument("--arg").default_value(std::string("string object")); + REQUIRE_NOTHROW(program.parse_args({"test"})); + REQUIRE(program.get("--arg") == std::string("string object")); + } +}