Skip to content

Commit

Permalink
Merge pull request #233 from SergiusTheBest/is-subcommand-used-overload
Browse files Browse the repository at this point in the history
Add is_subcommand_used overload
  • Loading branch information
p-ranav authored Nov 8, 2022
2 parents 6960571 + 8e6a36d commit 2ed761a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
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);
}
}

0 comments on commit 2ed761a

Please sign in to comment.