From a8ef5b8d2fe3932039fa51bcd351cf25001c192a Mon Sep 17 00:00:00 2001 From: Philip Top Date: Thu, 17 Jun 2021 14:42:53 -0700 Subject: [PATCH] fix: avoid printing description for non configurable subcommand (#604) --- include/CLI/Config.hpp | 2 +- tests/ConfigFileTest.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/CLI/Config.hpp b/include/CLI/Config.hpp index 17b816a..4909231 100644 --- a/include/CLI/Config.hpp +++ b/include/CLI/Config.hpp @@ -277,7 +277,7 @@ ConfigBase::to_config(const App *app, bool default_also, bool write_description, std::vector groups = app->get_groups(); bool defaultUsed = false; groups.insert(groups.begin(), std::string("Options")); - if(write_description) { + if(write_description && (app->get_configurable() || app->get_parent() == nullptr || app->get_name().empty())) { out << commentLead << app->get_description() << '\n'; } for(auto &group : groups) { diff --git a/tests/ConfigFileTest.cpp b/tests/ConfigFileTest.cpp index afe1705..8c77360 100644 --- a/tests/ConfigFileTest.cpp +++ b/tests/ConfigFileTest.cpp @@ -1888,6 +1888,27 @@ TEST_CASE_METHOD(TApp, "TomlOutputSubsubcomConfigurable", "[config]") { CHECK(std::string::npos == str.find("sub2.newest=true")); } +TEST_CASE_METHOD(TApp, "TomlOutputSubcomNonConfigurable", "[config]") { + + app.add_flag("--simple"); + auto subcom = app.add_subcommand("other", "other_descriptor")->configurable(); + subcom->add_flag("--newer"); + + auto subcom2 = app.add_subcommand("sub2", "descriptor2"); + subcom2->add_flag("--newest")->configurable(false); + + args = {"--simple", "other", "--newer", "sub2", "--newest"}; + run(); + + std::string str = app.config_to_str(true, true); + CHECK_THAT(str, Contains("other_descriptor")); + CHECK_THAT(str, Contains("simple=true")); + CHECK_THAT(str, Contains("[other]")); + CHECK_THAT(str, Contains("newer=true")); + CHECK_THAT(str, !Contains("newest")); + CHECK_THAT(str, !Contains("descriptor2")); +} + TEST_CASE_METHOD(TApp, "TomlOutputSubsubcomConfigurableDeep", "[config]") { app.add_flag("--simple"); -- libgit2 0.21.4