mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Add is_subcommand_used overload
It's useful for removing string literals duplication in a code.
This commit is contained in:
parent
6960571156
commit
8e6a36dd0d
@ -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.
|
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
|
### Parse Known Args
|
||||||
|
|
||||||
|
@ -1255,13 +1255,18 @@ public:
|
|||||||
return (*this)[arg_name].m_is_used;
|
return (*this)[arg_name].m_is_used;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Getter that returns true for user-supplied options. Returns false if not
|
/* Getter that returns true if a subcommand is used.
|
||||||
* user-supplied, even with a default value.
|
|
||||||
*/
|
*/
|
||||||
auto is_subcommand_used(std::string_view subcommand_name) const {
|
auto is_subcommand_used(std::string_view subcommand_name) const {
|
||||||
return m_subparser_used.at(subcommand_name);
|
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
|
/* Indexing operator. Return a reference to an Argument object
|
||||||
* Used in conjuction with Argument.operator== e.g., parser["foo"] == true
|
* Used in conjuction with Argument.operator== e.g., parser["foo"] == true
|
||||||
* @throws std::logic_error in case of an invalid argument name
|
* @throws std::logic_error in case of an invalid argument name
|
||||||
|
@ -199,4 +199,41 @@ TEST_CASE("Parse git commands" * test_suite("subparsers")) {
|
|||||||
REQUIRE(submodule_update_command.get<bool>("--init") == true);
|
REQUIRE(submodule_update_command.get<bool>("--init") == true);
|
||||||
REQUIRE(submodule_update_command.get<bool>("--recursive") == true);
|
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