Shell completion export from the wrapper binary
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlinehelpmodule.cpp
index d4746133a685431b1b966cc477510c8f5e5c3f2d..9ae14c958cb7f55405ab9800735e1e3d6ec12054 100644 (file)
@@ -43,6 +43,7 @@
 
 #include <algorithm>
 #include <string>
+#include <vector>
 
 #include <boost/scoped_ptr.hpp>
 
@@ -50,6 +51,7 @@
 
 #include "gromacs/commandline/cmdlinehelpcontext.h"
 #include "gromacs/commandline/cmdlineparser.h"
+#include "gromacs/commandline/shellcompletions.h"
 #include "gromacs/onlinehelp/helpformat.h"
 #include "gromacs/onlinehelp/helpmanager.h"
 #include "gromacs/onlinehelp/helptopic.h"
@@ -597,6 +599,70 @@ void HelpExportHtml::writeHtmlFooter(File *file) const
     file->writeLine(footer_);
 }
 
+/********************************************************************
+ * HelpExportCompletion
+ */
+
+/*! \internal \brief
+ * Implements export for command-line completion.
+ *
+ * \ingroup module_commandline
+ */
+class HelpExportCompletion : public HelpExportInterface
+{
+    public:
+        //! Initializes completion exporter.
+        explicit HelpExportCompletion(const CommandLineHelpModuleImpl &helpModule);
+
+        virtual void startModuleExport();
+        virtual void exportModuleHelp(
+            const CommandLineModuleInterface &module,
+            const std::string                &tag,
+            const std::string                &displayName);
+        virtual void finishModuleExport();
+
+        virtual void startModuleGroupExport() {}
+        virtual void exportModuleGroup(const char                * /*title*/,
+                                       const ModuleGroupContents & /*modules*/) {}
+        virtual void finishModuleGroupExport() {}
+
+    private:
+        ShellCompletionWriter    bashWriter_;
+        std::vector<std::string> modules_;
+};
+
+HelpExportCompletion::HelpExportCompletion(
+        const CommandLineHelpModuleImpl &helpModule)
+    : bashWriter_(helpModule.binaryName_, eShellCompletionFormat_Bash)
+{
+}
+
+void HelpExportCompletion::startModuleExport()
+{
+    bashWriter_.startCompletions();
+}
+
+void HelpExportCompletion::exportModuleHelp(
+        const CommandLineModuleInterface &module,
+        const std::string                 & /*tag*/,
+        const std::string                 & /*displayName*/)
+{
+    modules_.push_back(module.name());
+    {
+        CommandLineHelpContext context(&bashWriter_);
+        // We use the display name to pass the name of the module to the
+        // completion writer.
+        context.setModuleDisplayName(module.name());
+        module.writeHelp(context);
+    }
+}
+
+void HelpExportCompletion::finishModuleExport()
+{
+    bashWriter_.writeWrapperCompletions(modules_);
+    bashWriter_.finishCompletions();
+}
+
 }   // namespace
 
 /********************************************************************
@@ -701,6 +767,10 @@ int CommandLineHelpModule::run(int argc, char *argv[])
         {
             exporter.reset(new HelpExportHtml(*impl_));
         }
+        else if (exportFormat == "completion")
+        {
+            exporter.reset(new HelpExportCompletion(*impl_));
+        }
         else
         {
             GMX_THROW(NotImplementedError("This help format is not implemented"));