Skip to content

Commit a8ef5b8

Browse files
authored
fix: avoid printing description for non configurable subcommand (#604)
1 parent 2fa8cae commit a8ef5b8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

include/CLI/Config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ ConfigBase::to_config(const App *app, bool default_also, bool write_description,
277277
std::vector<std::string> groups = app->get_groups();
278278
bool defaultUsed = false;
279279
groups.insert(groups.begin(), std::string("Options"));
280-
if(write_description) {
280+
if(write_description && (app->get_configurable() || app->get_parent() == nullptr || app->get_name().empty())) {
281281
out << commentLead << app->get_description() << '\n';
282282
}
283283
for(auto &group : groups) {

tests/ConfigFileTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,27 @@ TEST_CASE_METHOD(TApp, "TomlOutputSubsubcomConfigurable", "[config]") {
18881888
CHECK(std::string::npos == str.find("sub2.newest=true"));
18891889
}
18901890

1891+
TEST_CASE_METHOD(TApp, "TomlOutputSubcomNonConfigurable", "[config]") {
1892+
1893+
app.add_flag("--simple");
1894+
auto subcom = app.add_subcommand("other", "other_descriptor")->configurable();
1895+
subcom->add_flag("--newer");
1896+
1897+
auto subcom2 = app.add_subcommand("sub2", "descriptor2");
1898+
subcom2->add_flag("--newest")->configurable(false);
1899+
1900+
args = {"--simple", "other", "--newer", "sub2", "--newest"};
1901+
run();
1902+
1903+
std::string str = app.config_to_str(true, true);
1904+
CHECK_THAT(str, Contains("other_descriptor"));
1905+
CHECK_THAT(str, Contains("simple=true"));
1906+
CHECK_THAT(str, Contains("[other]"));
1907+
CHECK_THAT(str, Contains("newer=true"));
1908+
CHECK_THAT(str, !Contains("newest"));
1909+
CHECK_THAT(str, !Contains("descriptor2"));
1910+
}
1911+
18911912
TEST_CASE_METHOD(TApp, "TomlOutputSubsubcomConfigurableDeep", "[config]") {
18921913

18931914
app.add_flag("--simple");

0 commit comments

Comments
 (0)