Skip to content

Commit

Permalink
Support groups for subcommands.
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdafu committed Nov 20, 2017
1 parent 79cd791 commit db20f91
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
27 changes: 24 additions & 3 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class App {
/// -1 for 1 or more, 0 for not required, # for exact number required
int require_subcommand_ = 0;

/// The group membership
std::string group_{"Subcommands"};

///@}
/// @name Config
///@{
Expand Down Expand Up @@ -212,6 +215,15 @@ class App {
return this;
}

/// Changes the group membership
App *group(std::string name) {
group_ = name;
return this;
}

/// Get the group of this subcommand
const std::string &get_group() const { return group_; }

///@}
/// @name Adding options
///@{
Expand Down Expand Up @@ -831,9 +843,18 @@ class App {

// Subcommands
if(!subcommands_.empty()) {
out << std::endl << "Subcommands:" << std::endl;
for(const App_p &com : subcommands_)
detail::format_help(out, com->get_name(), com->description_, wid);
std::set<std::string> subcmd_groups_seen;
for(const App_p &com : subcommands_) {
const std::string &group_key = detail::to_lower(com->get_group());
if(group_key == "hidden" || subcmd_groups_seen.count(group_key) != 0)
continue;

subcmd_groups_seen.insert(group_key);
out << std::endl << com->get_group() << ":" << std::endl;
for(const App_p &com : subcommands_)
if(detail::to_lower(com->get_group()) == group_key)
detail::format_help(out, com->get_name(), com->description_, wid);
}
}

if(!footer_.empty()) {
Expand Down
24 changes: 24 additions & 0 deletions tests/SubcommandTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "app_helper.hpp"

#include "gtest/gtest.h"
#include "gmock/gmock.h"

using ::testing::HasSubstr;
using ::testing::Not;

TEST_F(TApp, BasicSubcommands) {
auto sub1 = app.add_subcommand("sub1");
auto sub2 = app.add_subcommand("sub2");
Expand Down Expand Up @@ -482,3 +488,21 @@ TEST_F(SubcommandProgram, Callbacks) {

EXPECT_THROW(run(), CLI::Success);
}

TEST_F(SubcommandProgram, Groups) {

std::string help = app.help();
EXPECT_THAT(help, Not(HasSubstr("More Commands:")));
EXPECT_THAT(help, HasSubstr("Subcommands:"));

start->group("More Commands");
help = app.help();
EXPECT_THAT(help, HasSubstr("More Commands:"));
EXPECT_THAT(help, HasSubstr("Subcommands:"));

// Case is ignored but for the first subcommand in a group.
stop->group("more commands");
help = app.help();
EXPECT_THAT(help, HasSubstr("More Commands:"));
EXPECT_THAT(help, Not(HasSubstr("Subcommands:")));
}

0 comments on commit db20f91

Please sign in to comment.