diff options
author | Andrei Pavel <andrei@isc.org> | 2024-01-26 18:47:27 +0100 |
---|---|---|
committer | Andrei Pavel <andrei@isc.org> | 2024-01-26 18:47:41 +0100 |
commit | a146a119a9b717de4810159a68956a6619b4ba05 (patch) | |
tree | f891436f7e456510475aff0a27796ae0c604466e | |
parent | [#2788] reset optarg (diff) | |
download | kea-a146a119a9b717de4810159a68956a6619b4ba05.tar.xz kea-a146a119a9b717de4810159a68956a6619b4ba05.zip |
[#2788] add RAII struct for exhausting options
-rw-r--r-- | src/lib/process/d_controller.cc | 12 | ||||
-rw-r--r-- | src/lib/process/d_controller.h | 22 |
2 files changed, 23 insertions, 11 deletions
diff --git a/src/lib/process/d_controller.cc b/src/lib/process/d_controller.cc index f08794a9c3..cee3cd2284 100644 --- a/src/lib/process/d_controller.cc +++ b/src/lib/process/d_controller.cc @@ -254,6 +254,10 @@ DControllerBase::parseArgs(int argc, char* argv[]) { opterr = 0; optind = 1; std::string opts("dvVWc:t:" + getCustomOpts()); + + // Defer exhausting of arguments to the end. + ExhaustOptions e(argc, argv, opts); + while ((ch = getopt(argc, argv, opts.c_str())) != -1) { switch (ch) { case 'd': @@ -297,10 +301,6 @@ DControllerBase::parseArgs(int argc, char* argv[]) { char const saved_optopt(optopt); std::string const saved_optarg(optarg ? optarg : std::string()); - // Exhaust all remaining options in case parseArgs() is called again. - while (getopt(argc, argv, opts.c_str()) != -1) { - } - // We hit an invalid option. isc_throw(InvalidUsage, "unsupported option: -" << saved_optopt << (saved_optarg.empty() ? std::string() : " " + saved_optarg)); @@ -314,10 +314,6 @@ DControllerBase::parseArgs(int argc, char* argv[]) { char const saved_optopt(optopt); std::string const saved_optarg(optarg ? optarg : std::string()); - // Exhaust all remaining options in case parseArgs() is called again. - while (getopt(argc, argv, opts.c_str()) != -1) { - } - // We hit an invalid option. isc_throw(InvalidUsage, "unsupported option: -" << saved_optopt << (saved_optarg.empty() ? std::string() : " " + saved_optarg)); diff --git a/src/lib/process/d_controller.h b/src/lib/process/d_controller.h index e16253e372..8221e68383 100644 --- a/src/lib/process/d_controller.h +++ b/src/lib/process/d_controller.h @@ -652,9 +652,25 @@ private: /// @brief Singleton instance value. static DControllerBasePtr controller_; -// DControllerTest is named a friend class to facilitate unit testing while -// leaving the intended member scopes intact. -friend class DControllerTest; + // DControllerTest is named a friend class to facilitate unit testing while + // leaving the intended member scopes intact. + friend class DControllerTest; + + /// @brief Structure used in parseArgs() to reset arguments in case parseArgs() is called again. + struct ExhaustOptions { + ExhaustOptions(int argc, char* argv[], std::string opts) + : argc_(argc), argv_(argv), opts_(opts) { + } + ~ExhaustOptions() { + while (getopt(argc_, argv_, opts_.c_str()) != -1) { + } + } + + private: + int argc_; + char** argv_; + std::string opts_; + }; }; } // namespace process |