Commit f27822deaa3b144fbaaafc9d856b85456114b57e

Authored by Philip Top
Committed by GitHub
1 parent 25cca2dc

fix: help flag should work even when config file is required (#606)

include/CLI/App.hpp
... ... @@ -2248,10 +2248,27 @@ class App {
2248 2248  
2249 2249 /// Process callbacks and such.
2250 2250 void _process() {
2251   - _process_config_file();
2252   - _process_env();
  2251 + CLI::FileError fe("ne");
  2252 + bool caught_error{false};
  2253 + try {
  2254 + // the config file might generate a FileError but that should not be processed until later in the process
  2255 + // to allow for help, version and other errors to generate first.
  2256 + _process_config_file();
  2257 + // process env shouldn't throw but no reason to process it if config generated an error
  2258 + _process_env();
  2259 + } catch(const CLI::FileError &fe2) {
  2260 + fe = fe2;
  2261 + caught_error = true;
  2262 + }
  2263 + // callbacks and help_flags can generate exceptions which should take priority over the config file error if one
  2264 + // exists
2253 2265 _process_callbacks();
2254 2266 _process_help_flags();
  2267 +
  2268 + if(caught_error) {
  2269 + throw CLI::FileError(std::move(fe));
  2270 + }
  2271 +
2255 2272 _process_requirements();
2256 2273 }
2257 2274  
... ...
tests/ConfigFileTest.cpp
... ... @@ -535,6 +535,10 @@ TEST_CASE_METHOD(TApp, "IniRequiredNoDefault", "[config]") {
535 535 int two{0};
536 536 app.add_option("--two", two);
537 537 REQUIRE_THROWS_AS(run(), CLI::FileError);
  538 + // test to make sure help still gets called correctly
  539 + // Github issue #533 https://github.com/CLIUtils/CLI11/issues/553
  540 + args = {"--help"};
  541 + REQUIRE_THROWS_AS(run(), CLI::CallForHelp);
538 542 }
539 543  
540 544 TEST_CASE_METHOD(TApp, "IniNotRequiredNoDefault", "[config]") {
... ...