From 08d840bfbe92c517c279c7527323d0d1d4470e2c Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 20 May 2024 11:14:55 -0700 Subject: [PATCH] Group merge (#1039) This PR adds a mechanism to hide groups but have their options visible as part of the parent. This works for option group names starting with a '+' for example ``` CLI::App app; bool flag = false; std::optional optional_flag = std::nullopt; app.add_option("--tester"); auto *m1=app.add_option_group("+tester"); m1->add_option("--flag", flag, "description"); m1->add_option("--optional_flag", optional_flag, "description"); CLI11_PARSE(app,argc, argv); ``` will produce help as ```txt Options: -h,--help Print this help message and exit --tester --flag BOOLEAN description --optional_flag BOOLEAN description ``` instead of ``` Options: -h,--help Print this help message and exit --tester [Option Group: tester] Options: --flag BOOLEAN description --optional_flag BOOLEAN description ``` Fixes issue #1034 and a few other past issues or questions --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- CHANGELOG.md | 5 +++++ README.md | 12 +++++++++++- include/CLI/App.hpp | 5 +++++ include/CLI/impl/App_inl.hpp | 17 +++++++++++++++-- include/CLI/impl/Formatter_inl.hpp | 6 +++--- tests/OptionGroupTest.cpp | 26 ++++++++++++++++++++++++++ 6 files changed, 65 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9cabab4..8a1cae53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## Unreleased + +- add mechanism to allow option groups to be hidden and all options be + considered part of the parent for help display + ## Version 2.4: Unicode and TOML support This version adds Unicode support, support for TOML standard including multiline diff --git a/README.md b/README.md index 0f322991..19c855fb 100644 --- a/README.md +++ b/README.md @@ -1205,7 +1205,17 @@ auto hidden_group=app.add_option_group(""); ``` will create a group such that no options in that group are displayed in the help -string. +string. For the purposes of help display, if the option group name starts with a +'+' it is treated as if it were not in a group for help and get_options. For +example: + +```cpp +auto added_group=app.add_option_group("+sub"); +``` + +In this case the help output will not reference the option group and options +inside of it will be treated for most purposes as if they were part of the +parent. ### Configuration file diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 940b858b..e3fcbd52 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -1340,6 +1340,11 @@ class Option_group : public App { : App(std::move(group_description), "", parent) { group(group_name); // option groups should have automatic fallthrough + if(group_name.empty() || group_name.front() == '+') { + // help will not be used by default in these contexts + set_help_flag(""); + set_help_all_flag(""); + } } using App::add_option; /// Add an existing option to the Option_group diff --git a/include/CLI/impl/App_inl.hpp b/include/CLI/impl/App_inl.hpp index 43b9dfe4..a06955c4 100644 --- a/include/CLI/impl/App_inl.hpp +++ b/include/CLI/impl/App_inl.hpp @@ -784,7 +784,14 @@ CLI11_INLINE std::vector App::get_options(const std::functionget_name().empty() && !subc->get_group().empty() && subc->get_group().front() == '+') { + std::vector subcopts = subc->get_options(filter); + options.insert(options.end(), subcopts.begin(), subcopts.end()); + } + } return options; } @@ -798,7 +805,13 @@ CLI11_INLINE std::vector