Skip to content

Commit 82c24b3

Browse files
authored
Merge pull request #43 from sy-c/master
daemon extra options
2 parents 3006dfe + 5cdcd96 commit 82c24b3

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

include/Common/Daemon.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "SimpleLog.h"
77
#include <string>
8+
#include <vector>
89

910
#include <Common/Configuration.h>
1011

@@ -35,13 +36,15 @@ class Daemon
3536
// takes as input main(argc,argv) command line parameters, and custom configuration options.
3637
// accepted command line parameters:
3738
// -z configFileURI : load provided config file (e.g. file:../.../daemon.cfg. See Configuration.h for URI specs)
38-
// -okey=value : set a given key/value pair parameter.
39+
// -o key=value : set a given key/value pair parameter.
3940
//
4041
// Configuration options may are overwritten by those provided in [daemon] section of the config file, if any.
4142
// There is a 1-to-1 match between key names and members of the DaemonConfigParameters class.
4243
// i.e. user name running the daemon can be set by passing a pointer to a custom DaemonConfigParameters object,
4344
// but it is overwritten by value of [daemon] userName key in configuration file, if defined.
44-
Daemon(int argc = 0, char* argv[] = nullptr, DaemonConfigParameters* = nullptr);
45+
// Optional argument "extraCommandLineOptions" gives a list of extra option keys (-o key=value) accepted on the command line.
46+
// They are parsed and made available in the "execOptions" variable after initialization.
47+
Daemon(int argc = 0, char* argv[] = nullptr, DaemonConfigParameters* = nullptr, std::vector<std::string> extraCommandLineOptions = {});
4548

4649
// destructor
4750
virtual ~Daemon();
@@ -69,6 +72,13 @@ class Daemon
6972
SimpleLog log; // object for output logging.
7073
ConfigFile config; // input configuration file, if any. Loaded if path provided on command line.
7174

75+
struct ConfigOption {
76+
std::string key;
77+
std::string value;
78+
};
79+
80+
std::vector<ConfigOption> execOptions; // options extracted from command line arguments (-o key=value)
81+
7282
// check daemon status (e.g. after constructor, before starting main loop by calling run(), to know if init success)
7383
bool isOk();
7484

src/Daemon.cxx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void print_usage()
7373
printf("\n");
7474
}
7575

76-
Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams)
76+
Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams, std::vector<std::string> extraCommandLineOptions)
7777
{
7878
isInitialized = 0;
7979

@@ -127,8 +127,18 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams)
127127
} else if (key == "logRotateNow") {
128128
params.logRotateNow = std::stoi(value);
129129
} else {
130-
log.error("Unkown option key %s in option %s", key.c_str(), optarg);
131-
throw __LINE__;
130+
bool keyOk = 0;
131+
for (auto const& k : extraCommandLineOptions) {
132+
if (k == key) {
133+
keyOk = 1;
134+
execOptions.push_back({ key, value });
135+
break;
136+
}
137+
}
138+
if (!keyOk) {
139+
log.error("Unkown option key %s in option %s", key.c_str(), optarg);
140+
throw __LINE__;
141+
}
132142
}
133143
} break;
134144

0 commit comments

Comments
 (0)