Skip to content

Commit

Permalink
style: pre-commit.ci fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Oct 7, 2024
1 parent 7da8399 commit 592c125
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -905,13 +905,15 @@ option_groups. These are:
options are specified in the `add_option` calls or the ability to process
options in the form of `-s --long --file=file_name.ext`.
- `.fallthrough()`: Allow extra unmatched options and positionals to "fall
through" and be matched on a parent option. Subcommands by default are allowed to
"fall through" as in they will first attempt to match on the current
through" and be matched on a parent option. Subcommands by default are allowed
to "fall through" as in they will first attempt to match on the current
subcommand and if they fail will progressively check parents for matching
subcommands. This can be disabled through `subcommand_fallthrough(false)`
- `.subcommand_fallthrough()`: Allow subcommands to "fall
through" and be matched on a parent option. Disabling this prevents additional subcommands at the same level from being matched.
It can be useful in certain circumstances where there might be ambiguity between subcommands and positionals. The default is true.
- `.subcommand_fallthrough()`: Allow subcommands to "fall through" and be
matched on a parent option. Disabling this prevents additional subcommands at
the same level from being matched. It can be useful in certain circumstances
where there might be ambiguity between subcommands and positionals. The
default is true.
- `.configurable()`: Allow the subcommand to be triggered from a configuration
file. By default subcommand options in a configuration file do not trigger a
subcommand but will just update default values.
Expand Down
5 changes: 3 additions & 2 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ class App {
/// If true, the program should ignore underscores INHERITABLE
bool ignore_underscore_{false};

/// Allow options or other arguments to fallthrough, so that parent commands can collect options after subcommand. INHERITABLE
/// Allow options or other arguments to fallthrough, so that parent commands can collect options after subcommand.
/// INHERITABLE
bool fallthrough_{false};

/// Allow subcommands to fallthrough, so that parent commands can trigger other subcommands after subcommand.
Expand Down Expand Up @@ -838,7 +839,7 @@ class App {
return this;
}

/// Set subcommand fallthrough, set to true so that subcommands on parents are recognized
/// Set subcommand fallthrough, set to true so that subcommands on parents are recognized
App *subcommand_fallthrough(bool value = true) {
subcommand_fallthrough_ = value;
return this;
Expand Down
27 changes: 14 additions & 13 deletions include/CLI/impl/App_inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,15 +1038,16 @@ CLI11_INLINE void App::run_callback(bool final_mode, bool suppress_final_callbac

CLI11_NODISCARD CLI11_INLINE bool App::_valid_subcommand(const std::string &current, bool ignore_used) const {
// Don't match if max has been reached - but still check parents
if(require_subcommand_max_ != 0 && parsed_subcommands_.size() >= require_subcommand_max_ && subcommand_fallthrough_) {
if(require_subcommand_max_ != 0 && parsed_subcommands_.size() >= require_subcommand_max_ &&
subcommand_fallthrough_) {
return parent_ != nullptr && parent_->_valid_subcommand(current, ignore_used);
}
auto *com = _find_subcommand(current, true, ignore_used);
if(com != nullptr) {
return true;
}
// Check parent if exists, else return false
if (subcommand_fallthrough_) {
if(subcommand_fallthrough_) {
return parent_ != nullptr && parent_->_valid_subcommand(current, ignore_used);
}
return false;
Expand Down Expand Up @@ -1735,20 +1736,20 @@ CLI11_INLINE bool App::_parse_positional(std::vector<std::string> &args, bool ha
return true;
}

for (auto& subc : subcommands_) {
if ((subc->name_.empty()) && (!subc->disabled_)) {
if (subc->_parse_positional(args, false)) {
if (!subc->pre_parse_called_) {
for(auto &subc : subcommands_) {
if((subc->name_.empty()) && (!subc->disabled_)) {
if(subc->_parse_positional(args, false)) {
if(!subc->pre_parse_called_) {
subc->_trigger_pre_parse(args.size());
}
return true;
}
}
}
// let the parent deal with it if possible
if (parent_ != nullptr && fallthrough_) {
return _get_fallthrough_parent()->_parse_positional(args, static_cast<bool>(parse_complete_callback_));
}
if(parent_ != nullptr && fallthrough_) {
return _get_fallthrough_parent()->_parse_positional(args, static_cast<bool>(parse_complete_callback_));
}
/// Try to find a local subcommand that is repeated
auto *com = _find_subcommand(args.back(), true, false);
if(com != nullptr && (require_subcommand_max_ == 0 || require_subcommand_max_ > parsed_subcommands_.size())) {
Expand All @@ -1759,13 +1760,13 @@ CLI11_INLINE bool App::_parse_positional(std::vector<std::string> &args, bool ha
com->_parse(args);
return true;
}
if (subcommand_fallthrough_) {
if(subcommand_fallthrough_) {
/// now try one last gasp at subcommands that have been executed before, go to root app and try to find a
/// subcommand in a broader way, if one exists let the parent deal with it
auto* parent_app = (parent_ != nullptr) ? _get_fallthrough_parent() : this;
auto *parent_app = (parent_ != nullptr) ? _get_fallthrough_parent() : this;
com = parent_app->_find_subcommand(args.back(), true, false);
if (com != nullptr && (com->parent_->require_subcommand_max_ == 0 ||
com->parent_->require_subcommand_max_ > com->parent_->parsed_subcommands_.size())) {
if(com != nullptr && (com->parent_->require_subcommand_max_ == 0 ||
com->parent_->require_subcommand_max_ > com->parent_->parsed_subcommands_.size())) {
return false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/SubcommandTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,16 +726,16 @@ TEST_CASE_METHOD(TApp, "Required1SubCom", "[subcom]") {
}

TEST_CASE_METHOD(TApp, "subcomNoSubComfallthrough", "[subcom]") {
auto *sub1=app.add_subcommand("sub1");
auto *sub1 = app.add_subcommand("sub1");
std::vector<std::string> pos;
sub1->add_option("args",pos);
sub1->add_option("args", pos);
app.add_subcommand("sub2");
app.add_subcommand("sub3");
sub1->subcommand_fallthrough(false);
CHECK_FALSE(sub1->get_subcommand_fallthrough());
args = {"sub1", "sub2","sub3"};
args = {"sub1", "sub2", "sub3"};
run();
CHECK(pos.size()==2);
CHECK(pos.size() == 2);
}

TEST_CASE_METHOD(TApp, "BadSubcommandSearch", "[subcom]") {
Expand Down

0 comments on commit 592c125

Please sign in to comment.