Make 'gmx -h' invariant wrt. provided options
authorTeemu Murtola <teemu.murtola@gmail.com>
Fri, 7 Feb 2014 18:38:13 +0000 (20:38 +0200)
committerMark Abraham <mark.j.abraham@gmail.com>
Sun, 9 Feb 2014 13:54:34 +0000 (14:54 +0100)
The help output no longer depends on what the user provided on the
command line.  To do this, initialize a separate copy of the options
holder for writing the help.  This decouples the module manager and the
actual help output a bit more.  Make the option holder implementation
shared through cmdlinemodulemanager-impl.h.

Change-Id: I82ea1e969c7d02eadcb2a9f519af30561cb19991

src/gromacs/commandline/cmdlinehelpmodule.cpp
src/gromacs/commandline/cmdlinehelpmodule.h
src/gromacs/commandline/cmdlinemodulemanager-impl.h
src/gromacs/commandline/cmdlinemodulemanager.cpp

index 1f957b925cfc3c96eb83f399e9366e6a3f600ae7..0e3f43cec764e80e618ec51f69af8a93969f6bba 100644 (file)
@@ -136,21 +136,14 @@ class RootHelpTopic : public CompositeHelpTopic<RootHelpText>
          * Does not throw.
          */
         explicit RootHelpTopic(const std::string &binaryName)
-            : binaryName_(binaryName), commonOptions_(NULL)
+            : binaryName_(binaryName)
         {
         }
 
-        //! Sets the common options for the wrapper binary.
-        void setCommonOptions(const Options *options)
-        {
-            commonOptions_ = options;
-        }
-
         virtual void writeHelp(const HelpWriterContext &context) const;
 
     private:
         std::string                 binaryName_;
-        const Options              *commonOptions_;
 
         GMX_DISALLOW_COPY_AND_ASSIGN(RootHelpTopic);
 };
@@ -165,11 +158,13 @@ void RootHelpTopic::writeHelp(const HelpWriterContext &context) const
                           "Root help is not implemented for this output format"));
     }
     {
-        CommandLineHelpContext cmdlineContext(context);
+        CommandLineCommonOptionsHolder optionsHolder;
+        CommandLineHelpContext         cmdlineContext(context);
+        optionsHolder.initOptions();
         cmdlineContext.setModuleDisplayName(binaryName_);
         // TODO: Add <command> [<args>] into the synopsis.
         // TODO: Propagate the -hidden option here.
-        CommandLineHelpWriter(*commonOptions_)
+        CommandLineHelpWriter(*optionsHolder.options())
             .writeHelp(cmdlineContext);
     }
     // TODO: Consider printing a list of "core" commands. Would require someone
@@ -793,11 +788,6 @@ void CommandLineHelpModule::setShowHidden(bool bHidden)
     impl_->bHidden_ = bHidden;
 }
 
