Skip to content

Commit 1ab8646

Browse files
duplicate footer (#1200)
fix an issue with duplicate footers being printed with help_all Fixes #1183 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f7e4695 commit 1ab8646

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

include/CLI/impl/Formatter_inl.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,12 @@ CLI11_INLINE std::string Formatter::make_help(const App *app, std::string name,
172172
out << make_positionals(app);
173173
out << make_groups(app, mode);
174174
out << make_subcommands(app, mode);
175+
std::string footer_string = make_footer(app);
176+
175177
if(is_footer_paragraph_formatting_enabled()) {
176-
detail::streamOutAsParagraph(out, make_footer(app), footer_paragraph_width_); // Format footer as paragraph
178+
detail::streamOutAsParagraph(out, footer_string, footer_paragraph_width_); // Format footer as paragraph
177179
} else {
178-
out << make_footer(app) << '\n';
180+
out << footer_string << '\n';
179181
}
180182

181183
return out.str();
@@ -252,10 +254,19 @@ CLI11_INLINE std::string Formatter::make_expanded(const App *sub, AppFormatMode
252254
out << make_positionals(sub);
253255
out << make_groups(sub, mode);
254256
out << make_subcommands(sub, mode);
257+
std::string footer_string = make_footer(sub);
258+
259+
if(mode == AppFormatMode::Sub && !footer_string.empty()) {
260+
const auto *parent = sub->get_parent();
261+
std::string parent_footer = (parent != nullptr) ? make_footer(sub->get_parent()) : std::string{};
262+
if(footer_string == parent_footer) {
263+
footer_string = "";
264+
}
265+
}
255266
if(is_footer_paragraph_formatting_enabled()) {
256-
detail::streamOutAsParagraph(out, make_footer(sub), footer_paragraph_width_); // Format footer as paragraph
267+
detail::streamOutAsParagraph(out, footer_string, footer_paragraph_width_); // Format footer as paragraph
257268
} else {
258-
out << make_footer(sub) << '\n';
269+
out << footer_string << '\n';
259270
}
260271
out << '\n';
261272
return out.str();

tests/HelpTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,43 @@ TEST_CASE("THelp: FooterOptionGroup", "[help]") {
104104
CHECK(footer_loc2 == std::string::npos);
105105
}
106106

107+
/// @brief from github issue #1183
108+
TEST_CASE("THelp: FooterSubcommandHelpAll", "[help]") {
109+
CLI::App app{"My prog"};
110+
111+
app.footer("Report bugs to [email protected]");
112+
app.set_help_all_flag("--help-all", "All options of subcommands");
113+
app.add_subcommand("Subcommand1", "Desc1");
114+
app.add_subcommand("Subcommand2", "Desc2");
115+
116+
CHECK_THROWS_AS(app.parse("--help-all"), CLI::CallForAllHelp);
117+
118+
std::string help = app.help("", CLI::AppFormatMode::All);
119+
120+
auto footer_loc = help.find("[email protected]");
121+
auto footer_loc2 = help.find("[email protected]", footer_loc + 10);
122+
CHECK(footer_loc != std::string::npos);
123+
// should only see the footer once
124+
CHECK(footer_loc2 == std::string::npos);
125+
}
126+
127+
TEST_CASE("THelp: FooterSubcommandHelp", "[help]") {
128+
CLI::App app{"My prog"};
129+
130+
app.footer("Report bugs to [email protected]");
131+
app.add_subcommand("Subcommand1", "Desc1");
132+
app.add_subcommand("Subcommand2", "Desc2");
133+
134+
CHECK_THROWS_AS(app.parse("Subcommand1 Subcommand2 --help"), CLI::CallForHelp);
135+
136+
std::string help = app.help();
137+
auto footer_loc = help.find("[email protected]");
138+
auto footer_loc2 = help.find("[email protected]", footer_loc + 10);
139+
CHECK(footer_loc != std::string::npos);
140+
// should only see the footer once
141+
CHECK(footer_loc2 == std::string::npos);
142+
}
143+
107144
TEST_CASE("THelp: OptionalPositional", "[help]") {
108145
CLI::App app{"My prog", "program"};
109146

0 commit comments

Comments
 (0)