Skip to content

Commit a146a11

Browse files
committed
[#2788] add RAII struct for exhausting options
1 parent c3507eb commit a146a11

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/lib/process/d_controller.cc

+4-8
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ DControllerBase::parseArgs(int argc, char* argv[]) {
254254
opterr = 0;
255255
optind = 1;
256256
std::string opts("dvVWc:t:" + getCustomOpts());
257+
258+
// Defer exhausting of arguments to the end.
259+
ExhaustOptions e(argc, argv, opts);
260+
257261
while ((ch = getopt(argc, argv, opts.c_str())) != -1) {
258262
switch (ch) {
259263
case 'd':
@@ -297,10 +301,6 @@ DControllerBase::parseArgs(int argc, char* argv[]) {
297301
char const saved_optopt(optopt);
298302
std::string const saved_optarg(optarg ? optarg : std::string());
299303

300-
// Exhaust all remaining options in case parseArgs() is called again.
301-
while (getopt(argc, argv, opts.c_str()) != -1) {
302-
}
303-
304304
// We hit an invalid option.
305305
isc_throw(InvalidUsage, "unsupported option: -" << saved_optopt <<
306306
(saved_optarg.empty() ? std::string() : " " + saved_optarg));
@@ -314,10 +314,6 @@ DControllerBase::parseArgs(int argc, char* argv[]) {
314314
char const saved_optopt(optopt);
315315
std::string const saved_optarg(optarg ? optarg : std::string());
316316

317-
// Exhaust all remaining options in case parseArgs() is called again.
318-
while (getopt(argc, argv, opts.c_str()) != -1) {
319-
}
320-
321317
// We hit an invalid option.
322318
isc_throw(InvalidUsage, "unsupported option: -" << saved_optopt <<
323319
(saved_optarg.empty() ? std::string() : " " + saved_optarg));

src/lib/process/d_controller.h

+19-3
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,25 @@ class DControllerBase : public Daemon {
652652
/// @brief Singleton instance value.
653653
static DControllerBasePtr controller_;
654654

655-
// DControllerTest is named a friend class to facilitate unit testing while
656-
// leaving the intended member scopes intact.
657-
friend class DControllerTest;
655+
// DControllerTest is named a friend class to facilitate unit testing while
656+
// leaving the intended member scopes intact.
657+
friend class DControllerTest;
658+
659+
/// @brief Structure used in parseArgs() to reset arguments in case parseArgs() is called again.
660+
struct ExhaustOptions {
661+
ExhaustOptions(int argc, char* argv[], std::string opts)
662+
: argc_(argc), argv_(argv), opts_(opts) {
663+
}
664+
~ExhaustOptions() {
665+
while (getopt(argc_, argv_, opts_.c_str()) != -1) {
666+
}
667+
}
668+
669+
private:
670+
int argc_;
671+
char** argv_;
672+
std::string opts_;
673+
};
658674
};
659675

660676
} // namespace process

0 commit comments

Comments
 (0)