summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2011-02-25 21:17:11 +0000
committerrandomdan <randomdan@localhost>2011-02-25 21:17:11 +0000
commit9160a405d4fb6daaaaa75bf1bb33a20fdc538774 (patch)
tree9e03dfb1cac8e86aff6b4dac2d6aa004169c44f0
parentBuild a common base for loading p2 xml scripts with proper error checking (diff)
downloadproject2-9160a405d4fb6daaaaa75bf1bb33a20fdc538774.tar.bz2
project2-9160a405d4fb6daaaaa75bf1bb33a20fdc538774.tar.xz
project2-9160a405d4fb6daaaaa75bf1bb33a20fdc538774.zip
Only initialise program_options once (only really affects p2fcgi)
-rw-r--r--project2/cgi/cgiEnvironment.cpp6
-rw-r--r--project2/console/consoleEnvironment.cpp10
-rw-r--r--project2/environment.cpp42
-rw-r--r--project2/environment.h7
4 files changed, 36 insertions, 29 deletions
diff --git a/project2/cgi/cgiEnvironment.cpp b/project2/cgi/cgiEnvironment.cpp
index 3fdb158..43f45f2 100644
--- a/project2/cgi/cgiEnvironment.cpp
+++ b/project2/cgi/cgiEnvironment.cpp
@@ -28,12 +28,12 @@ CgiEnvironment::addOptions(boost::program_options::positional_options_descriptio
{
boost::program_options::options_description cgi("Project2 CGI options");
cgi.add_options()
- ("errorcontenttype", boost::program_options::value<Glib::ustring>(&errorContentType)->default_value("text/xml"),
+ ("errorcontenttype", boost::program_options::value(&errorContentType)->default_value("text/xml"),
"The Content-Type to use in HTTP headers in event of an error")
- ("errortransformstyle", boost::program_options::value<Glib::ustring>(&errorTransformStyle),
+ ("errortransformstyle", boost::program_options::value(&errorTransformStyle),
"The xml-stylesheet to specify in the data document in event of an error")
#ifndef NDEBUG
- ("dumpdatadoc", boost::program_options::value<std::string>(&dumpdatadoc),
+ ("dumpdatadoc", boost::program_options::value(&dumpdatadoc),
"Write a copy of the data document before sending it to the web server")
#endif
;
diff --git a/project2/console/consoleEnvironment.cpp b/project2/console/consoleEnvironment.cpp
index f2d4667..5233a67 100644
--- a/project2/console/consoleEnvironment.cpp
+++ b/project2/console/consoleEnvironment.cpp
@@ -38,11 +38,11 @@ ConsoleEnvironment::addOptions(boost::program_options::positional_options_descri
console.add_options()
//("version,v", "Print version and exit")
("help,h", "Print usage and exit")
- ("syslogident,i", po::value<std::string>(&scriptname)->default_value(scriptname), "Log to syslog with ident <arg>")
- ("platform,p", po::value<std::string>(&platform), "Platform")
- ("queryparam,q", po::value<QueryParams>(&queryParams), "Query parameter")
- ("uriparam,u", po::value<UriParams>(&uriParams), "URL paramater")
- ("file,f", po::value<ToDoList>(&todolist), "File to process")
+ ("syslogident,i", po::value(&scriptname)->default_value(scriptname), "Log to syslog with ident <arg>")
+ ("platform,p", po::value(&platform), "Platform")
+ ("queryparam,q", po::value(&queryParams), "Query parameter")
+ ("uriparam,u", po::value(&uriParams), "URL paramater")
+ ("file,f", po::value(&todolist), "File to process")
;
poptions.add("file", -1);
return console;
diff --git a/project2/environment.cpp b/project2/environment.cpp
index d9e2687..a3985ca 100644
--- a/project2/environment.cpp
+++ b/project2/environment.cpp
@@ -6,6 +6,12 @@
namespace po = boost::program_options;
+int Environment::clLevel(-1);
+int Environment::slLevel(-1);
+bool Environment::optionsBuilt(false);
+boost::program_options::options_description Environment::allOptions("Project2 options");
+boost::program_options::positional_options_description Environment::posOptions;
+
Environment::Environment(int c, char ** v) :
argc(c),
argv(v)
@@ -15,35 +21,29 @@ Environment::Environment(int c, char ** v) :
void
Environment::init()
{
- int clLevel = -1;
- int slLevel = -1;
-
- po::options_description common("Project2 Common options");
- po::positional_options_description poptions;
- common.add_options()
- ("sysloglevel,s", po::value<int>(&slLevel)->default_value(-1),
- "Log to syslog with level <arg> (default OFF)")
- ("consoleloglevel,c", po::value<int>(&clLevel)->default_value(LOG_WARNING),
- "Log to console with level <arg> (default WARNING)")
- ;
- po::options_description all("Project2 options");
- all.add(common).add(addOptions(poptions));
-
+ if (!optionsBuilt) {
+ po::options_description common("Project2 Common options");
+ common.add_options()
+ ("sysloglevel,s", po::value(&slLevel)->default_value(-1),
+ "Log to syslog with level <arg> (default OFF)")
+ ("consoleloglevel,c", po::value(&clLevel)->default_value(LOG_WARNING),
+ "Log to console with level <arg> (default WARNING)")
+ ;
+ allOptions.add(common).add(addOptions(posOptions));
+ optionsBuilt = true;
+ }
po::variables_map settings;
if (argc > 0 && argv != NULL) {
- po::store(po::command_line_parser(argc, argv).options(all).positional(poptions).run(), settings);
+ po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), settings);
}
- po::store(po::parse_environment(all, "P2_"), settings);
+ po::store(po::parse_environment(allOptions, "P2_"), settings);
if (boost::filesystem::exists(".p2config")) {
std::ifstream f(".p2config");
- // Allow anything in release, but valid the config file in debug
- // Some options only work in debug and you wouldn't want extra
- // config entries to break an install
- po::store(po::parse_config_file(f, all, true), settings);
+ po::store(po::parse_config_file(f, allOptions, true), settings);
}
po::notify(settings);
- postinit(all, settings);
+ postinit(allOptions, settings);
if (clLevel >= 0) {
Logger()->addLogger(new ConsoleLogDriver(stderr, clLevel, false));
diff --git a/project2/environment.h b/project2/environment.h
index 246c525..99f9253 100644
--- a/project2/environment.h
+++ b/project2/environment.h
@@ -23,6 +23,13 @@ class Environment {
virtual void postinit(const boost::program_options::options_description &, const boost::program_options::variables_map &) = 0;
int argc;
char ** argv;
+
+ static bool optionsBuilt;
+ static boost::program_options::options_description allOptions;
+ static boost::program_options::positional_options_description posOptions;
+
+ static int clLevel;
+ static int slLevel;
};
#endif