summaryrefslogtreecommitdiff
path: root/cpp/lib
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/lib')
-rw-r--r--cpp/lib/common/CommonOptions.cpp24
-rw-r--r--cpp/lib/common/CommonOptions.h7
2 files changed, 18 insertions, 13 deletions
diff --git a/cpp/lib/common/CommonOptions.cpp b/cpp/lib/common/CommonOptions.cpp
index ebb3543105..2b6657b4e0 100644
--- a/cpp/lib/common/CommonOptions.cpp
+++ b/cpp/lib/common/CommonOptions.cpp
@@ -26,20 +26,20 @@ namespace qpid {
namespace program_options {
-char env2optchar(char env) {
- return (env=='_') ? '-' : tolower(env);
-}
-
-const std::string envPrefix("QPID_");
-
-std::string env2option(const std::string& env) {
- if (env.find(envPrefix) ==0) {
- std::string opt = env.substr(envPrefix.size());
- std::transform(opt.begin(), opt.end(), opt.begin(), env2optchar);
- return opt;
+static const std::string prefix("QPID_");
+
+static char env2optchar(char env) { return (env=='_') ? '-' : tolower(env); }
+
+std::string EnvMapper::operator()(const std::string& env) {
+ if (env.substr(0, prefix.size()) == prefix) {
+ std::string opt = env.substr(prefix.size());
+ transform(opt.begin(), opt.end(), opt.begin(), env2optchar);
+ // Ignore env vars that don't match to known options.
+ if (opts.find_nothrow(opt, false))
+ return opt;
}
return std::string();
-}
+}
} // namespace program_options
diff --git a/cpp/lib/common/CommonOptions.h b/cpp/lib/common/CommonOptions.h
index beaeb114e6..086b9742fb 100644
--- a/cpp/lib/common/CommonOptions.h
+++ b/cpp/lib/common/CommonOptions.h
@@ -64,8 +64,13 @@ value_semantic* optValue(T& value, const char* arg) {
/** Environment-to-option name mapping.
* Maps env variable "QPID_SOME_VAR" to option "some-var"
+ * Ignores env vars that dont match known options._
*/
-std::string env2option(const std::string& env);
+struct EnvMapper {
+ EnvMapper(const options_description& o) : opts(o) {}
+ std::string operator()(const std::string& env);
+ const options_description& opts;
+};
/**
* Like boost::program_options::bool_switch but takes reference, not pointer.