Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_subcommand_used overload #233

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,13 +1255,18 @@ class ArgumentParser {
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
Expand Down
37 changes: 37 additions & 0 deletions test/test_subparsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>("--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);
}
}