From 9e6f4e4da79ce4943289d7ec792e8d54fca05f96 Mon Sep 17 00:00:00 2001 From: randomdan Date: Sat, 30 Nov 2013 19:19:47 +0000 Subject: 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 --- project2/basics/Jamfile.jam | 2 ++ project2/basics/options/flagSet.cpp | 22 ++++++++++++++++++++++ project2/basics/options/flagSet.h | 19 +++++++++++++++++++ project2/basics/options/showHelp.cpp | 25 +++++++++++++++++++++++++ project2/basics/options/showHelp.h | 19 +++++++++++++++++++ project2/common/componentLoader.h | 9 +++++++++ project2/console/Jamfile.jam | 1 + project2/console/consoleAppEngine.cpp | 24 +++--------------------- project2/daemon/p2daemonAppEngine.cpp | 4 ++++ 9 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 project2/basics/options/flagSet.cpp create mode 100644 project2/basics/options/flagSet.h create mode 100644 project2/basics/options/showHelp.cpp create mode 100644 project2/basics/options/showHelp.h 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 : dl boost_filesystem ../common//p2common + : : + . ; 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 + +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 +#include +#include + +void ShowHelpComponent::onConfigLoad() +{ + if (!showHelp) + return; + fprintf(stdout, "Help\n"); + InstanceSet::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 + +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 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 : . ..//p2parts ../common//p2common + ../basics//p2basics ../cli//p2cli ../../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 +#include #include #include #include @@ -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::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 #include #include +#include +#include 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); -- cgit v1.2.3