-void CommandLineHelpModule::setCommonOptions(const Options *options)
-{
-    impl_->rootTopic_->setCommonOptions(options);
-}
-
 void CommandLineHelpModule::setModuleOverride(
         const CommandLineModuleInterface &module)
 {
index a0eb12cc747a1bd68b3bc928b4c70ae36139d19f..fa120c732e3498e7e384f3a5c89f03727d9f192d 100644 (file)
@@ -53,7 +53,6 @@ namespace gmx
 
 class CommandLineHelpContext;
 class File;
-class Options;
 class ProgramContextInterface;
 
 class CommandLineHelpModuleImpl;
@@ -107,8 +106,6 @@ class CommandLineHelpModule : public CommandLineModuleInterface
         void addTopic(HelpTopicPointer topic);
         //! Sets whether hidden options will be shown in help.
         void setShowHidden(bool bHidden);
-        //! Sets the common options for the wrapper binary.
-        void setCommonOptions(const Options *options);
         /*! \brief
          * Sets an override to show the help for the given module.
          *
index 1d9f1c08f8cbacbd2693addae8ea4fd1f9090f12..b93d684ee3b6692ecb94922198b0a8e290c164a2 100644 (file)
 #include "cmdlinemodule.h"
 #include "cmdlinemodulemanager.h"
 
+#include "gromacs/legacyheaders/copyrite.h"
+#include "gromacs/options/options.h"
 #include "gromacs/utility/common.h"
 #include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/uniqueptr.h"
 
 namespace gmx
 {
+
+//! \addtogroup module_commandline
+//! \{
+
 //! Container type for mapping module names to module objects.
 typedef std::map<std::string, CommandLineModulePointer> CommandLineModuleMap;
 
@@ -65,8 +71,6 @@ typedef std::map<std::string, CommandLineModulePointer> CommandLineModuleMap;
  * This class contains the state of a module group.  CommandLineModuleGroup
  * provides the public interface to construct/alter the state, and
  * CommandLineModuleManager and its associated classes use it for help output.
- *
- * \ingroup module_commandline
  */
 class CommandLineModuleGroupData
 {
@@ -130,6 +134,70 @@ typedef gmx_unique_ptr<CommandLineModuleGroupData>::type
 typedef std::vector<CommandLineModuleGroupDataPointer>
     CommandLineModuleGroupList;
 
+/*! \internal
+ * \brief
+ * Encapsulates some handling of common options to the wrapper binary.
+ */
+class CommandLineCommonOptionsHolder
+{
+    public:
+        CommandLineCommonOptionsHolder();
+        ~CommandLineCommonOptionsHolder();
+
+        //! Initializes the common options.
+        void initOptions();
+        /*! \brief
+         * Finishes option parsing.
+         *
+         * \returns `false` if the wrapper binary should quit without executing
+         *     any module.
+         */
+        bool finishOptions();
+
+        //! Returns the internal Options object.
+        Options *options() { return &options_; }
+        //! Returns the settings for printing startup information.
+        const BinaryInformationSettings &binaryInfoSettings() const
+        {
+            return binaryInfoSettings_;
+        }
+
+        /*! \brief
+         * Returns `true` if common options are set such that the wrapper
+         * binary should quit, without running the actual module.
+         */
+        bool shouldIgnoreActualModule() const
+        {
+            return bHelp_ || bVersion_;
+        }
+        //! Returns whether common options specify showing help.
+        bool shouldShowHelp() const { return bHelp_; }
+        //! Returns whether common options specify showing hidden options in help.
+        bool shouldShowHidden() const { return bHidden_; }
+        //! Returns whether common options specify quiet execution.
+        bool shouldBeQuiet() const
+        {
+            return bQuiet_ && !bVersion_;
+        }
+
+        //! Returns the file to which startup information should be printed.
+        FILE *startupInfoFile() const { return (bVersion_ ? stdout : stderr); }
+
+    private:
+        Options                      options_;
+        //! Settings for what to write in the startup header.
+        BinaryInformationSettings    binaryInfoSettings_;
+        bool                         bHelp_;
+        bool                         bHidden_;
+        bool                         bQuiet_;
+        bool                         bVersion_;
+        bool                         bCopyright_;
+
+        GMX_DISALLOW_COPY_AND_ASSIGN(CommandLineCommonOptionsHolder);
+};
+
+//! \}
+
 } // namespace gmx
 
 #endif
index c893bcb869fc92e3d60c970776ef12e422530bdc..e723e00b0e7b268f54f63f47cefd6b0e2b76a580 100644 (file)
@@ -136,98 +136,49 @@ class CMainCommandLineModule : public CommandLineModuleInterface
 
 };
 
+//! \}
+
+}   // namespace
+
 /********************************************************************
- * CommonOptionsHolder
+ * CommandLineCommonOptionsHolder
  */
 
-/*! \brief
- * Encapsulates some handling of common options to the wrapper binary.
- */
-class CommonOptionsHolder
+CommandLineCommonOptionsHolder::CommandLineCommonOptionsHolder()
+    : options_(NULL, NULL), bHelp_(false), bHidden_(false),
+      bQuiet_(false), bVersion_(false), bCopyright_(true)
 {
-    public:
-        CommonOptionsHolder()
-            : options_(NULL, NULL), bHelp_(false), bHidden_(false),
-              bQuiet_(false), bVersion_(false), bCopyright_(true)
-        {
-            binaryInfoSettings_.copyright(true);
-        }
-
-        //! Initializes the common options.
-        void initOptions()
-        {
-            options_.addOption(BooleanOption("h").store(&bHelp_)
-                                   .description("Print help and quit"));
-            options_.addOption(BooleanOption("hidden").store(&bHidden_)
-                                   .hidden()
-                                   .description("Show hidden options in help"));
-            options_.addOption(BooleanOption("quiet").store(&bQuiet_)
-                                   .description("Do not print common startup info or quotes"));
-            options_.addOption(BooleanOption("version").store(&bVersion_)
-                                   .description("Print extended version information and quit"));
-            options_.addOption(BooleanOption("copyright").store(&bCopyright_)
-                                   .description("Print copyright information on startup"));
-        }
-
-        /*! \brief
-         * Finishes option parsing.
-         *
-         * \returns `false` if the wrapper binary should quit without executing
-         *     any module.
-         */
-        bool finishOptions()
-        {
-            options_.finish();
-            binaryInfoSettings_.extendedInfo(bVersion_);
-            // The latter condition suppresses the copyright with
-            // -quiet -version.
-            binaryInfoSettings_.copyright(bCopyright_ && !bQuiet_);
-            return !bVersion_;
-        }
-
-        //! Returns the internal Options object.
-        Options *options() { return &options_; }
-        //! Returns the settings for printing startup information.
-        const BinaryInformationSettings &binaryInfoSettings() const
-        {
-            return binaryInfoSettings_;
-        }
-
-        /*! \brief
-         * Returns `true` if common options are set such that the wrapper
-         * binary should quit, without running the actual module.
-         */
-        bool shouldIgnoreActualModule() const
-        {
-            return bHelp_ || bVersion_;
-        }
-        //! Returns whether common options specify showing help.
-        bool shouldShowHelp() const { return bHelp_; }
-        //! Returns whether common options specify showing hidden options in help.
-        bool shouldShowHidden() const { return bHidden_; }
-        //! Returns whether common options specify quiet execution.
-        bool shouldBeQuiet() const
-        {
-            return bQuiet_ && !bVersion_;
-        }
-
-        //! Returns the file to which startup information should be printed.
-        FILE *startupInfoFile() const { return (bVersion_ ? stdout : stderr); }
+    binaryInfoSettings_.copyright(true);
+}
 
