Commit 69674dc91bb530cd351b6b89a61598b2b46d88a1

Authored by Philip Top
Committed by GitHub
1 parent e50a75f2

fix: help all was showing up in the required list error if requirement are not met. (#530)

include/CLI/App.hpp
... ... @@ -2253,10 +2253,13 @@ class App {
2253 2253 }
2254 2254  
2255 2255 if(require_option_min_ > used_options || (require_option_max_ > 0 && require_option_max_ < used_options)) {
2256   - auto option_list = detail::join(options_, [](const Option_p &ptr) { return ptr->get_name(false, true); });
2257   - if(option_list.compare(0, 10, "-h,--help,") == 0) {
2258   - option_list.erase(0, 10);
2259   - }
  2256 + auto option_list = detail::join(options_, [this](const Option_p &ptr) {
  2257 + if(ptr.get() == help_ptr_ || ptr.get() == help_all_ptr_) {
  2258 + return std::string{};
  2259 + }
  2260 + return ptr->get_name(false, true);
  2261 + });
  2262 +
2260 2263 auto subc_list = get_subcommands([](App *app) { return ((app->get_name().empty()) && (!app->disabled_)); });
2261 2264 if(!subc_list.empty()) {
2262 2265 option_list += "," + detail::join(subc_list, [](const App *app) { return app->get_display_name(); });
... ...
include/CLI/StringTools.hpp
... ... @@ -76,10 +76,14 @@ std::string join(const T &amp;v, Callable func, std::string delim = &quot;,&quot;) {
76 76 std::ostringstream s;
77 77 auto beg = std::begin(v);
78 78 auto end = std::end(v);
79   - if(beg != end)
80   - s << func(*beg++);
  79 + auto loc = s.tellp();
81 80 while(beg != end) {
82   - s << delim << func(*beg++);
  81 + auto nloc = s.tellp();
  82 + if(nloc > loc) {
  83 + s << delim;
  84 + loc = nloc;
  85 + }
  86 + s << func(*beg++);
83 87 }
84 88 return s.str();
85 89 }
... ...
tests/AppTest.cpp
... ... @@ -135,11 +135,14 @@ TEST_F(TApp, RequireOptionsError) {
135 135 app.add_flag("-c");
136 136 app.add_flag("--q");
137 137 app.add_flag("--this,--that");
  138 + app.set_help_flag("-h,--help");
  139 + app.set_help_all_flag("--help_all");
138 140 app.require_option(1, 2);
139 141 try {
140 142 app.parse("-c --q --this --that");
141 143 } catch(const CLI::RequiredError &re) {
142 144 EXPECT_THAT(re.what(), Not(HasSubstr("-h,--help")));
  145 + EXPECT_THAT(re.what(), Not(HasSubstr("help_all")));
143 146 }
144 147  
145 148 EXPECT_NO_THROW(app.parse("-c --q"));
... ...