Skip to content

Commit ae7a553

Browse files
Final solution for custom requirements processing
1 parent 3d89b8c commit ae7a553

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

src/utils/CLI11.hpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6783,6 +6783,9 @@ class App {
67836783
/// This is a function that runs when all processing has completed
67846784
std::function<void()> final_callback_{};
67856785

6786+
/// This is a function tat runs after all standard requirement checks havv been processed
6787+
std::function<void()> require_callback_{};
6788+
67866789
///@}
67876790
/// @name Options
67886791
///@{
@@ -7010,6 +7013,13 @@ class App {
70107013
return this;
70117014
}
70127015

7016+
/// Set a callback to execute after all standard requirement checks have been processed
7017+
///
7018+
App *require_callback(std::function<void()> req_callback) {
7019+
require_callback_ = std::move(req_callback);
7020+
return this;
7021+
}
7022+
70137023
/// Set a name for the app (empty will use parser to set the name)
70147024
App *name(std::string app_name = "");
70157025

@@ -9415,21 +9425,34 @@ CLI11_INLINE void App::_process_requirements() {
94159425
}
94169426
}
94179427
for(const auto &subc : need_subcommands_) {
9418-
subc->_process_requirements();
9428+
try {
9429+
subc->_process_requirements();
9430+
} catch (const CLI::RequiresError& e) {
9431+
if (subc->count() == 0) {
9432+
std::string out = get_display_name();
9433+
9434+
for(const App* parent = get_parent(); parent != nullptr; parent = parent->get_parent()) {
9435+
out = parent->get_display_name() + ":" + out;
9436+
}
9437+
throw RequiresError(out, subc->name_);
9438+
} else {
9439+
throw;
9440+
}
9441+
}
94199442
if(subc->count_all() == 0) {
94209443
missing_needed = true;
94219444
missing_need = subc->get_display_name();
94229445
}
94239446
}
94249447
if(missing_needed) {
94259448
if(count_all() > 0) {
9426-
std::string out;
9449+
std::string out = get_display_name();
94279450

94289451
for(const App* parent = get_parent(); parent != nullptr; parent = parent->get_parent()) {
94299452
out = parent->get_display_name() + ":" + out;
94309453
}
94319454

9432-
throw RequiresError(out + get_display_name(), missing_need);
9455+
throw RequiresError(out, missing_need);
94339456
}
94349457
// if we missing something but didn't have any options, just return
94359458
return;
@@ -9507,13 +9530,33 @@ CLI11_INLINE void App::_process_requirements() {
95079530
}
95089531
}
95099532
if(sub->count() > 0 || sub->name_.empty()) {
9510-
sub->_process_requirements();
9533+
try {
9534+
sub->_process_requirements();
9535+
} catch (const CLI::RequiredError& e) {
9536+
std::string out = get_display_name() + ":" ;
9537+
9538+
for(const App* parent = get_parent(); parent != nullptr; parent = parent->get_parent()) {
9539+
out = parent->get_display_name() + ":" + out;
9540+
}
9541+
throw RequiredError(out + e.what());
9542+
}
95119543
}
95129544

95139545
if(sub->required_ && sub->count_all() == 0) {
9514-
throw(CLI::RequiredError(sub->get_display_name()));
9546+
std::string out = sub->get_display_name();
9547+
9548+
for(const App* parent = get_parent(); parent != nullptr; parent = parent->get_parent()) {
9549+
out = parent->get_display_name() + ":" + out;
9550+
}
9551+
9552+
throw(RequiredError(out));
95159553
}
95169554
}
9555+
9556+
9557+
if (require_callback_) {
9558+
require_callback_();
9559+
}
95179560
}
95189561

95199562
CLI11_INLINE void App::_process() {

src/utils/Config.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,10 @@ namespace utils {
607607
}
608608
}
609609
} catch (const DaemonError& e) {
610-
std::cout << "1[" << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT << "] Daemonize: " << e.what()
610+
std::cout << "[" << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT << "] Daemonize: " << e.what()
611611
<< " ... exiting" << std::endl;
612612
} catch (const DaemonFailure& e) {
613-
std::cout << "2[" << Color::Code::FG_RED << "Failure" << Color::Code::FG_DEFAULT << "] Daemonize: " << e.what()
613+
std::cout << "[" << Color::Code::FG_RED << "Failure" << Color::Code::FG_DEFAULT << "] Daemonize: " << e.what()
614614
<< " ... exiting" << std::endl;
615615
} catch (const DaemonSignaled& e) {
616616
std::cout << "Pid: " << getpid() << ", child pid: " << e.getPid() << ": " << e.what() << std::endl;
@@ -637,8 +637,8 @@ namespace utils {
637637
try {
638638
std::cout << e.getApp()->config_to_str(true, true);
639639
} catch (const CLI::ParseError& e1) {
640-
std::cout << "3[" << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT
641-
<< "] Showing config file: " << e.getApp() << " " << e1.get_name() << " " << e1.what() << std::endl;
640+
std::cout << "[" << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT << "] Showing config file: " << e.getApp()
641+
<< " " << e1.get_name() << " " << e1.what() << std::endl;
642642
throw;
643643
}
644644
} catch (const CLI::CallForWriteConfig& e) {
@@ -654,17 +654,17 @@ namespace utils {
654654
throw;
655655
}
656656
} else {
657-
std::cout << "4[" << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT
657+
std::cout << "[" << Color::Code::FG_RED << "Error" << Color::Code::FG_DEFAULT
658658
<< "] Writing config file: " << std::strerror(errno) << std::endl;
659659
}
660660
} catch (const CLI::ConversionError& e) {
661-
std::cout << "5[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
661+
std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
662662
throw;
663663
} catch (const CLI::ArgumentMismatch& e) {
664-
std::cout << "6[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
664+
std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
665665
throw;
666666
} catch (const CLI::ConfigError& e) {
667-
std::cout << "7[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
667+
std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
668668
std::cout << " Adding '-w' on the command line may solve this problem" << std::endl;
669669
throw;
670670
} catch (const CLI::ParseError& e) {
@@ -855,7 +855,6 @@ namespace utils {
855855
return sc->get_required();
856856
})) {
857857
instance->needs(sub);
858-
instance->configurable();
859858
}
860859
} else {
861860
app->remove_needs(instance);
@@ -864,7 +863,6 @@ namespace utils {
864863
return sc->get_required();
865864
})) {
866865
instance->remove_needs(sub);
867-
instance->configurable(false);
868866
}
869867
}
870868

0 commit comments

Comments
 (0)