-    private:
-        Options                      options_;
-        //! Settings for what to write in the startup header.
-        BinaryInformationSettings    binaryInfoSettings_;
-        bool                         bHelp_;
-        bool                         bHidden_;
-        bool                         bQuiet_;
-        bool                         bVersion_;
-        bool                         bCopyright_;
-};
+CommandLineCommonOptionsHolder::~CommandLineCommonOptionsHolder()
+{
+}
 
-//! \}
+void CommandLineCommonOptionsHolder::initOptions()
+{
+    options_.addOption(BooleanOption("h").store(&bHelp_)
+                           .description("Print help and quit"));
+    options_.addOption(BooleanOption("hidden").store(&bHidden_)
+                           .hidden()
+                           .description("Show hidden options in help"));
+    options_.addOption(BooleanOption("quiet").store(&bQuiet_)
+                           .description("Do not print common startup info or quotes"));
+    options_.addOption(BooleanOption("version").store(&bVersion_)
+                           .description("Print extended version information and quit"));
+    options_.addOption(BooleanOption("copyright").store(&bCopyright_)
+                           .description("Print copyright information on startup"));
+}
 
-}   // namespace
+bool CommandLineCommonOptionsHolder::finishOptions()
+{
+    options_.finish();
+    binaryInfoSettings_.extendedInfo(bVersion_);
+    // The latter condition suppresses the copyright with
+    // -quiet -version.
+    binaryInfoSettings_.copyright(bCopyright_ && !bQuiet_);
+    return !bVersion_;
+}
 
 /********************************************************************
  * CommandLineModuleManager::Impl
@@ -312,7 +263,7 @@ class CommandLineModuleManager::Impl
          * arguments that should be passed to it.
          */
         CommandLineModuleInterface *
-        processCommonOptions(CommonOptionsHolder *optionsHolder,
+        processCommonOptions(CommandLineCommonOptionsHolder *optionsHolder,
                              int *argc, char ***argv);
 
         /*! \brief
@@ -410,7 +361,7 @@ CommandLineModuleManager::Impl::findModuleFromBinaryName(
 
 CommandLineModuleInterface *
 CommandLineModuleManager::Impl::processCommonOptions(
-        CommonOptionsHolder *optionsHolder, int *argc, char ***argv)
+        CommandLineCommonOptionsHolder *optionsHolder, int *argc, char ***argv)
 {
     // Check if we are directly invoking a certain module.
     CommandLineModuleInterface *module = singleModule_;
@@ -492,7 +443,6 @@ CommandLineModuleManager::Impl::processCommonOptions(
     if (module == helpModule_)
     {
         helpModule_->setShowHidden(optionsHolder->shouldShowHidden());
-        helpModule_->setCommonOptions(optionsHolder->options());
     }
     return module;
 }
@@ -559,10 +509,10 @@ void CommandLineModuleManager::addHelpTopic(HelpTopicPointer topic)
 
 int CommandLineModuleManager::run(int argc, char *argv[])
 {
-    CommandLineModuleInterface *module;
-    const bool                  bMaster = (!gmx_mpi_initialized() || gmx_node_rank() == 0);
-    bool                        bQuiet  = impl_->bQuiet_ || !bMaster;
-    CommonOptionsHolder         optionsHolder;
+    CommandLineModuleInterface    *module;
+    const bool                     bMaster = (!gmx_mpi_initialized() || gmx_node_rank() == 0);
+    bool                           bQuiet  = impl_->bQuiet_ || !bMaster;
+    CommandLineCommonOptionsHolder optionsHolder;
     try
     {
         optionsHolder.initOptions();