mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 20:23:55 +00:00
Allow to set a footer in the description
This commit is contained in:
parent
1b02682223
commit
ce007eb5a2
@ -63,6 +63,9 @@ class App {
|
|||||||
/// Description of the current program/subcommand
|
/// Description of the current program/subcommand
|
||||||
std::string description_;
|
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).
|
/// If true, allow extra arguments (ie, don't throw an error).
|
||||||
bool allow_extras_{false};
|
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.
|
/// 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) {}
|
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.
|
/// Set a callback for the end of parsing.
|
||||||
///
|
///
|
||||||
/// Due to a bug in c++11,
|
/// Due to a bug in c++11,
|
||||||
@ -794,18 +803,17 @@ class App {
|
|||||||
out << " [SUBCOMMAND]";
|
out << " [SUBCOMMAND]";
|
||||||
}
|
}
|
||||||
|
|
||||||
out << std::endl << std::endl;
|
out << std::endl;
|
||||||
|
|
||||||
// Positional descriptions
|
// Positional descriptions
|
||||||
if(pos) {
|
if(pos) {
|
||||||
out << "Positionals:" << std::endl;
|
out << std::endl << "Positionals:" << std::endl;
|
||||||
for(const Option_p &opt : options_) {
|
for(const Option_p &opt : options_) {
|
||||||
if(detail::to_lower(opt->get_group()) == "hidden")
|
if(detail::to_lower(opt->get_group()) == "hidden")
|
||||||
continue;
|
continue;
|
||||||
if(opt->_has_help_positional())
|
if(opt->_has_help_positional())
|
||||||
detail::format_help(out, opt->help_pname(), opt->get_description(), wid);
|
detail::format_help(out, opt->help_pname(), opt->get_description(), wid);
|
||||||
}
|
}
|
||||||
out << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options
|
// Options
|
||||||
@ -813,21 +821,25 @@ class App {
|
|||||||
for(const std::string &group : groups) {
|
for(const std::string &group : groups) {
|
||||||
if(detail::to_lower(group) == "hidden")
|
if(detail::to_lower(group) == "hidden")
|
||||||
continue;
|
continue;
|
||||||
out << group << ":" << std::endl;
|
out << std::endl << group << ":" << std::endl;
|
||||||
for(const Option_p &opt : options_) {
|
for(const Option_p &opt : options_) {
|
||||||
if(opt->nonpositional() && opt->get_group() == group)
|
if(opt->nonpositional() && opt->get_group() == group)
|
||||||
detail::format_help(out, opt->help_name(), opt->get_description(), wid);
|
detail::format_help(out, opt->help_name(), opt->get_description(), wid);
|
||||||
}
|
}
|
||||||
out << std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subcommands
|
// Subcommands
|
||||||
if(!subcommands_.empty()) {
|
if(!subcommands_.empty()) {
|
||||||
out << "Subcommands:" << std::endl;
|
out << std::endl << "Subcommands:" << std::endl;
|
||||||
for(const App_p &com : subcommands_)
|
for(const App_p &com : subcommands_)
|
||||||
detail::format_help(out, com->get_name(), com->description_, wid);
|
detail::format_help(out, com->get_name(), com->description_, wid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!footer_.empty()) {
|
||||||
|
out << std::endl << footer_ << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return out.str();
|
return out.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,19 @@ TEST(THelp, Basic) {
|
|||||||
EXPECT_THAT(help, HasSubstr("Usage:"));
|
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) {
|
TEST(THelp, OptionalPositional) {
|
||||||
CLI::App app{"My prog"};
|
CLI::App app{"My prog"};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user