Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with char[] default values #253

Merged
merged 1 commit into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}
}