Skip to content

Commit d32c0b3

Browse files
Recursive needs processing
The current way needs relations are checked in void App::_process_requirements() is missleading. Assume you have an application 'test' with a CLI11 config system configuring following needs relations between some subcommands: app ---> sub1 ---> sub11 | |-> sub12 |-> sub2 A) Current needs processing gives: ./test -> test requires sub1 // OK for the user ./test sub1 -> test requires sub2 // OK for the user ./test sub1 sub2 -> sub1 requires sub11 // Confuses the user ./test sub1 sub2 sub11 -> ready for execution B) Recursive needs processing gives: ./test -> test requires sub1 // OK ./test sub1 -> sub1 requires sub11 // Much more intuitive for the user ./test sub1 sub2 -> sub1 requires sub11 // Makes sense: sub1 configuration is not compleate yet ./test sub1 sub11 -> test requires sub2 // OK, sub1 configuration is compleate ./test sub1 sub11 sub2 -> ready for execution
1 parent 5248d5b commit d32c0b3

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

include/CLI/impl/App_inl.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,10 +1317,22 @@ CLI11_INLINE void App::_process_requirements() {
13171317
missing_need = opt->get_name();
13181318
}
13191319
}
1320+
for(const auto &subc : need_subcommands_) { // Process subcommands given on the commandline first
1321+
if (subc->count() > 0) {
1322+
subc->_process_requirements();
1323+
}
1324+
}
13201325
for(const auto &subc : need_subcommands_) {
1321-
if(subc->count_all() == 0) {
1322-
missing_needed = true;
1323-
missing_need = subc->get_display_name();
1326+
if (subc->count() == 0) {
1327+
try {
1328+
subc->_process_requirements();
1329+
} catch (const CLI::RequiresError&) {
1330+
throw RequiresError(get_display_name(), subc->name_);
1331+
}
1332+
if(subc->count_all() == 0) {
1333+
missing_needed = true;
1334+
missing_need = subc->get_display_name();
1335+
}
13241336
}
13251337
}
13261338
if(missing_needed) {
@@ -1402,7 +1414,7 @@ CLI11_INLINE void App::_process_requirements() {
14021414
}
14031415
}
14041416
}
1405-
if(sub->count() > 0 || sub->name_.empty()) {
1417+
if(sub->count() > 0 || sub->name_.empty() && need_subcommands_.find(sub.get()) == need_subcommands_.end()) {
14061418
sub->_process_requirements();
14071419
}
14081420

0 commit comments

Comments
 (0)