diff options
author | randomdan <randomdan@localhost> | 2011-12-16 19:44:00 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-12-16 19:44:00 +0000 |
commit | 9b728d20cfadb185ccbe6df9c553af71155cb348 (patch) | |
tree | 05af7babef544e4832c293d149aa7a33e3181b03 /project2/common | |
parent | Remove straggling references to XML and configuration from recent changes (diff) | |
download | project2-9b728d20cfadb185ccbe6df9c553af71155cb348.tar.bz2 project2-9b728d20cfadb185ccbe6df9c553af71155cb348.tar.xz project2-9b728d20cfadb185ccbe6df9c553af71155cb348.zip |
Improvements to the handling of command line arguments
Diffstat (limited to 'project2/common')
-rw-r--r-- | project2/common/environment.cpp | 38 | ||||
-rw-r--r-- | project2/common/options.cpp | 49 | ||||
-rw-r--r-- | project2/common/options.h | 9 | ||||
-rw-r--r-- | project2/common/optionsSource.h | 7 | ||||
-rw-r--r-- | project2/common/variables-modconfig.cpp | 12 |
5 files changed, 92 insertions, 23 deletions
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<std::string, boost::shared_ptr<OptionsSourceLoader> > ConfigParsersMap; BOOST_FOREACH(const ConfigParsersMap::value_type & cp, *LoaderBase::objLoaders<OptionsSourceLoader>()) { - 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<const NamedOption *>(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<Target> 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<Option> OptionPtr; @@ -101,6 +109,7 @@ class Options { void reset() const; void consume(const Glib::ustring & name, const Glib::ustring & platform, const Glib::ustring & value) const; + const Option * find(const Glib::ustring & name) const; private: typedef std::list<OptionPtr> OptionList; OptionList options; diff --git a/project2/common/optionsSource.h b/project2/common/optionsSource.h index 59d404f..c73a500 100644 --- a/project2/common/optionsSource.h +++ b/project2/common/optionsSource.h @@ -5,8 +5,13 @@ #include <glibmm/ustring.h> #include <intrusivePtrBase.h> #include "scriptLoader.h" +#include "options.h" -typedef boost::function3<void, const Glib::ustring &, const Glib::ustring &, const Glib::ustring &> ConfigConsumer; +class ConfigConsumer { + public: + virtual void operator()(const Glib::ustring &, const Glib::ustring &, const Glib::ustring &) const = 0; + virtual const Options::Option * get(const Glib::ustring & name) const = 0; +}; /// Base class of things that load options class OptionsSource : public IntrusivePtrBase { diff --git a/project2/common/variables-modconfig.cpp b/project2/common/variables-modconfig.cpp index 0dd7feb..3f5e25a 100644 --- a/project2/common/variables-modconfig.cpp +++ b/project2/common/variables-modconfig.cpp @@ -56,6 +56,18 @@ class VariableConfigLoader : public VariableLoaderImpl<VariableConfig> { } } } + bool accepts(const Glib::ustring & n) const { + return (boost::algorithm::starts_with(n, "application.")); + } + bool paramRequired() const { + return true; + } + Glib::ustring name() const { + return "application.*"; + } + Glib::ustring description() const { + return "Load settings into the client application"; + } }; VariableConfigLoader() : opts("Variables - ModConfig options") |