@@ -257,12 +257,7 @@ namespace utils {
257257 ->type_name (" configfile" )
258258 ->check (!CLI::ExistingDirectory);
259259
260- app->add_option_function <std::string>(
261- " -w,--write-config" ,
262- []([[maybe_unused]] const std::string& configFile) {
263- throw CLI::CallForWriteConfig (configFile);
264- },
265- " Write config file and exit" )
260+ app->add_option (" -w,--write-config" , " Write config file and exit" )
266261 ->configurable (false )
267262 ->default_val (configDirectory + " /" + applicationName + " .conf" )
268263 ->type_name (" [configfile]" )
@@ -368,8 +363,8 @@ namespace utils {
368363 aliases.clear ();
369364
370365 app->final_callback ([]() {
371- if (daemonizeOpt->as <bool >() && (*app)[ " --show-config " ]-> count () == 0 && (*app)[ " --write-config " ]-> count () == 0 &&
372- (*app)[" --command-line" ]->count () == 0 ) {
366+ if (daemonizeOpt->as <bool >() && helpTriggerApp == nullptr && showConfigTriggerApp == nullptr &&
367+ (*app)[" --write-config " ]-> count () == 0 && (*app)[ " -- command-line" ]->count () == 0 ) {
373368 std::cout << " Running as daemon (double fork)" << std::endl;
374369
375370 utils::Daemon::startDaemon (
@@ -415,9 +410,7 @@ namespace utils {
415410
416411 proceed = false ;
417412 } else {
418- if ((*app)[" --show-config" ]->count () == 0 && (*app)[" --write-config" ]->count () == 0 && (*app)[" --command-line" ]->count () == 0 ) {
419- app->allow_extras (false );
420- }
413+ app->allow_extras ((*app)[" --show-config" ]->count () != 0 );
421414
422415 if (!quietOpt->as <bool >()) {
423416 logger::Logger::setLogLevel (logLevelOpt->as <int >());
@@ -440,9 +433,9 @@ namespace utils {
440433 };
441434
442435 std::string out;
443- out.reserve (s.size () * 2 );
436+ out.reserve (s.size () * 3 );
444437
445- for (char c : s) {
438+ for (const char c : s) {
446439 if (special.contains (c)) {
447440 out.push_back (' \\ ' );
448441 }
@@ -522,7 +515,7 @@ namespace utils {
522515 createCommandLineOptions (out, app, mode);
523516
524517 std::string optionString = out.str ();
525- if (optionString.back () == ' ' ) {
518+ if (!optionString. empty () && optionString.back () == ' ' ) {
526519 optionString.pop_back ();
527520 }
528521
@@ -586,22 +579,47 @@ namespace utils {
586579
587580 try {
588581 try {
589- app->parse (argc, argv);
590- success = true ;
582+ try {
583+ app->parse (argc, argv);
584+ success = true ;
585+ } catch (const CLI::ParseError&) {
586+ if (helpTriggerApp == nullptr ) {
587+ if (showConfigTriggerApp != nullptr ) {
588+ success = false ;
589+ throw CLI::CallForShowConfig (showConfigTriggerApp);
590+ }
591+ if ((*app)[" --write-config" ]->count () > 0 ) {
592+ success = false ;
593+ throw CLI::CallForWriteConfig ((*app)[" --write-config" ]->as <std::string>());
594+ }
595+ }
596+
597+ throw ;
598+ }
599+ if (helpTriggerApp == nullptr ) {
600+ if (showConfigTriggerApp != nullptr ) {
601+ success = false ;
602+ throw CLI::CallForShowConfig (showConfigTriggerApp);
603+ }
604+ if ((*app)[" --write-config" ]->count () > 0 ) {
605+ success = false ;
606+ throw CLI::CallForWriteConfig ((*app)[" --write-config" ]->as <std::string>());
607+ }
608+ }
591609 } catch (const DaemonError& e) {
592610 std::cout << " Daemon error: " << e.what () << " ... exiting" << std::endl;
593611 } catch (const DaemonFailure& e) {
594612 std::cout << " Daemon failure: " << e.what () << " ... exiting" << std::endl;
595613 } catch (const DaemonSignaled& e) {
596614 std::cout << " Pid: " << getpid () << " , child pid: " << e.getPid () << " : " << e.what () << std::endl;
597615 } catch (const CLI::CallForHelp&) {
598- const std::string helpMode = helpApp ->get_option (" --help" )->as <std::string>();
616+ const std::string helpMode = helpTriggerApp ->get_option (" --help" )->as <std::string>();
599617 const CLI::App* helpApp = nullptr ;
600618 CLI::AppFormatMode mode = CLI::AppFormatMode::Normal;
601619 if (helpMode == " exact" ) {
602- helpApp = utils::Config::helpApp ;
620+ helpApp = utils::Config::helpTriggerApp ;
603621 } else if (helpMode == " expanded" ) {
604- helpApp = utils::Config::helpApp ;
622+ helpApp = utils::Config::helpTriggerApp ;
605623 mode = CLI::AppFormatMode::All;
606624 }
607625 std::cout << app->help (helpApp, " " , mode) << std::endl;
@@ -657,10 +675,11 @@ namespace utils {
657675 throw ;
658676 }
659677 } catch ([[maybe_unused]] const CLI::ParseError& e) {
660- std::cout << std::endl << " Append -h or --help to your command line for more information." << std::endl;
678+ std::cout << std::endl << " Append -h or --help to your command line for more information." << std::endl;
661679 } catch (const CLI::Error& e) {
662- std::cout << " [" << Color::Code::FG_RED << e.get_name () << Color::Code::FG_DEFAULT << " ] " << e.what () << std::endl;
663- std::cout << " Append -h or --help to your command line for more information." << std::endl;
680+ std::cout << " [" << Color::Code::FG_RED << e.get_name () << Color::Code::FG_DEFAULT << " ] " << e.what () << std::endl;
681+
682+ std::cout << std::endl << " Append -h or --help to your command line for more information." << std::endl;
664683 }
665684
666685 return success;
@@ -736,10 +755,10 @@ namespace utils {
736755
737756 CLI::App* Config::addStandardFlags (CLI::App* app) {
738757 app //
739- ->add_flag_callback (
758+ ->add_flag_function (
740759 " -s,--show-config" ,
741- [app]() {
742- throw CLI::CallForShowConfig ( app) ;
760+ [app](std:: size_t ) {
761+ showConfigTriggerApp = app;
743762 },
744763 " Show current configuration and exit" ) //
745764 ->configurable (false )
@@ -800,9 +819,9 @@ namespace utils {
800819
801820 CLI::App* Config::addHelp (CLI::App* app) {
802821 app->set_help_flag (
803- " -h{standard },--help{standard }" ,
822+ " -h{exact },--help{exact }" ,
804823 [app](std::size_t ) {
805- helpApp = app;
824+ helpTriggerApp = app;
806825 },
807826 " Print help message and exit" )
808827 ->group (app->get_formatter ()->get_label (" Nonpersistent Options" ))
@@ -813,13 +832,13 @@ namespace utils {
813832
814833 CLI::App* Config::addSimpleHelp (CLI::App* app) {
815834 app->set_help_flag (
816- " --help,-h" ,
835+ " --help{exact} ,-h{exact} " ,
817836 [app](std::size_t ) {
818- helpApp = app;
837+ helpTriggerApp = app;
819838 },
820839 " Print help message and exit" )
821840 ->group (app->get_formatter ()->get_label (" Nonpersistent Options" ))
822- ->disable_flag_override ( );
841+ ->check ( CLI::IsMember ({ " standard " , " exact " }) );
823842
824843 return app;
825844 }
@@ -832,6 +851,7 @@ namespace utils {
832851 return sc->get_required ();
833852 })) {
834853 instance->needs (sub);
854+ instance->configurable ();
835855 }
836856 } else {
837857 app->remove_needs (instance);
@@ -840,6 +860,7 @@ namespace utils {
840860 return sc->get_required ();
841861 })) {
842862 instance->remove_needs (sub);
863+ instance->configurable (false );
843864 }
844865 }
845866
@@ -988,7 +1009,8 @@ namespace utils {
9881009 CLI::Option* Config::verboseLevelOpt = nullptr ;
9891010 CLI::Option* Config::quietOpt = nullptr ;
9901011
991- CLI::App* Config::helpApp = nullptr ;
1012+ CLI::App* Config::helpTriggerApp = nullptr ;
1013+ CLI::App* Config::showConfigTriggerApp = nullptr ;
9921014
9931015 std::map<std::string, std::string> Config::aliases;
9941016 std::map<std::string, CLI::Option*> Config::applicationOptions;
0 commit comments