From: Teemu Murtola Date: Fri, 7 Feb 2014 18:38:13 +0000 (+0200) Subject: Make 'gmx -h' invariant wrt. provided options X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=29049f6dee5d171362b49c637ffc2c4edb24878f;p=alexxy%2Fgromacs.git Make 'gmx -h' invariant wrt. provided options 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 --- diff --git a/src/gromacs/commandline/cmdlinehelpmodule.cpp b/src/gromacs/commandline/cmdlinehelpmodule.cpp index 1f957b925c..0e3f43cec7 100644 --- a/src/gromacs/commandline/cmdlinehelpmodule.cpp +++ b/src/gromacs/commandline/cmdlinehelpmodule.cpp @@ -136,21 +136,14 @@ class RootHelpTopic : public CompositeHelpTopic * 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 [] 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) { diff --git a/src/gromacs/commandline/cmdlinehelpmodule.h b/src/gromacs/commandline/cmdlinehelpmodule.h index a0eb12cc74..fa120c732e 100644 --- a/src/gromacs/commandline/cmdlinehelpmodule.h +++ b/src/gromacs/commandline/cmdlinehelpmodule.h @@ -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. * diff --git a/src/gromacs/commandline/cmdlinemodulemanager-impl.h b/src/gromacs/commandline/cmdlinemodulemanager-impl.h index 1d9f1c08f8..b93d684ee3 100644 --- a/src/gromacs/commandline/cmdlinemodulemanager-impl.h +++ b/src/gromacs/commandline/cmdlinemodulemanager-impl.h @@ -49,12 +49,18 @@ #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 CommandLineModuleMap; @@ -65,8 +71,6 @@ typedef std::map 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::type typedef std::vector 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 diff --git a/src/gromacs/commandline/cmdlinemodulemanager.cpp b/src/gromacs/commandline/cmdlinemodulemanager.cpp index c893bcb869..e723e00b0e 100644 --- a/src/gromacs/commandline/cmdlinemodulemanager.cpp +++ b/src/gromacs/commandline/cmdlinemodulemanager.cpp @@ -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();