diff options
author | Kim van der Riet <kpvdr@apache.org> | 2013-02-28 16:14:30 +0000 |
---|---|---|
committer | Kim van der Riet <kpvdr@apache.org> | 2013-02-28 16:14:30 +0000 |
commit | 9c73ef7a5ac10acd6a50d5d52bd721fc2faa5919 (patch) | |
tree | 2a890e1df09e5b896a9b4168a7b22648f559a1f2 /cpp/src/qpid/Options.cpp | |
parent | 172d9b2a16cfb817bbe632d050acba7e31401cd2 (diff) | |
download | qpid-python-asyncstore.tar.gz |
Update from trunk r1375509 through r1450773asyncstore
git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/asyncstore@1451244 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/Options.cpp')
-rw-r--r-- | cpp/src/qpid/Options.cpp | 96 |
1 files changed, 77 insertions, 19 deletions
diff --git a/cpp/src/qpid/Options.cpp b/cpp/src/qpid/Options.cpp index 35787aa8f3..c0e955e2b3 100644 --- a/cpp/src/qpid/Options.cpp +++ b/cpp/src/qpid/Options.cpp @@ -41,13 +41,13 @@ struct EnvOptMapper { return desc->long_name().size() == env.size() && std::equal(env.begin(), env.end(), desc->long_name().begin(), &matchChar); } - + static bool matchCase(const string& env, boost::shared_ptr<po::option_description> desc) { return env == desc->long_name(); } - + EnvOptMapper(const Options& o) : opts(o) {} - + string operator()(const string& envVar) { static const std::string prefix("QPID_"); if (envVar.substr(0, prefix.size()) == prefix) { @@ -74,31 +74,60 @@ struct EnvOptMapper { } - string configFileLine (string& line) { - - if ( isComment ( line ) ) - return string(); + void badArg ( string& line ) { + ostringstream msg; + msg << "Bad argument: |" << line << "|\n"; + throw Exception(msg.str()); + } + + + string configFileLine (string& line, bool allowUnknowns=true) { - size_t pos = line.find ('='); - if (pos == string::npos) + if ( isComment ( line ) ) { return string(); + } + + size_t pos = line.find ('='); + if (pos == string::npos) { + if ( allowUnknowns ) { + return string(); + } + else { + badArg ( line ); + } + } string key = line.substr (0, pos); #if (BOOST_VERSION >= 103300) typedef const std::vector< boost::shared_ptr<po::option_description> > OptDescs; - OptDescs::const_iterator i = + OptDescs::const_iterator i = find_if(opts.options().begin(), opts.options().end(), boost::bind(matchCase, key, _1)); if (i != opts.options().end()) return string (line) + "\n"; - else - return string(); + else { + if ( allowUnknowns ) { + return string(); + } + else { + badArg ( line ); + } + } #else - // Use 'count' to see if this option exists. Using 'find' will SEGV or hang - // if the option has not been defined yet. + // Use 'count' to see if this option exists. Using 'find' will + // SEGV or hang if the option has not been defined yet. if ( opts.count(key.c_str()) > 0 ) return string ( line ) + "\n"; - else - return string ( ); + else { + if ( allowUnknowns ) { + return string ( ); + } + else { + badArg ( line ); + } + } #endif + // Control will not arrive here, but the compiler things it could. + // Calls to badArg(), that I used above, throw. + return string(); } const Options& opts; @@ -109,8 +138,8 @@ std::string prettyArg(const std::string& name, const std::string& value) { return value.empty() ? name+" " : name+" ("+value+") "; } -Options::Options(const string& name) : - po::options_description(name) +Options::Options(const string& name) : + po::options_description(name) { } @@ -150,6 +179,7 @@ void Options::parse(int argc, char const* const* argv, const std::string& config if (!configFile.empty()) { parsing="configuration file "+configFile; ifstream conf(configFile.c_str()); + conf.peek(); if (conf.good()) { // Remove this hack when we get a stable version of boost that // can allow unregistered options in config files. @@ -159,7 +189,7 @@ void Options::parse(int argc, char const* const* argv, const std::string& config while (!conf.eof()) { string line; getline (conf, line); - filtered << mapper.configFileLine (line); + filtered << mapper.configFileLine (line, allowUnknown); } po::store(po::parse_config_file(filtered, *this), vm); @@ -197,5 +227,33 @@ CommonOptions::CommonOptions(const string& name, const string& configfile, const } + +bool Options::findArg(int argc, char const* const* argv, const std::string& theArg) +{ + const string parsing("command line options"); + bool result(false); + try { + if (argc > 0 && argv != 0) { + po::command_line_parser clp = po::command_line_parser(argc, const_cast<char**>(argv)). + options(*this).allow_unregistered(); + po::parsed_options opts = clp.run(); + + for (std::vector< po::basic_option<char> >::iterator + i = opts.options.begin(); i != opts.options.end(); i++) { + if (theArg.compare(i->string_key) == 0) { + result = true; + break; + } + } + } + return result; + } + catch (const std::exception& e) { + ostringstream msg; + msg << "Error in " << parsing << ": " << e.what() << endl; + throw Exception(msg.str()); + } +} + } // namespace qpid |