summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2013-11-30 19:19:47 +0000
committerrandomdan <randomdan@localhost>2013-11-30 19:19:47 +0000
commit614a474bf4a68007d4f66cfe5424b0e5478e88a7 (patch)
tree29fb14232ad0f74e0bfd88c611cdd9015107bac7
parentSupport for options registering new options sources (diff)
downloadproject2-614a474bf4a68007d4f66cfe5424b0e5478e88a7.tar.bz2
project2-614a474bf4a68007d4f66cfe5424b0e5478e88a7.tar.xz
project2-614a474bf4a68007d4f66cfe5424b0e5478e88a7.zip
Allow registration of a standalone component
Move help display into basics lib Use onConfigLoad to trigger help after all options have been parsed (enables showing help of libraries defined in config files) Create a trivial options target for setting a boolean flag Add help support to p2daemon
-rw-r--r--project2/basics/Jamfile.jam2
-rw-r--r--project2/basics/options/flagSet.cpp22
-rw-r--r--project2/basics/options/flagSet.h19
-rw-r--r--project2/basics/options/showHelp.cpp25
-rw-r--r--project2/basics/options/showHelp.h19
-rw-r--r--project2/common/componentLoader.h9
-rw-r--r--project2/console/Jamfile.jam1
-rw-r--r--project2/console/consoleAppEngine.cpp24
-rw-r--r--project2/daemon/p2daemonAppEngine.cpp4
9 files changed, 104 insertions, 21 deletions
diff --git a/project2/basics/Jamfile.jam b/project2/basics/Jamfile.jam
index 02de4c3..d64a617 100644
--- a/project2/basics/Jamfile.jam
+++ b/project2/basics/Jamfile.jam
@@ -20,5 +20,7 @@ lib p2basics :
<library>dl
<library>boost_filesystem
<library>../common//p2common
+ : :
+ <include>.
;
diff --git a/project2/basics/options/flagSet.cpp b/project2/basics/options/flagSet.cpp
new file mode 100644
index 0000000..bfb905e
--- /dev/null
+++ b/project2/basics/options/flagSet.cpp
@@ -0,0 +1,22 @@
+#include "flagSet.h"
+
+OptionFlagSet::OptionFlagSet(bool * target) :
+ targetFlag(target)
+{
+}
+
+bool OptionFlagSet::paramRequired() const
+{
+ return false;
+}
+
+void OptionFlagSet::reset() const
+{
+ *targetFlag = false;
+}
+
+void OptionFlagSet::consume(const Glib::ustring &, const VariableType &, const Options::CurrentPlatform &) const
+{
+ *targetFlag = true;
+}
+
diff --git a/project2/basics/options/flagSet.h b/project2/basics/options/flagSet.h
new file mode 100644
index 0000000..0e7830f
--- /dev/null
+++ b/project2/basics/options/flagSet.h
@@ -0,0 +1,19 @@
+#ifndef OPTIONS_FLAGSET_H
+#define OPTIONS_FLAGSET_H
+
+#include <options.h>
+
+class OptionFlagSet : public Options::Target {
+ public:
+ OptionFlagSet(bool * target);
+
+ void reset() const;
+ bool paramRequired() const;
+ void consume(const Glib::ustring &, const VariableType &, const Options::CurrentPlatform &) const;
+
+ private:
+ bool * const targetFlag;
+};
+
+#endif
+
diff --git a/project2/basics/options/showHelp.cpp b/project2/basics/options/showHelp.cpp
new file mode 100644
index 0000000..feabd45
--- /dev/null
+++ b/project2/basics/options/showHelp.cpp
@@ -0,0 +1,25 @@
+#include "showHelp.h"
+#include <options.h>
+#include <boost/bind.hpp>
+#include <boost/foreach.hpp>
+
+void ShowHelpComponent::onConfigLoad()
+{
+ if (!showHelp)
+ return;
+ fprintf(stdout, "Help\n");
+ InstanceSet<Options>::OnAll(boost::bind(&ShowHelpComponent::outputOptions, this, _1));
+ exit(1);
+}
+
+void ShowHelpComponent::outputOptions(const Options * options) const
+{
+ fprintf(stdout, " * %s\n", options->name.c_str());
+ BOOST_FOREACH(const auto & option, options->allOptions()) {
+ fprintf(stdout, " * %s - %s\n", option->name().c_str(), option->description().c_str());
+ }
+}
+
+bool ShowHelpComponent::showHelp;
+DECLARE_COMPONENT("ShowHelpComponent", ShowHelpComponent);
+
diff --git a/project2/basics/options/showHelp.h b/project2/basics/options/showHelp.h
new file mode 100644
index 0000000..e1b95ef
--- /dev/null
+++ b/project2/basics/options/showHelp.h
@@ -0,0 +1,19 @@
+#ifndef SHOWHELP_H
+#define SHOWHELP_H
+
+#include <componentLoader.h>
+
+class Options;
+
+class ShowHelpComponent : public ComponentLoader {
+ public:
+ void onConfigLoad();
+ private:
+ void outputOptions(const Options * options) const;
+
+ public:
+ static bool showHelp;
+};
+
+#endif
+
diff --git a/project2/common/componentLoader.h b/project2/common/componentLoader.h
index 5d14efe..45664e3 100644
--- a/project2/common/componentLoader.h
+++ b/project2/common/componentLoader.h
@@ -1,6 +1,8 @@
#ifndef COMPONENTLOADER_H
#define COMPONENTLOADER_H
+#include "plugable.h"
+
/// Helper for loading and maintaining Project2 components
class ComponentLoader {
public:
@@ -14,5 +16,12 @@ class ComponentLoader {
virtual bool cacheable() const { return true; } // The component can be cached for next run
};
+typedef PluginsSameBase<ComponentLoader, std::string> Components;
+#define DECLARE_COMPONENT(Id, Inst) \
+ static void init_optionsSource_##Type() __attribute__ ((constructor(200))); \
+ static void init_optionsSource_##Type() { Components::Add(Id, new Inst()); } \
+ static void kill_optionsSource_##Type() __attribute__ ((destructor(200))); \
+ static void kill_optionsSource_##Type() { Components::Remove(Id); }
+
#endif
diff --git a/project2/console/Jamfile.jam b/project2/console/Jamfile.jam
index 260de3e..24d8504 100644
--- a/project2/console/Jamfile.jam
+++ b/project2/console/Jamfile.jam
@@ -14,6 +14,7 @@ exe p2console :
<include>.
<library>..//p2parts
<library>../common//p2common
+ <library>../basics//p2basics
<library>../cli//p2cli
<include>../../libmisc
;
diff --git a/project2/console/consoleAppEngine.cpp b/project2/console/consoleAppEngine.cpp
index 98e5d4d..8aa4fc0 100644
--- a/project2/console/consoleAppEngine.cpp
+++ b/project2/console/consoleAppEngine.cpp
@@ -8,6 +8,8 @@
#include "viewHost.h"
#include "taskHost.h"
#include "scriptLoader.h"
+#include <options/flagSet.h>
+#include <options/showHelp.h>
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
#include <boost/uuid/uuid_generators.hpp>
@@ -16,28 +18,8 @@
StaticMessageException(InvalidScriptName, "Script name should be group/name");
SimpleMessageException(UnknownPlatformAlias);
-class ShowHelpTrigger : public Options::Target {
- public:
- void reset() const { }
- bool paramRequired() const {
- return false;
- }
- void consume(const Glib::ustring &, const VariableType &, const Options::CurrentPlatform &) const {
- fprintf(stdout, "Help\n");
- InstanceSet<Options>::OnAll(boost::bind(&ShowHelpTrigger::outputOptions, this, _1));
- exit(1);
- }
- private:
- void outputOptions(const Options * options) const {
- fprintf(stdout, " * %s\n", options->name.c_str());
- BOOST_FOREACH(const auto & option, options->allOptions()) {
- fprintf(stdout, " * %s - %s\n", option->name().c_str(), option->description().c_str());
- }
- }
-};
-
DECLARE_OPTIONS(ConsoleApplicationEngine, "Console options")
-("help", new ShowHelpTrigger(),
+("help", new OptionFlagSet(&ShowHelpComponent::showHelp),
"Print usage and exit")("h")
("console.platform", Options::value(&reqPlatform),
"Platform")("p")
diff --git a/project2/daemon/p2daemonAppEngine.cpp b/project2/daemon/p2daemonAppEngine.cpp
index 7daed70..2330b88 100644
--- a/project2/daemon/p2daemonAppEngine.cpp
+++ b/project2/daemon/p2daemonAppEngine.cpp
@@ -2,12 +2,16 @@
#include <logger.h>
#include <optionsSource.h>
#include <dlfcn.h>
+#include <options/flagSet.h>
+#include <options/showHelp.h>
std::string DaemonAppEngine::daemonType;
Glib::ustring DaemonAppEngine::reqPlatform;
DaemonPtr DaemonAppEngine::daemon;
DECLARE_OPTIONS(DaemonAppEngine, "Project2 Daemon options")
+("help", new OptionFlagSet(&ShowHelpComponent::showHelp),
+ "Print usage and exit")("h")
("daemon.type", Options::value(&daemonType), "The core daemon module to run")
("daemon.platform", Options::value(&reqPlatform), "Platform")
END_OPTIONS(DaemonAppEngine);