Skip to content

Commit

Permalink
Merge pull request #253 from skrobinson/fix-string-crash
Browse files Browse the repository at this point in the history
Fix crash with char[] default values
  • Loading branch information
p-ranav authored Feb 19, 2023
2 parents 15d745f + cb3da17 commit e077137
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/test_default_value.cpp
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit e077137

Please sign in to comment.