Commit 6bd31c392d621486f2a90d705a2c7ac6bcb9a581

Authored by Henry Fredrick Schreiner
Committed by Henry Schreiner
1 parent 899100c1

Fixing #168

examples/CMakeLists.txt
... ... @@ -128,3 +128,7 @@ set_property(TEST subcom_in_files PROPERTY PASS_REGULAR_EXPRESSION
128 128 add_cli_exe(formatter formatter.cpp)
129 129  
130 130 add_cli_exe(nested nested.cpp)
  131 +
  132 +add_cli_exe(subcom_help subcom_help.cpp)
  133 +add_test(NAME subcom_help_normal COMMAND subcom_help sub --help)
  134 +add_test(NAME subcom_help_reversed COMMAND subcom_help --help sub)
... ...
examples/subcom_help.cpp 0 → 100644
  1 +#include <CLI/CLI.hpp>
  2 +#include <iostream>
  3 +
  4 +int main(int argc, char *argv[]) {
  5 + CLI::App cli_global{"Demo app"};
  6 + auto &cli_sub = *cli_global.add_subcommand("sub", "Some subcommand");
  7 + std::string sub_arg;
  8 + cli_sub.add_option("sub_arg", sub_arg, "Argument for subcommand")->required();
  9 + CLI11_PARSE(cli_global, argc, argv);
  10 + if(cli_sub) {
  11 + std::cout << "Got: " << sub_arg << std::endl;
  12 + }
  13 + return 0;
  14 +}
... ...
include/CLI/App.hpp
... ... @@ -1341,6 +1341,10 @@ class App {
1341 1341  
1342 1342 // Verify required options
1343 1343 for(const Option_p &opt : options_) {
  1344 + // Exit if a help flag was passed (requirements not required in that case)
  1345 + if(_any_help_flag())
  1346 + break;
  1347 +
1344 1348 // Required or partially filled
1345 1349 if(opt->get_required() || opt->count() != 0) {
1346 1350 // Make sure enough -N arguments parsed (+N is already handled in parsing function)
... ... @@ -1379,6 +1383,21 @@ class App {
1379 1383 }
1380 1384 }
1381 1385  
  1386 + /// Return True if a help flag detected (checks all parents)
  1387 + bool _any_help_flag() const {
  1388 + bool result = false;
  1389 + const Option *help_ptr = get_help_ptr();
  1390 + const Option *help_all_ptr = get_help_all_ptr();
  1391 + if(help_ptr != nullptr && help_ptr->count() > 0)
  1392 + result = true;
  1393 + if(help_all_ptr != nullptr && help_all_ptr->count() > 0)
  1394 + result = true;
  1395 + if(parent_ != nullptr)
  1396 + return result || parent_->_any_help_flag();
  1397 + else
  1398 + return result;
  1399 + }
  1400 +
1382 1401 /// Parse one config param, return false if not found in any subcommand, remove if it is
1383 1402 ///
1384 1403 /// If this has more than one dot.separated.name, go into the subcommand matching it
... ...