mirror of
https://github.com/p-ranav/argparse
synced 2026-06-08 16:35:45 +00:00
ea1f7ef663
The help and version arguments are still included by default, but which default arguments to include can be overridden at ArgumentParser creation. argparse generally copies Python argparse behavior. This includes a default `--help`/`-h` argument to print a help message and exit. Some developers using argparse find the automatic exit to be undesirable. The Python argparse has an opt-out parameter when constructing an ArgumentParser. Using `add_help=False` avoids adding a default `--help` argument and allows the developer to implement a custom help. This commit adds a similar opt-out to our C++ argparse, but keeps the current behavior as the default. The `--help`/`-h` and `--version`/`-v` Arguments handle their own output and exit rather than specially treating them in ArgumentParser::parse_args_internal. Closes #119 Closes #138 Closes #139 Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
20 lines
746 B
C++
20 lines
746 B
C++
#include <argparse/argparse.hpp>
|
|
#include <doctest.hpp>
|
|
|
|
using doctest::test_suite;
|
|
|
|
TEST_CASE("Include all default arguments" * test_suite("default_args")) {
|
|
argparse::ArgumentParser parser("test");
|
|
auto help_msg { parser.help().str() };
|
|
REQUIRE(help_msg.find("shows help message") != std::string::npos);
|
|
REQUIRE(help_msg.find("prints version information") != std::string::npos);
|
|
}
|
|
|
|
TEST_CASE("Do not include default arguments" * test_suite("default_args")) {
|
|
argparse::ArgumentParser parser("test", "1.0",
|
|
argparse::default_arguments::none);
|
|
parser.parse_args({"test"});
|
|
REQUIRE_THROWS_AS(parser.get("--help"), std::logic_error);
|
|
REQUIRE_THROWS_AS(parser.get("--version"), std::logic_error);
|
|
}
|