Commit ba7b29f9f8a40e88f41cb473760ff867af2e5752

Authored by Philip Top
Committed by Henry Schreiner
1 parent 7a4d376d

allow hidden option groups for help. (#356)

* allow hidden option groups for help.

* refactor condition so it continues appropriately but only prints on the desired conditions.
README.md
... ... @@ -632,7 +632,7 @@ The subcommand method
632 632 .add_option_group(name,description)
633 633 ```
634 634  
635   -Will create an option group, and return a pointer to it. An option group allows creation of a collection of options, similar to the groups function on options, but with additional controls and requirements. They allow specific sets of options to be composed and controlled as a collective. For an example see [range example](https://github.com/CLIUtils/CLI11/blob/master/examples/ranges.cpp). Option groups are a specialization of an App so all [functions](#subcommand-options) that work with an App or subcommand also work on option groups. Options can be created as part of an option group using the add functions just like a subcommand, or previously created options can be added through
  635 +Will create an option group, and return a pointer to it. The argument for `description` is optional and can be omitted. An option group allows creation of a collection of options, similar to the groups function on options, but with additional controls and requirements. They allow specific sets of options to be composed and controlled as a collective. For an example see [range example](https://github.com/CLIUtils/CLI11/blob/master/examples/ranges.cpp). Option groups are a specialization of an App so all [functions](#subcommand-options) that work with an App or subcommand also work on option groups. Options can be created as part of an option group using the add functions just like a subcommand, or previously created options can be added through
636 636  
637 637 ```cpp
638 638 ogroup->add_option(option_pointer);
... ... @@ -660,6 +660,12 @@ CLI::TriggerOff(group2_pointer, disabled_group);
660 660  
661 661 These functions make use of `preparse_callback`, `enabled_by_default()` and `disabled_by_default`. The triggered group may be a vector of group pointers. These methods should only be used once per group and will override any previous use of the underlying functions. More complex arrangements can be accomplished using similar methodology with a custom preparse_callback function that does more.
662 662  
  663 +If an empty string is passed the option group name the entire group will be hidden in the help results. For example.
  664 +
  665 +```cpp
  666 +auto hidden_group=app.add_option_group("");
  667 +```
  668 +will create a group such that no options in that group are displayed in the help string.
663 669  
664 670 ### Configuration file
665 671  
... ...
include/CLI/Formatter.hpp
... ... @@ -165,7 +165,9 @@ inline std::string Formatter::make_subcommands(const App *app, AppFormatMode mod
165 165 std::vector<std::string> subcmd_groups_seen;
166 166 for(const App *com : subcommands) {
167 167 if(com->get_name().empty()) {
168   - out << make_expanded(com);
  168 + if(!com->get_group().empty()) {
  169 + out << make_expanded(com);
  170 + }
169 171 continue;
170 172 }
171 173 std::string group_key = com->get_group();
... ...
tests/HelpTest.cpp
... ... @@ -97,6 +97,32 @@ TEST(THelp, Hidden) {
97 97 EXPECT_THAT(help, Not(HasSubstr("another")));
98 98 }
99 99  
  100 +TEST(THelp, HiddenGroup) {
  101 + CLI::App app{"My prog"};
  102 + // empty option group name should be hidden
  103 + auto hgroup = app.add_option_group("");
  104 + std::string x;
  105 + hgroup->add_option("something", x, "My option here");
  106 + std::string y;
  107 + hgroup->add_option("--another", y);
  108 +
  109 + std::string help = app.help();
  110 +
  111 + EXPECT_THAT(help, HasSubstr("My prog"));
  112 + EXPECT_THAT(help, HasSubstr("-h,--help"));
  113 + EXPECT_THAT(help, HasSubstr("Options:"));
  114 + EXPECT_THAT(help, Not(HasSubstr("[something]")));
  115 + EXPECT_THAT(help, Not(HasSubstr("something ")));
  116 + EXPECT_THAT(help, Not(HasSubstr("another")));
  117 +
  118 + hgroup->group("ghidden");
  119 +
  120 + help = app.help();
  121 +
  122 + EXPECT_THAT(help, HasSubstr("something "));
  123 + EXPECT_THAT(help, HasSubstr("another"));
  124 +}
  125 +
100 126 TEST(THelp, OptionalPositionalAndOptions) {
101 127 CLI::App app{"My prog", "AnotherProgram"};
102 128 app.add_flag("-q,--quick");
... ...