-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow removal of default arguments (i.e. --help and --version)
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 <[email protected]>
- Loading branch information
1 parent
2b05334
commit ea1f7ef
Showing
6 changed files
with
124 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters