Uniform code path for writing out console help.
authorTeemu Murtola <teemu.murtola@gmail.com>
Sun, 8 Sep 2013 17:34:42 +0000 (20:34 +0300)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Thu, 19 Sep 2013 03:28:15 +0000 (05:28 +0200)
Now both 'gmx help <module>' and 'gmx <module> -h' trigger exactly the
same code path, making things a lot easier to work with.
Some notable things:
 - Moved the responsibility to parse the -hidden argument into the
   wrapper binary, from where it gets passed down in
   CommandLineHelpContext.  Quite a few files are touched by this.
 - The -h option now causes all other options to the module get ignored.
   g_tune_pme requires some other approach to deal with option
   validation, but even adding a separate command-line option for only
   this purpose is probably better than the multiple code paths that
   were there before this change.
 - Related to the above, the help output could be significantly
   simplified, since it no longer depends on the command-line.
 - Removed the -verbose option that caused the options to be printed
   during a normal run.  Possible to add back if people want it, but it
   simplifies things if it isn't needed.

Related to #685 and #969.

Change-Id: Ibe735711f650eafaecf28ffb1ed92da97dcb81b6

16 files changed:
src/gromacs/commandline/cmdlinehelpcontext.cpp
src/gromacs/commandline/cmdlinehelpcontext.h
src/gromacs/commandline/cmdlinehelpwriter.cpp
src/gromacs/commandline/cmdlinehelpwriter.h
src/gromacs/commandline/cmdlinemodulemanager.cpp
src/gromacs/commandline/tests/cmdlinehelpwriter.cpp
src/gromacs/commandline/tests/cmdlinemodulemanager.cpp
src/gromacs/gmxana/gmx_tune_pme.c
src/gromacs/gmxlib/pargs.c
src/gromacs/gmxlib/statutil.cpp
src/gromacs/legacyheaders/readinp.h
src/gromacs/onlinehelp/wman.cpp
src/gromacs/onlinehelp/wman.h
src/gromacs/trajectoryanalysis/cmdlinerunner.cpp
src/gromacs/trajectoryanalysis/runnercommon.cpp
src/gromacs/trajectoryanalysis/runnercommon.h

index 803f41522c496af3e6317c1cacee017dcfd771f8..6c237910f6256c831a1c08b65fca87039c49f1bb 100644 (file)
@@ -69,7 +69,8 @@ class CommandLineHelpContext::Impl
     public:
         //! Creates the implementation class and the low-level context.
         Impl(File *file, HelpOutputFormat format)
-            : writerContext_(file, format), moduleDisplayName_("gmx")
+            : writerContext_(file, format), moduleDisplayName_("gmx"),
+              bHidden_(false)
         {
         }
 
@@ -77,6 +78,8 @@ class CommandLineHelpContext::Impl
         HelpWriterContext  writerContext_;
         //! Display name for the module for which help is written.
         std::string        moduleDisplayName_;
+        //! Whether hidden options should be shown in help output.
+        bool               bHidden_;
 };
 
 CommandLineHelpContext::CommandLineHelpContext(
@@ -94,6 +97,12 @@ void CommandLineHelpContext::setModuleDisplayName(const std::string &name)
     impl_->moduleDisplayName_ = name;
 }
 
+
+void CommandLineHelpContext::setShowHidden(bool bHidden)
+{
+    impl_->bHidden_ = bHidden;
+}
+
 const HelpWriterContext &CommandLineHelpContext::writerContext() const
 {
     return impl_->writerContext_;
@@ -104,6 +113,11 @@ const char *CommandLineHelpContext::moduleDisplayName() const
     return impl_->moduleDisplayName_.c_str();
 }
 
+bool CommandLineHelpContext::showHidden() const
+{
+    return impl_->bHidden_;
+}
+
 /********************************************************************
  * GlobalCommandLineHelpContext
  */
index 6b768e78eafd5d6df017eadfd91adb31548ca323..db1d78bf096cfb3f2fe50f2e2bb6cf871cdcefb0 100644 (file)
@@ -76,6 +76,8 @@ class CommandLineHelpContext
          * \throws std::bad_alloc if out of memory.
          */
         void setModuleDisplayName(const std::string &name);
+        //! Sets whether hidden options should be shown in help output.
+        void setShowHidden(bool bHidden);
 
         //! Returns the lower-level context for writing the help.
         const HelpWriterContext &writerContext() const;
@@ -85,6 +87,8 @@ class CommandLineHelpContext
          * Does not throw.
          */
         const char *moduleDisplayName() const;
+        //! Returns whether hidden options should be shown in help output.
+        bool showHidden() const;
 
     private:
         class Impl;
index 7781fb8e1cb0538f6494ffd3d7743db4f1a17b58..4ebb5b5c467e8a74e9a549c3c0a7d26063bbc209 100644 (file)
@@ -492,13 +492,11 @@ class CommandLineHelpWriter::Impl
         std::string             timeUnit_;
         //! Whether to write descriptions to output.
         bool                    bShowDescriptions_;
-        //! Whether to write hidden options to output.
-        bool                    bShowHidden_;
 };
 
 CommandLineHelpWriter::Impl::Impl(const Options &options)
     : options_(options), timeUnit_(TimeUnitManager().timeUnitAsString()),
-      bShowDescriptions_(false), bShowHidden_(false)
+      bShowDescriptions_(false)
 {
 }
 
@@ -515,12 +513,6 @@ CommandLineHelpWriter::~CommandLineHelpWriter()
 {
 }
 
-CommandLineHelpWriter &CommandLineHelpWriter::setShowHidden(bool bSet)
-{
-    impl_->bShowHidden_ = bSet;
-    return *this;
-}
-
 CommandLineHelpWriter &CommandLineHelpWriter::setShowDescriptions(bool bSet)
 {
     impl_->bShowDescriptions_ = bSet;
@@ -550,7 +542,7 @@ void CommandLineHelpWriter::writeHelp(const CommandLineHelpContext &context)
                               "Command-line help is not implemented for this output format"));
     }
     OptionsFilter filter(writerContext, formatter.get());
-    filter.setShowHidden(impl_->bShowHidden_);
+    filter.setShowHidden(context.showHidden());
 
     if (impl_->bShowDescriptions_)
     {
index 56249f84f154ea84072b2bd1d3a1baa8528e7fac..f0e6c7f1ae957f6322e06198eec104e368a8df79 100644 (file)
@@ -68,10 +68,6 @@ class CommandLineHelpWriter
         explicit CommandLineHelpWriter(const Options &options);
         ~CommandLineHelpWriter();
 
-        /*! \brief
-         * Sets whether hidden options are shown in the help.
-         */
-        CommandLineHelpWriter &setShowHidden(bool bShow);
         /*! \brief
          * Sets whether long descriptions for sections are shown in the help.
          */
index 785507f1662f8f1696867b71d44ea8587cdbc3d0..00e27f7d60e95b40234f5b28e360ce3635c38268 100644 (file)
@@ -74,6 +74,8 @@ namespace gmx
 //! Container type for mapping module names to module objects.
 typedef std::map<std::string, CommandLineModulePointer> CommandLineModuleMap;
 
+class CommandLineHelpModule;
+
 namespace
 {
 
@@ -189,7 +191,7 @@ void RootHelpTopic::printModuleList(const HelpWriterContext &context) const
 }
 
 /********************************************************************
- * ModuleHelpTopic
+ * ModuleHelpTopic declaration
  */
 
 /*! \internal \brief
@@ -205,8 +207,9 @@ class ModuleHelpTopic : public HelpTopicInterface
 {
     public:
         //! Constructs a help topic for a specific module.
-        explicit ModuleHelpTopic(const CommandLineModuleInterface &module)
-            : module_(module)
+        ModuleHelpTopic(const CommandLineModuleInterface &module,
+                        const CommandLineHelpModule      &helpModule)
+            : module_(module), helpModule_(helpModule)
         {
         }
 
@@ -221,17 +224,11 @@ class ModuleHelpTopic : public HelpTopicInterface
 
     private:
         const CommandLineModuleInterface &module_;
+        const CommandLineHelpModule      &helpModule_;
 
         GMX_DISALLOW_COPY_AND_ASSIGN(ModuleHelpTopic);
 };
 
-void ModuleHelpTopic::writeHelp(const HelpWriterContext &context) const
-{
-    CommandLineHelpContext newContext(&context.outputFile(),
-                                      context.outputFormat());
-    module_.writeHelp(newContext);
-}
-
 /********************************************************************
  * HelpExportInterface
  */
@@ -288,6 +285,24 @@ class CommandLineHelpModule : public CommandLineModuleInterface
          * \throws    std::bad_alloc if out of memory.
          */
         void addTopic(HelpTopicPointer topic);
+        //! Sets whether hidden options will be shown in help.
+        void setShowHidden(bool bHidden) { bHidden_ = bHidden; }
+        /*! \brief
+         * Sets an override to show the help for the given module.
+         *
+         * If called, the help module directly prints the help for the given
+         * module when called, skipping any other processing.
+         */
+        void setModuleOverride(const CommandLineModuleInterface &module)
+        {
+            moduleOverride_ = &module;
+        }
+
+        //! Returns the context object for help output.
+        const CommandLineHelpContext &context() const
+        {
+            return *context_;
+        }
 
         virtual const char *name() const { return "help"; }
         virtual const char *shortDescription() const
@@ -304,11 +319,16 @@ class CommandLineHelpModule : public CommandLineModuleInterface
         boost::scoped_ptr<RootHelpTopic>  rootTopic_;
         const CommandLineModuleMap       &modules_;
 
+        CommandLineHelpContext           *context_;
+        const CommandLineModuleInterface *moduleOverride_;
+        bool                              bHidden_;
+
         GMX_DISALLOW_COPY_AND_ASSIGN(CommandLineHelpModule);
 };
 
 CommandLineHelpModule::CommandLineHelpModule(const CommandLineModuleMap &modules)
