mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Merge pull request #233 from SergiusTheBest/is-subcommand-used-overload
Add is_subcommand_used overload
This commit is contained in:
commit
2ed761a201
@ -825,7 +825,7 @@ Subcommands:
|
||||
|
||||
When a help message is requested from a subparser, only the help for that particular parser will be printed. The help message will not include parent parser or sibling parser messages.
|
||||
|
||||
Additionally, every parser has a `.is_subcommand_used("<command_name>")` member function to check if a subcommand was used.
|
||||
Additionally, every parser has the `.is_subcommand_used("<command_name>")` and `.is_subcommand_used(subparser)` member functions to check if a subcommand was used.
|
||||
|
||||
### Parse Known Args
|
||||
|
||||
|
@ -1255,13 +1255,18 @@ public:
|
||||
return (*this)[arg_name].m_is_used;
|
||||
}
|
||||
|
||||
/* Getter that returns true for user-supplied options. Returns false if not
|
||||
* user-supplied, even with a default value.
|
||||
/* Getter that returns true if a subcommand is used.
|
||||
*/
|
||||
auto is_subcommand_used(std::string_view subcommand_name) const {
|
||||
return m_subparser_used.at(subcommand_name);
|
||||
}
|
||||
|
||||
/* Getter that returns true if a subcommand is used.
|
||||
*/
|
||||
auto is_subcommand_used(const ArgumentParser &subparser) const {
|
||||
return is_subcommand_used(subparser.m_program_name);
|
||||
}
|
||||
|
||||
/* Indexing operator. Return a reference to an Argument object
|
||||
* Used in conjuction with Argument.operator== e.g., parser["foo"] == true
|
||||
* @throws std::logic_error in case of an invalid argument name
|
||||
|
@ -200,3 +200,40 @@ TEST_CASE("Parse git commands" * test_suite("subparsers")) {
|
||||
REQUIRE(submodule_update_command.get<bool>("--recursive") == true);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Check is_subcommand_used after parse" * test_suite("subparsers")) {
|
||||
argparse::ArgumentParser command_1("add");
|
||||
|
||||
argparse::ArgumentParser command_2("clean");
|
||||
command_2.add_argument("--fullclean")
|
||||
.default_value(false)
|
||||
.implicit_value(true);
|
||||
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_subparser(command_1);
|
||||
program.add_subparser(command_2);
|
||||
|
||||
SUBCASE("command 1") {
|
||||
program.parse_args({"test", "add"});
|
||||
REQUIRE(program.is_subcommand_used("add") == true);
|
||||
REQUIRE(program.is_subcommand_used(command_1) == true);
|
||||
REQUIRE(program.is_subcommand_used("clean") == false);
|
||||
REQUIRE(program.is_subcommand_used(command_2) == false);
|
||||
}
|
||||
|
||||
SUBCASE("command 2") {
|
||||
program.parse_args({"test", "clean", "--fullclean"});
|
||||
REQUIRE(program.is_subcommand_used("add") == false);
|
||||
REQUIRE(program.is_subcommand_used(command_1) == false);
|
||||
REQUIRE(program.is_subcommand_used("clean") == true);
|
||||
REQUIRE(program.is_subcommand_used(command_2) == true);
|
||||
}
|
||||
|
||||
SUBCASE("none") {
|
||||
program.parse_args({"test"});
|
||||
REQUIRE(program.is_subcommand_used("add") == false);
|
||||
REQUIRE(program.is_subcommand_used(command_1) == false);
|
||||
REQUIRE(program.is_subcommand_used("clean") == false);
|
||||
REQUIRE(program.is_subcommand_used(command_2) == false);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user