mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 15:14:39 +00:00
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>
40 lines
1.2 KiB
C++
40 lines
1.2 KiB
C++
#include <doctest.hpp>
|
|
#include <argparse/argparse.hpp>
|
|
#include <sstream>
|
|
|
|
using doctest::test_suite;
|
|
|
|
TEST_CASE("Users can print version and exit" * test_suite("version")
|
|
* doctest::skip()) {
|
|
argparse::ArgumentParser program("cli-test", "1.9.0");
|
|
program.add_argument("-d", "--dir")
|
|
.required();
|
|
program.parse_args( { "test", "--version" });
|
|
REQUIRE(program.get("--version") == "1.9.0");
|
|
}
|
|
|
|
TEST_CASE("Users can disable default -v/--version" * test_suite("version")) {
|
|
argparse::ArgumentParser program("test", "1.0",
|
|
argparse::default_arguments::help);
|
|
REQUIRE_THROWS_AS(program.parse_args({"test", "--version"}),
|
|
std::runtime_error);
|
|
}
|
|
|
|
TEST_CASE("Users can replace default -v/--version" * test_suite("version")) {
|
|
std::string version { "3.1415" };
|
|
argparse::ArgumentParser program("test", version,
|
|
argparse::default_arguments::help);
|
|
std::stringstream buffer;
|
|
program.add_argument("-v", "--version")
|
|
.action([&](const auto &) {
|
|
buffer << version;
|
|
})
|
|
.default_value(true)
|
|
.implicit_value(false)
|
|
.nargs(0);
|
|
|
|
REQUIRE(buffer.str().empty());
|
|
program.parse_args({"test", "--version"});
|
|
REQUIRE_FALSE(buffer.str().empty());
|
|
}
|