-    : rootTopic_(new RootHelpTopic(modules)), modules_(modules)
+    : rootTopic_(new RootHelpTopic(modules)), modules_(modules),
+      context_(NULL), moduleOverride_(NULL), bHidden_(false)
 {
 }
 
@@ -335,9 +355,18 @@ int CommandLineHelpModule::run(int argc, char *argv[])
         return 0;
     }
 
-    HelpWriterContext context(&File::standardOutput(),
-                              eHelpOutputFormat_Console);
-    HelpManager       helpManager(*rootTopic_, context);
+    boost::scoped_ptr<CommandLineHelpContext> context(
+            new CommandLineHelpContext(&File::standardOutput(),
+                                       eHelpOutputFormat_Console));
+    context->setShowHidden(bHidden_);
+    context_ = context.get();
+    if (moduleOverride_ != NULL)
+    {
+        ModuleHelpTopic(*moduleOverride_, *this).writeHelp(context->writerContext());
+        return 0;
+    }
+
+    HelpManager       helpManager(*rootTopic_, context->writerContext());
     try
     {
         for (int i = 1; i < argc; ++i)
@@ -389,6 +418,15 @@ void CommandLineHelpModule::exportHelp(HelpExportInterface *exporter) const
 namespace
 {
 
+/********************************************************************
+ * ModuleHelpTopic implementation
+ */
+
+void ModuleHelpTopic::writeHelp(const HelpWriterContext & /*context*/) const
+{
+    module_.writeHelp(helpModule_.context());
+}
+
 /********************************************************************
  * CMainCommandLineModule
  */
@@ -438,20 +476,23 @@ class CMainCommandLineModule : public CommandLineModuleInterface
         virtual void writeHelp(const CommandLineHelpContext &context) const
         {
             const HelpOutputFormat format = context.writerContext().outputFormat();
-            char                  *argv[2];
-            int                    argc = 1;
-            // TODO: The constness should not be cast away.
-            argv[0] = const_cast<char *>(name_);
+            const char            *type;
             switch (format)
             {
                 case eHelpOutputFormat_Console:
-                    argv[1] = const_cast<char *>("-h");
-                    argc    = 2;
+                    type = "help";
                     break;
                 default:
                     GMX_THROW(NotImplementedError(
                                       "Command-line help is not implemented for this output format"));
             }
+            char *argv[4];
+            int   argc = 3;
+            // TODO: The constness should not be cast away.
+            argv[0] = const_cast<char *>(name_);
+            argv[1] = const_cast<char *>("-man");
+            argv[2] = const_cast<char *>(type);
+            argv[3] = NULL;
             GlobalCommandLineHelpContext global(context);
             mainFunction_(argc, argv);
         }
@@ -484,6 +525,21 @@ class CommandLineModuleManager::Impl
          */
         explicit Impl(ProgramInfo *programInfo);
 
+        /*! \brief
+         * Helper method that adds a given module to the module manager.
+         *
+         * \throws    std::bad_alloc if out of memory.
+         */
+        void addModule(CommandLineModulePointer module);
+        /*! \brief
+         * Creates the help module if it does not yet exist.
+         *
+         * \throws    std::bad_alloc if out of memory.
+         *
+         * This method should be called before accessing \a helpModule_.
+         */
+        void ensureHelpModuleExists();
+
         /*! \brief
          * Finds a module that matches a name.
          *
@@ -566,6 +622,26 @@ CommandLineModuleManager::Impl::Impl(ProgramInfo *programInfo)
     binaryInfoSettings_.copyright(true);
 }
 
+void CommandLineModuleManager::Impl::addModule(CommandLineModulePointer module)
+{
+    GMX_ASSERT(modules_.find(module->name()) == modules_.end(),
+               "Attempted to register a duplicate module name");
+    ensureHelpModuleExists();
+    HelpTopicPointer helpTopic(new ModuleHelpTopic(*module, *helpModule_));
+    modules_.insert(std::make_pair(std::string(module->name()),
+                                   move(module)));
+    helpModule_->addTopic(move(helpTopic));
+}
+
+void CommandLineModuleManager::Impl::ensureHelpModuleExists()
+{
+    if (helpModule_ == NULL)
+    {
+        helpModule_ = new CommandLineHelpModule(modules_);
+        addModule(CommandLineModulePointer(helpModule_));
+    }
+}
+
 CommandLineModuleMap::const_iterator
 CommandLineModuleManager::Impl::findModuleByName(const std::string &name) const
 {
@@ -606,23 +682,15 @@ CommandLineModuleManager::Impl::processCommonOptions(int *argc, char ***argv)
     }
 
     bool bHelp      = false;
+    bool bHidden    = false;
     bool bVersion   = false;
     bool bCopyright = true;
     // TODO: Print the common options into the help.
     // TODO: It would be nice to propagate at least the -quiet option to
     // the modules so that they can also be quiet in response to this.
-    // TODO: Consider handling -h and related options here instead of in the
-    // modules (also -hidden needs to be transfered here to make that work).
-    // That would mean that with -h, all module-specific options would get
-    // ignored.  This means that the help output would not depend on the
-    // command line, but would always show the default values (making it
-    // possible to simplify it further), but also that mdrun -h could not be
-    // used for option validation in g_tune_pme.
     Options options(NULL, NULL);
-    if (module == NULL)
-    {
-        options.addOption(BooleanOption("h").store(&bHelp));
-    }
+    options.addOption(BooleanOption("h").store(&bHelp));
+    options.addOption(BooleanOption("hidden").store(&bHidden));
     options.addOption(BooleanOption("quiet").store(&bQuiet_));
     options.addOption(BooleanOption("version").store(&bVersion));
     options.addOption(BooleanOption("copyright").store(&bCopyright));
@@ -661,10 +729,11 @@ CommandLineModuleManager::Impl::processCommonOptions(int *argc, char ***argv)
             // which path is taken: (*argv)[0] is the module name.
         }
     }
-    else
+    if (module != NULL)
     {
-        // In single-module mode, recognize the common options also after the
-        // module name.
+        // Recognize the common options also after the module name.
+        // TODO: It could be nicer to only recognize -h/-hidden if module is not
+        // null.
         CommandLineParser(&options).skipUnknown(true).parse(argc, *argv);
     }
     options.finish();
@@ -678,10 +747,19 @@ CommandLineModuleManager::Impl::processCommonOptions(int *argc, char ***argv)
     }
     // If no module specified and no other action, show the help.
     // Also explicitly specifying -h for the wrapper binary goes here.
-    if (module == NULL)
+    if (module == NULL || bHelp)
     {
-        *argc = 1;
-        return helpModule_;
+        ensureHelpModuleExists();
+        if (module != NULL)
+        {
+            helpModule_->setModuleOverride(*module);
+        }
+        *argc  = 1;
+        module = helpModule_;
+    }
+    if (module == helpModule_)
+    {
+        helpModule_->setShowHidden(bHidden);
     }
     return module;
 }
@@ -706,12 +784,7 @@ void CommandLineModuleManager::setQuiet(bool bQuiet)
 
 void CommandLineModuleManager::addModule(CommandLineModulePointer module)
 {
-    GMX_ASSERT(impl_->modules_.find(module->name()) == impl_->modules_.end(),
-               "Attempted to register a duplicate module name");
-    HelpTopicPointer helpTopic(new ModuleHelpTopic(*module));
-    impl_->modules_.insert(std::make_pair(std::string(module->name()),
-                                          move(module)));
-    addHelpTopic(move(helpTopic));
+    impl_->addModule(move(module));
 }
 
 void CommandLineModuleManager::addModuleCMain(
@@ -725,11 +798,7 @@ void CommandLineModuleManager::addModuleCMain(
 
 void CommandLineModuleManager::addHelpTopic(HelpTopicPointer topic)
 {
-    if (impl_->helpModule_ == NULL)
-    {
-        impl_->helpModule_ = new CommandLineHelpModule(impl_->modules_);
-        addModule(CommandLineModulePointer(impl_->helpModule_));
-    }
+    impl_->ensureHelpModuleExists();
     impl_->helpModule_->addTopic(move(topic));
 }
 
index e05c5780176458fc83a611c838c659bfc1cdc1a1..08ff17aa6f9c56bc749dad92f85685ac045c2c07 100644 (file)
@@ -69,9 +69,12 @@ namespace
 class CommandLineHelpWriterTest : public ::gmx::test::StringTestBase
 {
     public:
+        CommandLineHelpWriterTest() : bHidden_(false) {}
+
         void checkHelp(gmx::CommandLineHelpWriter *writer);
 
         gmx::test::TestFileManager tempFiles_;
+        bool                       bHidden_;
 };
 
 void CommandLineHelpWriterTest::checkHelp(gmx::CommandLineHelpWriter *writer)
@@ -79,6 +82,7 @@ void CommandLineHelpWriterTest::checkHelp(gmx::CommandLineHelpWriter *writer)
     std::string                 filename = tempFiles_.getTemporaryFilePath("helptext.txt");
     gmx::File                   file(filename, "w");
     gmx::CommandLineHelpContext context(&file, gmx::eHelpOutputFormat_Console);
+    context.setShowHidden(bHidden_);
     writer->writeHelp(context);
     file.close();
 
@@ -143,7 +147,7 @@ TEST_F(CommandLineHelpWriterTest, HandlesOptionTypes)
     options.addOption(SelectionOption("sel").description("Selection option"));
 
     CommandLineHelpWriter writer(options);
-    writer.setShowHidden(true);
+    bHidden_ = true;
     checkHelp(&writer);
 }
 
index 8e1f7042fe352904d5a0a63d4a0010ee7ccb9c93..838e5d086b41a59e27f18c80d5e8657e845fa18c 100644 (file)
@@ -175,6 +175,22 @@ TEST_F(CommandLineModuleManagerTest, RunsModuleHelp)
     ASSERT_EQ(0, rc);
 }
 
+TEST_F(CommandLineModuleManagerTest, RunsModuleHelpWithDashH)
+{
+    const char *const cmdline[] = {
+        "g_test", "module", "-h"
+    };
+    CommandLine       args(CommandLine::create(cmdline));
+    initManager(args);
+    MockModule       &mod1 = addModule("module", "First module");
+    addModule("other", "Second module");
+    using ::testing::_;
+    EXPECT_CALL(mod1, writeHelp(_));
+    int rc = 0;
+    ASSERT_NO_THROW_GMX(rc = manager().run(args.argc(), args.argv()));
+    ASSERT_EQ(0, rc);
+}
+
 TEST_F(CommandLineModuleManagerTest, PrintsHelpOnTopic)
 {
     const char *const cmdline[] = {
index d0841d057e72434e0d10127ba8724ea57c11379f..f9af7ce888479eaa88d6c91b142520754021f617 100644 (file)
@@ -1305,7 +1305,9 @@ static void make_sure_it_runs(char *mdrun_cmd_line, int length, FILE *fp)
     snew(command, length +  15);
     snew(msg, length + 500);
 
-    fprintf(stdout, "Making shure the benchmarks can be executed ...\n");
+    fprintf(stdout, "Making sure the benchmarks can be executed ...\n");
+    /* FIXME: mdrun -h no longer actually does anything useful.
+     * It unconditionally prints the help, ignoring all other options. */
     sprintf(command, "%s-h -quiet", mdrun_cmd_line);
     ret = gmx_system_call(command);
 
index ff41a16151603a0df3f15b855b404fb540f9b5b1..6206985ea51d06f62af0dc4effdc96050765f1c7 100644 (file)
@@ -352,7 +352,7 @@ char *pa_val(t_pargs *pa, char buf[], int sz)
 #define OPTLEN 12
 #define TYPELEN 6
 #define LONGSTR 1024
-char *pargs_print_line(t_pargs *pa, gmx_bool bLeadingSpace)
+static char *pargs_print_line(t_pargs *pa)
 {
     char buf[LONGSTR], *buf2, *tmp, *desc;
 
@@ -370,22 +370,19 @@ char *pargs_print_line(t_pargs *pa, gmx_bool bLeadingSpace)
     desc = check_tty(pa->desc);
     if (strlen(buf) > ((OPTLEN+TYPELEN)-max(strlen(get_arg_desc(pa->type)), 4)))
     {
-        sprintf(buf2, "%s%s %-6s %-6s  %-s\n",
-                bLeadingSpace ? " " : "", buf,
-                get_arg_desc(pa->type), pa_val(pa, tmp, LONGSTR-1), desc);
+        sprintf(buf2, "%s %-6s %-6s  %-s\n",
+                buf, get_arg_desc(pa->type), pa_val(pa, tmp, LONGSTR-1), desc);
     }
     else if (strlen(buf) > OPTLEN)
     {
         /* so type can be 3 or 4 char's, this fits in the %4s */
-        sprintf(buf2, "%s%-14s %-4s %-6s  %-s\n",
-                bLeadingSpace ? " " : "", buf, get_arg_desc(pa->type),
-                pa_val(pa, tmp, LONGSTR-1), desc);
+        sprintf(buf2, "%-14s %-4s %-6s  %-s\n",
+                buf, get_arg_desc(pa->type), pa_val(pa, tmp, LONGSTR-1), desc);
     }
     else
     {
-        sprintf(buf2, "%s%-12s %-6s %-6s  %-s\n",
-                bLeadingSpace ? " " : "", buf, get_arg_desc(pa->type),
-                pa_val(pa, tmp, LONGSTR-1), desc);
+        sprintf(buf2, "%-12s %-6s %-6s  %-s\n",
+                buf, get_arg_desc(pa->type), pa_val(pa, tmp, LONGSTR-1), desc);
     }
     sfree(desc);
     sfree(tmp);
@@ -397,39 +394,23 @@ char *pargs_print_line(t_pargs *pa, gmx_bool bLeadingSpace)
     return tmp;
 }
 
-void print_pargs(FILE *fp, int npargs, t_pargs pa[], gmx_bool bLeadingSpace)
+void print_pargs(FILE *fp, int npargs, t_pargs pa[])
 {
-    gmx_bool bShowHidden;
     char     buf[32], buf2[256], tmp[256];
     char    *wdesc;
     int      i;
 
-    /* Cannot call opt2parg_bSet here, because it crashes when the option
-     * is not in the list (mdrun)
-     */
-    bShowHidden = FALSE;
-    for (i = 0; (i < npargs); i++)
-    {
-        if ((strcmp(pa[i].option, "-hidden") == 0) && (pa[i].bSet))
-        {
-            bShowHidden = TRUE;
-        }
-    }
 
     if (npargs > 0)
     {
-        fprintf(fp, "%s%-12s %-6s %-6s  %-s\n",
-                bLeadingSpace ? " " : "", "Option", "Type", "Value", "Description");
-        fprintf(fp, "%s------------------------------------------------------\n",
-                bLeadingSpace ? " " : "");
+        fprintf(fp, "%-12s %-6s %-6s  %-s\n",
+                "Option", "Type", "Value", "Description");
+        fprintf(fp, "------------------------------------------------------\n");
         for (i = 0; (i < npargs); i++)
         {
-            if (bShowHidden || !is_hidden(&pa[i]))
-            {
-                wdesc = pargs_print_line(&pa[i], bLeadingSpace);
-                fprintf(fp, "%s", wdesc);
-                sfree(wdesc);
-            }
+            wdesc = pargs_print_line(&pa[i]);
+            fprintf(fp, "%s", wdesc);
+            sfree(wdesc);
         }
         fprintf(fp, "\n");
     }
index e72c7c6036efcb7c9a673b8b6a5e8304a6d1c696..7e1ca8194d5472ab1bf4f3d0854c1eb53c9ab4db 100644 (file)
@@ -466,9 +466,8 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags,
                            int nbugs, const char **bugs,
                            output_env_t *oenv)
 {
-    gmx_bool    bHelp    = FALSE, bHidden = FALSE, bVerbose = FALSE;
     const char *manstr[] = {
-        NULL, "no", "html", "tex", "nroff", "completion", NULL
+        NULL, "no", "help", "html", "tex", "nroff", "completion", NULL
     };
     /* This array should match the order of the enum in oenv.h */
     const char *xvg_format[] = { NULL, "xmgrace", "xmgr", "none", NULL };
@@ -527,12 +526,6 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags,
 #define EXTRA_PA 16
 
     t_pargs  pca_pa[] = {
-        { "-h",    FALSE, etBOOL, {&bHelp},
-          "Print help info and quit" },
-        { "-verbose",  FALSE, etBOOL, {&bVerbose},
-          "HIDDENShow options during normal run" },
-        { "-hidden", FALSE, etBOOL, {&bHidden},
-          "HIDDENPrint hidden options" },
         { "-man",  FALSE, etENUM,  {manstr},
           "HIDDENWrite manual and quit" },
         { "-debug", FALSE, etINT, {&debug_level},
@@ -703,7 +696,10 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags,
         all_pa[i].desc = mk_desc(&(all_pa[i]), output_env_get_time_unit(*oenv));
     }
 
-    bExit = bHelp || (strcmp(manstr[0], "no") != 0);
+    // To satisfy clang.
+    GMX_ASSERT(manstr[0] != NULL,
+               "Enum option default assignment should have changed this");
+    bExit = (strcmp(manstr[0], "no") != 0);
 
 #if (defined __sgi && USE_SGI_FPE)
     doexceptions();
@@ -738,36 +734,22 @@ gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags,
 #endif
 #endif
 
-    if (!(FF(PCA_QUIET)))
-    {
-        if (bHelp)
-        {
-            write_man("help", output_env_get_program_name(*oenv),
-                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs, bHidden);
-        }
-        else if (bVerbose)
-        {
-            pr_fns(stderr, nfile, fnm);
-            print_pargs(stderr, npall, all_pa, FALSE);
-        }
-    }
-
-    if (strcmp(manstr[0], "no") != 0)
+    if (strcmp(manstr[0], "no") != 0 && !(FF(PCA_QUIET)))
     {
         if (!strcmp(manstr[0], "completion"))
         {
             /* one file each for csh, bash and zsh if we do completions */
             write_man("completion-zsh", output_env_get_short_program_name(*oenv),
-                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs, bHidden);
+                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
             write_man("completion-bash", output_env_get_short_program_name(*oenv),
-                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs, bHidden);
+                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
             write_man("completion-csh", output_env_get_short_program_name(*oenv),
-                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs, bHidden);
+                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
         }
         else
         {
             write_man(manstr[0], output_env_get_short_program_name(*oenv),
-                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs, bHidden);
+                      ndesc, desc, nfile, fnm, npall, all_pa, nbugs, bugs);
         }
     }
 
index 5bfc294ad73ca6562536cc98ecb3497e7f5efe45..1bd859ecb09be77fa3fa7bdbc92a1342c3038317 100644 (file)
@@ -162,9 +162,7 @@ const char *opt2parg_enum(const char *option, int nparg, t_pargs pa[]);
 
 gmx_bool opt2parg_bSet(const char *option, int nparg, t_pargs pa[]);
 
-void print_pargs(FILE *fp, int npargs, t_pargs pa[], gmx_bool bLeadingSpace);
-
-char *pargs_print_line(t_pargs *pa, gmx_bool bLeadingSpace);
+void print_pargs(FILE *fp, int npargs, t_pargs pa[]);
 
 void pr_enums(FILE *fp, int npargs, t_pargs pa[], int shell);
 
index 603c78d646389b12fb961e7d2a869e8d6097be01..4e9e2e848d03fd876b91d19c8b5ec61e44ecb2ff 100644 (file)
@@ -745,7 +745,7 @@ static void write_ttyman(FILE *out,
     }
     if (npargs > 0)
     {
-        print_pargs(out, npargs, pa, FALSE);
+        print_pargs(out, npargs, pa);
     }
 }
 
@@ -986,10 +986,10 @@ void write_man(const char *mantp,
                int nldesc, const char **desc,
                int nfile, t_filenm *fnm,
                int npargs, t_pargs *pa,
-               int nbug, const char **bugs,
-               gmx_bool bHidden)
+               int nbug, const char **bugs)
 {
-    int         i, npar;
+    bool        bHidden = false;
+    int         npar;
     t_pargs    *par;
 
     t_linkdata *links;
@@ -1002,11 +1002,8 @@ void write_man(const char *mantp,
     FILE *out;
     if (context != NULL)
     {
-        out = context->writerContext().outputFile().handle();
-    }
-    else if (strcmp(mantp, "help") == 0)
-    {
-        out = stderr;
+        out     = context->writerContext().outputFile().handle();
+        bHidden = context->showHidden();
     }
     else
     {
@@ -1025,7 +1022,7 @@ void write_man(const char *mantp,
     {
         snew(par, npargs);
         npar = 0;
-        for (i = 0; i < npargs; i++)
+        for (int i = 0; i < npargs; i++)
         {
             if (!is_hidden(&pa[i]))
             {
index 82bdccbc4fe097ac47d961642adc65c576529a8b..e2201ab687c167c191bbd08779ebb3af8a280fa1 100644 (file)
@@ -45,8 +45,7 @@ void write_man(const char *mantp, const char *program,
                int nldesc, const char **desc,
                int nfile, t_filenm *fnm,
                int npargs, t_pargs *pa,
-               int nbug, const char **bugs,
-               gmx_bool bHidden);
+               int nbug, const char **bugs);
 
 char *fileopt(unsigned long flag, char buf[], int maxsize);
 /* Return a string describing the file type in flag.
index ee5875bfb1ad3203d407ba0a4ec52fbc4357a750..667b2a450040af66d240168db03f821a7a540fcc 100644 (file)
@@ -55,7 +55,6 @@
 #include "gromacs/commandline/cmdlinemodule.h"
 #include "gromacs/commandline/cmdlinemodulemanager.h"
 #include "gromacs/commandline/cmdlineparser.h"
-#include "gromacs/onlinehelp/helpwritercontext.h"
 #include "gromacs/options/options.h"
 #include "gromacs/selection/selectioncollection.h"
 #include "gromacs/selection/selectionoptionmanager.h"
@@ -82,9 +81,6 @@ class TrajectoryAnalysisCommandLineRunner::Impl
         Impl(TrajectoryAnalysisModule *module);
         ~Impl();
 
-        void printHelp(const Options                        &options,
-                       const TrajectoryAnalysisSettings     &settings,
-                       const TrajectoryAnalysisRunnerCommon &common);
         bool parseOptions(TrajectoryAnalysisSettings *settings,
                           TrajectoryAnalysisRunnerCommon *common,
                           SelectionCollection *selections,
@@ -107,26 +103,6 @@ TrajectoryAnalysisCommandLineRunner::Impl::~Impl()
 }
 
 
-void
-TrajectoryAnalysisCommandLineRunner::Impl::printHelp(
-        const Options                        &options,
-        const TrajectoryAnalysisSettings     &settings,
-        const TrajectoryAnalysisRunnerCommon &common)
-{
-    TrajectoryAnalysisRunnerCommon::HelpFlags flags = common.helpFlags();
-    if (flags != 0)
-    {
-        CommandLineHelpContext context(&File::standardError(),
-                                       eHelpOutputFormat_Console);
-        CommandLineHelpWriter(options)
-            .setShowDescriptions(flags & TrajectoryAnalysisRunnerCommon::efHelpShowDescriptions)
-            .setShowHidden(flags & TrajectoryAnalysisRunnerCommon::efHelpShowHidden)
-            .setTimeUnitString(settings.timeUnitManager().timeUnitAsString())
-            .writeHelp(context);
-    }
-}
-
-
 bool
 TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
         TrajectoryAnalysisSettings *settings,
@@ -151,24 +127,14 @@ TrajectoryAnalysisCommandLineRunner::Impl::parseOptions(
 
     {
         CommandLineParser  parser(&options);
-        try
-        {
-            parser.parse(argc, argv);
-        }
-        catch (const UserInputError &ex)
-        {
-            printHelp(options, *settings, *common);
-            throw;
-        }
-        printHelp(options, *settings, *common);
+        // TODO: Print the help if user provides an invalid option?
+        // Or just add a message advising the user to invoke the help?
+        parser.parse(argc, argv);
         common->scaleTimeOptions(&options);
         options.finish();
     }
 
-    if (!common->optionsFinished(&commonOptions))
-    {
-        return false;
-    }
+    common->optionsFinished(&commonOptions);
     module_->optionsFinished(&moduleOptions, settings);
 
     common->initIndexGroups(selections);
@@ -283,7 +249,7 @@ TrajectoryAnalysisCommandLineRunner::run(int argc, char *argv[])
 void
 TrajectoryAnalysisCommandLineRunner::writeHelp(const CommandLineHelpContext &context)
 {
-    // TODO: This method duplicates some code from run() and Impl::printHelp().
+    // TODO: This method duplicates some code from run().
     // See how to best refactor it to share the common code.
     SelectionCollection             selections;
     TrajectoryAnalysisSettings      settings;
index 3b2006eb81880d85ce7f4367beaaf6afc4878ff7..889df25daa4f9b755bea55be54380ff3f457d5bd 100644 (file)
@@ -81,9 +81,6 @@ class TrajectoryAnalysisRunnerCommon::Impl
         TrajectoryAnalysisSettings &settings_;
         TopologyInformation         topInfo_;
 
-        bool                        bHelp_;
-        bool                        bShowHidden_;
-        bool                        bVerbose_;
         //! Name of the trajectory file (empty if not provided).
         std::string                 trjfile_;
         //! Name of the topology file (empty if no topology provided).
@@ -107,7 +104,6 @@ class TrajectoryAnalysisRunnerCommon::Impl
 
 TrajectoryAnalysisRunnerCommon::Impl::Impl(TrajectoryAnalysisSettings *settings)
     : settings_(*settings),
-      bHelp_(false), bShowHidden_(false), bVerbose_(false),
       startTime_(0.0), endTime_(0.0), deltaTime_(0.0),
       grps_(NULL),
       bTrajOpen_(false), fr(NULL), gpbc_(NULL), status_(NULL), oenv_(NULL)
@@ -173,16 +169,6 @@ TrajectoryAnalysisRunnerCommon::initOptions(Options *options)
 {
     TrajectoryAnalysisSettings &settings = impl_->settings_;
 
-    // Add options for help.
-    options->addOption(BooleanOption("h").store(&impl_->bHelp_)
-                           .description("Print help and quit"));
-    options->addOption(BooleanOption("hidden").store(&impl_->bShowHidden_)
-                           .hidden()
-                           .description("Show hidden options"));
-    options->addOption(BooleanOption("verbose").store(&impl_->bVerbose_)
-                           .hidden()
-                           .description("Show options during normal run"));
-
     // Add common file name arguments.
     options->addOption(FileNameOption("f")
                            .filetype(eftTrajectory).inputFile()
@@ -236,14 +222,9 @@ TrajectoryAnalysisRunnerCommon::scaleTimeOptions(Options *options)
 }
 
 
-bool
+void
 TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
 {
-    if (impl_->bHelp_)
-    {
-        return false;
-    }
-
     impl_->settings_.impl_->plotSettings.setTimeUnit(
             impl_->settings_.impl_->timeUnitManager.timeUnit());
 
@@ -264,8 +245,6 @@ TrajectoryAnalysisRunnerCommon::optionsFinished(Options *options)
     {
         setTimeValue(TDELTA, impl_->deltaTime_);
     }
-
-    return true;
 }
 
 
@@ -448,27 +427,6 @@ TrajectoryAnalysisRunnerCommon::initFrame()
 }
 
 
-TrajectoryAnalysisRunnerCommon::HelpFlags
-TrajectoryAnalysisRunnerCommon::helpFlags() const
-{
-    HelpFlags flags = 0;
-
-    if (impl_->bVerbose_)
-    {
-        flags |= efHelpShowOptions;
-    }
-    if (impl_->bHelp_)
-    {
-        flags |= efHelpShowOptions;
-        flags |= efHelpShowDescriptions;
-    }
-    if (flags != 0 && impl_->bShowHidden_)
-    {
-        flags |= efHelpShowHidden;
-    }
-    return flags;
-}
-
 bool
 TrajectoryAnalysisRunnerCommon::hasTrajectory() const
 {
index 9a9c92e94c32cc27a480d61be13ecfa07ff7c443..8c0b0958fe1db67c7b30f56dcb4b7b7cb19b52ff 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
  * others, as listed in the AUTHORS file in the top-level source
  * directory and at http://www.gromacs.org.
@@ -66,18 +66,6 @@ class TrajectoryAnalysisSettings;
 class TrajectoryAnalysisRunnerCommon
 {
     public:
-        /*! \brief
-         * Flags that define what kind of help should be printed.
-         */
-        enum HelpFlag
-        {
-            efHelpShowOptions           = 1<<0, //!< Show options.
-            efHelpShowHidden            = 1<<1, //!< Show hidden options.
-            efHelpShowDescriptions      = 1<<2  //!< Show detailed description.
-        };
-        //! Combination of \ref HelpFlag values.
-        typedef unsigned long HelpFlags;
-
         /*! \brief
          * Initializes a new runner helper.
          *
@@ -98,9 +86,8 @@ class TrajectoryAnalysisRunnerCommon
          * Processes common option values after they have been parsed.
          *
          * \param[in,out] options Options object in which options are stored.
-         * \returns       false if the tool should exit after printing help.
          */
-        bool optionsFinished(Options *options);
+        void optionsFinished(Options *options);
         //! Initialize index groups for selections.
         void initIndexGroups(SelectionCollection *selections);
         //! Free memory allocated for index groups.
@@ -128,8 +115,6 @@ class TrajectoryAnalysisRunnerCommon
          */
         void initFrame();
 
-        //! Returns flags for help printing.
-        HelpFlags helpFlags() const;
         //! Returns true if input data comes from a trajectory.
         bool hasTrajectory() const;
         //! Returns the topology information object.