Man page export from the wrapper binary.
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlinemodulemanager.cpp
index dc3f8eb52beb4ab5a5311b55349fa0cd54bc2ef4..2fbed0342e217a230925b535de59b5f9eaf602d3 100644 (file)
@@ -43,6 +43,7 @@
 
 #include <cstdio>
 
+#include <algorithm>
 #include <map>
 #include <string>
 #include <utility>
@@ -253,6 +254,51 @@ class HelpExportInterface
                                       const CommandLineModuleInterface &module) = 0;
 };
 
+/********************************************************************
+ * HelpExportMan
+ */
+
+/*! \internal \brief
+ * Implements export for man pages.
+ *
+ * \ingroup module_commandline
+ */
+class HelpExportMan : public HelpExportInterface
+{
+    public:
+        virtual void exportModuleHelp(const std::string                &tag,
+                                      const CommandLineModuleInterface &module);
+};
+
+void HelpExportMan::exportModuleHelp(const std::string                &tag,
+                                     const CommandLineModuleInterface &module)
+{
+    File file("man1/" + tag + ".1", "w");
+
+    // TODO: It would be nice to remove the VERSION prefix from the version
+    // string to make it shorter.
+    file.writeLine(formatString(".TH %s 1 \"\" \"%s\" \"GROMACS Manual\"\n",
+                                tag.c_str(),
+                                GromacsVersion()));
+    file.writeLine(".SH NAME");
+    file.writeLine(formatString("%s - %s", tag.c_str(),
+                                module.shortDescription()));
+    file.writeLine();
+
+    CommandLineHelpContext context(&file, eHelpOutputFormat_Man);
+    std::string            displayName(tag);
+    std::replace(displayName.begin(), displayName.end(), '-', ' ');
+    context.setModuleDisplayName(displayName);
+    module.writeHelp(context);
+
+    file.writeLine(".SH SEE ALSO");
+    file.writeLine(".BR gromacs(7)");
+    file.writeLine();
+    file.writeLine("More information about \\fBGROMACS\\fR is available at <\\fIhttp://www.gromacs.org/\\fR>.");
+
+    file.close();
+}
+
 }   // namespace
 
 /********************************************************************
@@ -349,6 +395,11 @@ int CommandLineHelpModule::run(int argc, char *argv[])
     if (!exportFormat.empty())
     {
         boost::scoped_ptr<HelpExportInterface> exporter;
+        if (exportFormat == "man")
+        {
+            exporter.reset(new HelpExportMan);
+        }
+        else
         {
             GMX_THROW(NotImplementedError("This help format is not implemented"));
         }
@@ -483,6 +534,9 @@ class CMainCommandLineModule : public CommandLineModuleInterface
                 case eHelpOutputFormat_Console:
                     type = "help";
                     break;
+                case eHelpOutputFormat_Man:
+                    type = "nroff";
+                    break;
                 default:
                     GMX_THROW(NotImplementedError(
                                       "Command-line help is not implemented for this output format"));