mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 07:04:39 +00:00
Merge pull request #253 from skrobinson/fix-string-crash
Fix crash with char[] default values
This commit is contained in:
commit
e077137cd2
@ -408,6 +408,10 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
Argument &default_value(const char *value) {
|
||||
return default_value(std::string(value));
|
||||
}
|
||||
|
||||
Argument &required() {
|
||||
m_is_required = true;
|
||||
return *this;
|
||||
|
@ -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
|
||||
|
21
test/test_default_value.cpp
Normal file
21
test/test_default_value.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <doctest.hpp>
|
||||
#include <string>
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user