From 50c5529c6e41f0e3a0f3ca199b6da7a541d4124a Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Mon, 20 Nov 2017 01:40:48 +0100 Subject: [PATCH] Allow to set a footer in the description --- include/CLI/App.hpp | 24 ++++++++++++++++++------ tests/HelpTest.cpp | 13 +++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 1cce85340..b79705007 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -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(); } diff --git a/tests/HelpTest.cpp b/tests/HelpTest.cpp index a1ad292d3..73587efa1 100644 --- a/tests/HelpTest.cpp +++ b/tests/HelpTest.cpp @@ -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"};