From 9b728d20cfadb185ccbe6df9c553af71155cb348 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 16 Dec 2011 19:44:00 +0000 Subject: Improvements to the handling of command line arguments --- project2/cgi/cgiEnvironment.cpp | 6 +++ project2/cgi/cgiEnvironment.h | 1 + project2/cgi/testCgi.cpp | 16 +++++- project2/common/environment.cpp | 38 ++++++++------ project2/common/options.cpp | 49 +++++++++++++++--- project2/common/options.h | 9 ++++ project2/common/optionsSource.h | 7 ++- project2/common/variables-modconfig.cpp | 12 +++++ project2/console/claOptions.cpp | 89 ++++++++++++++++++--------------- project2/console/consoleEnvironment.cpp | 3 ++ project2/xml/transformHtml.cpp | 4 +- 11 files changed, 168 insertions(+), 66 deletions(-) diff --git a/project2/cgi/cgiEnvironment.cpp b/project2/cgi/cgiEnvironment.cpp index ae7ad52..7b2b254 100644 --- a/project2/cgi/cgiEnvironment.cpp +++ b/project2/cgi/cgiEnvironment.cpp @@ -120,6 +120,12 @@ HostnamePlatformIdentifier::reset() const } } +bool +HostnamePlatformIdentifier::paramRequired() const +{ + return true; +} + void HostnamePlatformIdentifier::consume(const Glib::ustring & p, const Glib::ustring & r) const { diff --git a/project2/cgi/cgiEnvironment.h b/project2/cgi/cgiEnvironment.h index 654f772..28e47d3 100644 --- a/project2/cgi/cgiEnvironment.h +++ b/project2/cgi/cgiEnvironment.h @@ -15,6 +15,7 @@ class HostnamePlatformIdentifier : public Options::Target { HostnamePlatformIdentifier(); virtual ~HostnamePlatformIdentifier(); void reset() const; + bool paramRequired() const; void consume(const Glib::ustring &, const Glib::ustring &) const; const Glib::ustring & derivedPlatform() const; private: diff --git a/project2/cgi/testCgi.cpp b/project2/cgi/testCgi.cpp index c2e8d4e..855e59b 100644 --- a/project2/cgi/testCgi.cpp +++ b/project2/cgi/testCgi.cpp @@ -10,6 +10,20 @@ (name, Options::value(optStore.insert(OptStore::value_type(name, new std::string())).first->second, def), desc) class TestInput : public cgicc::CgiInput { public: + class TestConfigConsumer : public ConfigConsumer { + public: + TestConfigConsumer(const Options * os) : o(os) { + } + void operator()(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const { + o->consume(n, p, v); + } + const Options::Option * get(const Glib::ustring &) const { + return NULL; + } + private: + const Options * o; + }; + typedef std::string * StrPtr; typedef std::map OptStore; OptStore optStore; @@ -45,7 +59,7 @@ class TestInput : public cgicc::CgiInput { ; FileOptions fo(".testCgi.settings"); opts.reset(); - fo.loadInto(boost::bind(&Options::consume, &opts, _1, _2, _3)); + fo.loadInto(TestConfigConsumer(&opts)); } const Options * options() const { diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp index 891f1d9..2047813 100644 --- a/project2/common/environment.cpp +++ b/project2/common/environment.cpp @@ -44,30 +44,40 @@ optionsHelper(AllOptions * ao, const Options * options) } } -static -void -consumeIntoAll(const AllOptions * ao, const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) -{ - BOOST_FOREACH(const Options * o, *ao) { - o->consume(n, p, v); - } -} +class DefaultConfigConsumer : public ConfigConsumer { + public: + void operator()(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const { + BOOST_FOREACH(const Options * o, allOptions) { + o->consume(n, p, v); + } + } + const Options::Option * get(const Glib::ustring & n) const { + BOOST_FOREACH(const Options * os, allOptions) { + const Options::Option * o = os->find(n); + if (o) { + return o; + } + } + return NULL; + } + AllOptions allOptions; +}; void Environment::init() { - AllOptions allOptions; - allOptions.push_back(&commonOptions); - allOptions.push_back(&engineOptions()); - LoaderBase::onAllComponents(boost::bind(optionsHelper, &allOptions, boost::bind(&ComponentLoader::options, _1))); + DefaultConfigConsumer dcc; + dcc.allOptions.push_back(&commonOptions); + dcc.allOptions.push_back(&engineOptions()); + LoaderBase::onAllComponents(boost::bind(optionsHelper, &dcc.allOptions, boost::bind(&ComponentLoader::options, _1))); - BOOST_FOREACH(const AllOptions::value_type & v, allOptions) { + BOOST_FOREACH(const AllOptions::value_type & v, dcc.allOptions) { v->reset(); } typedef std::map > ConfigParsersMap; BOOST_FOREACH(const ConfigParsersMap::value_type & cp, *LoaderBase::objLoaders()) { - cp.second->create()->loadInto(boost::bind(&consumeIntoAll, &allOptions, _1, _2, _3)); + cp.second->create()->loadInto(dcc); } Logger()->clear(); diff --git a/project2/common/options.cpp b/project2/common/options.cpp index a99c78e..8ff7f55 100644 --- a/project2/common/options.cpp +++ b/project2/common/options.cpp @@ -6,42 +6,66 @@ class NamedOption : public Options::Option { public: NamedOption(Glib::ustring const & n, Options::TargetPtr t, Glib::ustring const & d) : - name(n), + id(n), target(t), desc(d) { } void consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const { - if (n == name) { + if (n == id) { target->consume(p, v); } } void reset() const { target->reset(); } + bool accepts(const Glib::ustring & n) const { + return (n == id); + } + bool paramRequired() const { + return target->paramRequired(); + } + Glib::ustring name() const { + return id; + } + Glib::ustring description() const { + return desc; + } - const Glib::ustring name; + const Glib::ustring id; const Options::TargetPtr target; const Glib::ustring desc; }; class OptionAlias : public Options::Option { public: - OptionAlias(const Glib::ustring & a, Options::TargetPtr t) : + OptionAlias(const Glib::ustring & a, const NamedOption * t) : alias(a), target(t) { } void consume(const Glib::ustring & a, const Glib::ustring & p, const Glib::ustring & v) const { if (a == alias) { - target->consume(p, v); + target->target->consume(p, v); } } void reset() const { - target->reset(); + target->target->reset(); + } + bool accepts(const Glib::ustring & n) const { + return (n == alias); + } + bool paramRequired() const { + return target->target->paramRequired(); + } + Glib::ustring name() const { + return alias; + } + Glib::ustring description() const { + return "Alias for " + target->name(); } const Glib::ustring alias; - const Options::TargetPtr target; + const NamedOption * target; }; Options::Options(Glib::ustring const&) @@ -60,7 +84,7 @@ Options::operator()(const Glib::ustring & a) { for (OptionList::const_reverse_iterator i = options.rbegin(); i != options.rend(); i++) { if (const NamedOption * no = dynamic_cast(i->get())) { - options.push_back(new OptionAlias(a, no->target)); + options.push_back(new OptionAlias(a, no)); break; } } @@ -89,6 +113,15 @@ Options::consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::u o->consume(n, p, v); } } +const Options::Option * +Options::find(const Glib::ustring & n) const { + BOOST_FOREACH(const OptionPtr & o, options) { + if (o->accepts(n)) { + return o.get(); + } + } + return NULL; +} Options::InstanceTarget::InstanceTarget() : ts(Default) diff --git a/project2/common/options.h b/project2/common/options.h index 77c71b5..207848a 100644 --- a/project2/common/options.h +++ b/project2/common/options.h @@ -16,6 +16,7 @@ class Options { class Target : public IntrusivePtrBase { public: virtual void reset() const = 0; + virtual bool paramRequired() const = 0; virtual void consume(const Glib::ustring & platform, const Glib::ustring & value) const = 0; }; typedef boost::intrusive_ptr TargetPtr; @@ -23,6 +24,9 @@ class Options { class InstanceTarget : public Target { public: InstanceTarget(); + bool paramRequired() const { + return true; + } void consume(const Glib::ustring & platform, const Glib::ustring & value) const; protected: virtual void assign(const Glib::ustring & value) const = 0; @@ -79,6 +83,10 @@ class Options { class Option : public IntrusivePtrBase { public: virtual void reset() const = 0; + virtual bool paramRequired() const = 0; + virtual Glib::ustring name() const = 0; + virtual Glib::ustring description() const = 0; + virtual bool accepts(const Glib::ustring & name) const = 0; virtual void consume(const Glib::ustring & name, const Glib::ustring & platform, const Glib::ustring & value) const = 0; }; typedef boost::intrusive_ptr