Skip to content

Commit

Permalink
Allow to set a footer in the description
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdafu committed Nov 20, 2017
1 parent 1b02682 commit 50c5529
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
24 changes: 18 additions & 6 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class App {
/// Description of the current program/subcommand
std::string description_;

/// Footer to put after all options in the help output
std::string footer_;

/// If true, allow extra arguments (ie, don't throw an error).
bool allow_extras_{false};

Expand Down Expand Up @@ -147,6 +150,12 @@ class App {
/// Create a new program. Pass in the same arguments as main(), along with a help string.
App(std::string description_ = "", bool help = true) : App(description_, help, detail::dummy) {}

/// Set footer.
App *set_footer(std::string footer) {
footer_ = footer;
return this;
}

/// Set a callback for the end of parsing.
///
/// Due to a bug in c++11,
Expand Down Expand Up @@ -794,40 +803,43 @@ class App {
out << " [SUBCOMMAND]";
}

out << std::endl << std::endl;
out << std::endl;

// Positional descriptions
if(pos) {
out << "Positionals:" << std::endl;
out << std::endl << "Positionals:" << std::endl;
for(const Option_p &opt : options_) {
if(detail::to_lower(opt->get_group()) == "hidden")
continue;
if(opt->_has_help_positional())
detail::format_help(out, opt->help_pname(), opt->get_description(), wid);
}
out << std::endl;
}

// Options
if(npos) {
for(const std::string &group : groups) {
if(detail::to_lower(group) == "hidden")
continue;
out << group << ":" << std::endl;
out << std::endl << group << ":" << std::endl;
for(const Option_p &opt : options_) {
if(opt->nonpositional() && opt->get_group() == group)
detail::format_help(out, opt->help_name(), opt->get_description(), wid);
}
out << std::endl;
}
}

// Subcommands
if(!subcommands_.empty()) {
out << "Subcommands:" << std::endl;
out << std::endl << "Subcommands:" << std::endl;
for(const App_p &com : subcommands_)
detail::format_help(out, com->get_name(), com->description_, wid);
}

if (!footer_.empty()) {
out << std::endl << footer_ << std::endl;
}

return out.str();
}

Expand Down
13 changes: 13 additions & 0 deletions tests/HelpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ TEST(THelp, Basic) {
EXPECT_THAT(help, HasSubstr("Usage:"));
}

TEST(THelp, Footer) {
CLI::App app{"My prog"};
app.set_footer("Report bugs to bugs@example.com");

std::string help = app.help();

EXPECT_THAT(help, HasSubstr("My prog"));
EXPECT_THAT(help, HasSubstr("-h,--help"));
EXPECT_THAT(help, HasSubstr("Options:"));
EXPECT_THAT(help, HasSubstr("Usage:"));
EXPECT_THAT(help, HasSubstr("Report bugs to bugs@example.com"));
}

TEST(THelp, OptionalPositional) {
CLI::App app{"My prog"};

Expand Down

0 comments on commit 50c5529

Please sign in to comment.