1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Allow to set a footer in the description

This commit is contained in:
Marcus Brinkmann 2017-11-20 01:40:48 +01:00 committed by Henry Schreiner
parent 1b02682223
commit ce007eb5a2
2 changed files with 31 additions and 6 deletions

View File

@ -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};
@ -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,
@ -794,18 +803,17 @@ 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
@ -813,21 +821,25 @@ class App {
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();
}

View File

@ -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"};