Improve version information handling
[gromacs.git] / src / gromacs / commandline / cmdlinehelpmodule.cpp
blob30bf3ce3515c5efa9be8db74ffb0a7e44b98e583
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \internal \file
36 * \brief
37 * Implements gmx::CommandLineHelpModule.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
42 #include "gromacs/commandline/cmdlinehelpmodule.h"
44 #include <algorithm>
45 #include <string>
46 #include <vector>
48 #include <boost/scoped_ptr.hpp>
50 #include "gromacs/commandline/cmdlinehelpcontext.h"
51 #include "gromacs/commandline/cmdlinehelpwriter.h"
52 #include "gromacs/commandline/cmdlineparser.h"
53 #include "gromacs/commandline/shellcompletions.h"
54 #include "gromacs/onlinehelp/helpformat.h"
55 #include "gromacs/onlinehelp/helpmanager.h"
56 #include "gromacs/onlinehelp/helptopic.h"
57 #include "gromacs/onlinehelp/helpwritercontext.h"
58 #include "gromacs/options/basicoptions.h"
59 #include "gromacs/options/options.h"
60 #include "gromacs/utility/baseversion.h"
61 #include "gromacs/utility/exceptions.h"
62 #include "gromacs/utility/file.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/programcontext.h"
65 #include "gromacs/utility/stringutil.h"
67 namespace gmx
70 namespace
72 class HelpExportInterface;
73 class RootHelpTopic;
74 } // namespace
76 /********************************************************************
77 * CommandLineHelpModuleImpl declaration
80 class CommandLineHelpModuleImpl
82 public:
83 CommandLineHelpModuleImpl(const ProgramContextInterface &programContext,
84 const std::string &binaryName,
85 const CommandLineModuleMap &modules,
86 const CommandLineModuleGroupList &groups);
88 void exportHelp(HelpExportInterface *exporter) const;
90 boost::scoped_ptr<RootHelpTopic> rootTopic_;
91 const ProgramContextInterface &programContext_;
92 std::string binaryName_;
93 const CommandLineModuleMap &modules_;
94 const CommandLineModuleGroupList &groups_;
96 CommandLineHelpContext *context_;
97 const CommandLineModuleInterface *moduleOverride_;
98 bool bHidden_;
100 File *outputOverride_;
102 GMX_DISALLOW_COPY_AND_ASSIGN(CommandLineHelpModuleImpl);
105 namespace
108 /********************************************************************
109 * RootHelpTopic
112 struct RootHelpText
114 static const char name[];
115 static const char title[];
116 static const char *const text[];
119 // The first two are not used.
120 const char RootHelpText::name[] = "";
121 const char RootHelpText::title[] = "";
122 const char *const RootHelpText::text[] = { "" };
124 /*! \brief
125 * Help topic that forms the root of the help tree for the help subcommand.
127 * \ingroup module_commandline
129 class RootHelpTopic : public CompositeHelpTopic<RootHelpText>
131 public:
132 /*! \brief
133 * Creates a root help topic.
135 * Does not throw.
137 explicit RootHelpTopic(const std::string &binaryName)
138 : binaryName_(binaryName)
142 virtual void writeHelp(const HelpWriterContext &context) const;
144 private:
145 std::string binaryName_;
147 GMX_DISALLOW_COPY_AND_ASSIGN(RootHelpTopic);
150 void RootHelpTopic::writeHelp(const HelpWriterContext &context) const
152 if (context.outputFormat() != eHelpOutputFormat_Console)
154 // TODO: Implement once the situation with Redmine issue #969 is more
155 // clear.
156 GMX_THROW(NotImplementedError(
157 "Root help is not implemented for this output format"));
160 CommandLineCommonOptionsHolder optionsHolder;
161 CommandLineHelpContext cmdlineContext(context);
162 optionsHolder.initOptions();
163 cmdlineContext.setModuleDisplayName(binaryName_);
164 // TODO: Add <command> [<args>] into the synopsis.
165 // TODO: Propagate the -hidden option here.
166 CommandLineHelpWriter(*optionsHolder.options())
167 .writeHelp(cmdlineContext);
169 // TODO: Consider printing a list of "core" commands. Would require someone
170 // to determine such a set...
171 writeSubTopicList(context,
172 "Additional help is available on the following topics:");
173 context.writeTextBlock(
174 "To access the help, use '[PROGRAM] help <topic>'.[BR]"
175 "For help on a command, use '[PROGRAM] help <command>'.");
178 /********************************************************************
179 * CommandsHelpTopic
182 /*! \brief
183 * Help topic for listing the commands.
185 * \ingroup module_commandline
187 class CommandsHelpTopic : public HelpTopicInterface
189 public:
190 /*! \brief
191 * Creates a command list help topic.
193 * \param[in] helpModule Help module to get module information from.
195 * Does not throw.
197 explicit CommandsHelpTopic(const CommandLineHelpModuleImpl &helpModule)
198 : helpModule_(helpModule)
202 virtual const char *name() const { return "commands"; }
203 virtual const char *title() const { return "List of available commands"; }
204 virtual bool hasSubTopics() const { return false; }
205 virtual const HelpTopicInterface *findSubTopic(const char * /*name*/) const
207 return NULL;
210 virtual void writeHelp(const HelpWriterContext &context) const;
212 private:
213 const CommandLineHelpModuleImpl &helpModule_;
215 GMX_DISALLOW_COPY_AND_ASSIGN(CommandsHelpTopic);
218 void CommandsHelpTopic::writeHelp(const HelpWriterContext &context) const
220 if (context.outputFormat() != eHelpOutputFormat_Console)
222 GMX_THROW(NotImplementedError(
223 "Module list is not implemented for this output format"));
225 int maxNameLength = 0;
226 const CommandLineModuleMap &modules = helpModule_.modules_;
227 CommandLineModuleMap::const_iterator module;
228 for (module = modules.begin(); module != modules.end(); ++module)
230 int nameLength = static_cast<int>(module->first.length());
231 if (module->second->shortDescription() != NULL
232 && nameLength > maxNameLength)
234 maxNameLength = nameLength;
237 context.writeTextBlock(
238 "Usage: [PROGRAM] [<options>] <command> [<args>][PAR]"
239 "Available commands:");
240 File &file = context.outputFile();
241 TextTableFormatter formatter;
242 formatter.addColumn(NULL, maxNameLength + 1, false);
243 formatter.addColumn(NULL, 72 - maxNameLength, true);
244 formatter.setFirstColumnIndent(4);
245 for (module = modules.begin(); module != modules.end(); ++module)
247 const char *name = module->first.c_str();
248 const char *description = module->second->shortDescription();
249 if (description != NULL)
251 formatter.clear();
252 formatter.addColumnLine(0, name);
253 formatter.addColumnLine(1, description);
254 file.writeString(formatter.formatRow());
257 context.writeTextBlock(
258 "For help on a command, use '[PROGRAM] help <command>'.");
261 /********************************************************************
262 * ModuleHelpTopic
265 /*! \brief
266 * Help topic wrapper for a command-line module.
268 * This class implements HelpTopicInterface such that it wraps a
269 * CommandLineModuleInterface, allowing subcommand "help <command>"
270 * to produce the help for "<command>".
272 * \ingroup module_commandline
274 class ModuleHelpTopic : public HelpTopicInterface
276 public:
277 //! Constructs a help topic for a specific module.
278 ModuleHelpTopic(const CommandLineModuleInterface &module,
279 const CommandLineHelpModuleImpl &helpModule)
280 : module_(module), helpModule_(helpModule)
284 virtual const char *name() const { return module_.name(); }
285 virtual const char *title() const { return NULL; }
286 virtual bool hasSubTopics() const { return false; }
287 virtual const HelpTopicInterface *findSubTopic(const char * /*name*/) const
289 return NULL;
291 virtual void writeHelp(const HelpWriterContext &context) const;
293 private:
294 const CommandLineModuleInterface &module_;
295 const CommandLineHelpModuleImpl &helpModule_;
297 GMX_DISALLOW_COPY_AND_ASSIGN(ModuleHelpTopic);
300 void ModuleHelpTopic::writeHelp(const HelpWriterContext & /*context*/) const
302 CommandLineHelpContext context(*helpModule_.context_);
303 const char *const program = helpModule_.binaryName_.c_str();
304 context.setModuleDisplayName(formatString("%s %s", program, module_.name()));
305 module_.writeHelp(context);
308 /********************************************************************
309 * HelpExportInterface
312 /*! \brief
313 * Callbacks for exporting help information for command-line modules.
315 * \ingroup module_commandline
317 class HelpExportInterface
319 public:
320 //! Shorthand for a list of modules contained in a group.
321 typedef CommandLineModuleGroupData::ModuleList ModuleGroupContents;
323 virtual ~HelpExportInterface() {};
325 /*! \brief
326 * Called once before exporting individual modules.
328 * Can, e.g., open shared output files (e.g., if the output is written
329 * into a single file, or if a separate index is required) and write
330 * headers into them.
332 virtual void startModuleExport() = 0;
333 /*! \brief
334 * Called to export the help for each module.
336 * \param[in] module Module for which the help should be exported.
337 * \param[in] tag Unique tag for the module (gmx-something).
338 * \param[in] displayName Display name for the module (gmx something).
340 virtual void exportModuleHelp(
341 const CommandLineModuleInterface &module,
342 const std::string &tag,
343 const std::string &displayName) = 0;
344 /*! \brief
345 * Called after all modules have been exported.
347 * Can close files opened in startModuleExport(), write footers to them
348 * etc.
350 virtual void finishModuleExport() = 0;
352 /*! \brief
353 * Called once before exporting module groups.
355 * Can, e.g., open a single output file for listing all the groups.
357 virtual void startModuleGroupExport() = 0;
358 /*! \brief
359 * Called to export the help for each module group.
361 * \param[in] title Title for the group.
362 * \param[in] modules List of modules in the group.
364 virtual void exportModuleGroup(const char *title,
365 const ModuleGroupContents &modules) = 0;
366 /*! \brief
367 * Called after all module groups have been exported.
369 * Can close files opened in startModuleGroupExport(), write footers to them
370 * etc.
372 virtual void finishModuleGroupExport() = 0;
375 /*! \internal \brief
376 * Adds hyperlinks to modules within this binary.
378 * \param[in,out] links Links are added here.
379 * \param[in] helpModule Help module to get module information from.
380 * \throws std::bad_alloc if out of memory.
382 * Initializes a HelpLinks object with links to modules defined in
383 * \p helpModule.
385 * \ingroup module_commandline
387 void initProgramLinks(HelpLinks *links, const CommandLineHelpModuleImpl &helpModule)
389 const char *const program = helpModule.binaryName_.c_str();
390 CommandLineModuleMap::const_iterator module;
391 for (module = helpModule.modules_.begin();
392 module != helpModule.modules_.end();
393 ++module)
395 if (module->second->shortDescription() != NULL)
397 std::string linkName("[gmx-" + module->first + "]");
398 std::string targetName(
399 formatString("%s-%s", program, module->first.c_str()));
400 std::string displayName(
401 formatString("[TT]%s %s[tt]", program, module->first.c_str()));
402 links->addLink(linkName, targetName, displayName);
407 /********************************************************************
408 * HelpExportMan
411 /*! \internal \brief
412 * Implements export for man pages.
414 * \ingroup module_commandline
416 class HelpExportMan : public HelpExportInterface
418 public:
419 //! Initializes man page exporter.
420 explicit HelpExportMan(const CommandLineHelpModuleImpl &helpModule)
421 : links_(eHelpOutputFormat_Man)
423 initProgramLinks(&links_, helpModule);
426 virtual void startModuleExport() {}
427 virtual void exportModuleHelp(
428 const CommandLineModuleInterface &module,
429 const std::string &tag,
430 const std::string &displayName);
431 virtual void finishModuleExport() {}
433 virtual void startModuleGroupExport();
434 virtual void exportModuleGroup(const char *title,
435 const ModuleGroupContents &modules);
436 virtual void finishModuleGroupExport();
438 private:
439 HelpLinks links_;
440 boost::scoped_ptr<File> man7File_;
441 std::string man7Footer_;
444 void HelpExportMan::exportModuleHelp(
445 const CommandLineModuleInterface &module,
446 const std::string &tag,
447 const std::string &displayName)
449 File file("man1/" + tag + ".1", "w");
451 // TODO: It would be nice to remove the VERSION prefix from the version
452 // string to make it shorter.
453 file.writeLine(formatString(".TH %s 1 \"\" \"%s\" \"GROMACS Manual\"\n",
454 tag.c_str(), gmx_version()));
455 file.writeLine(".SH NAME");
456 file.writeLine(formatString("%s - %s", tag.c_str(),
457 module.shortDescription()));
458 file.writeLine();
460 CommandLineHelpContext context(&file, eHelpOutputFormat_Man, &links_);
461 context.setModuleDisplayName(displayName);
462 module.writeHelp(context);
464 file.writeLine(".SH SEE ALSO");
465 file.writeLine(".BR gromacs(7)");
466 file.writeLine();
467 file.writeLine("More information about \\fBGROMACS\\fR is available at <\\fIhttp://www.gromacs.org/\\fR>.");
470 void HelpExportMan::startModuleGroupExport()
472 const char *const programListPlaceholder = "@PROGMANPAGES@";
474 const std::string man7Template = gmx::File::readToString("man7/gromacs.7.in");
475 const size_t index = man7Template.find(programListPlaceholder);
476 GMX_RELEASE_ASSERT(index != std::string::npos,
477 "gromacs.7.in must contain a @PROGMANPAGES@ line");
478 std::string header = man7Template.substr(0, index);
479 man7Footer_ = man7Template.substr(index + std::strlen(programListPlaceholder));
480 header = replaceAll(header, "@VERSION@", gmx_version());
481 man7File_.reset(new File("man7/gromacs.7", "w"));
482 man7File_->writeLine(header);
485 void HelpExportMan::exportModuleGroup(const char *title,
486 const ModuleGroupContents &modules)
488 man7File_->writeLine(formatString(".Sh \"%s\"", title));
489 man7File_->writeLine(formatString(".IX Subsection \"%s\"", title));
490 man7File_->writeLine(".Vb");
491 man7File_->writeLine(".ta 16n");
493 ModuleGroupContents::const_iterator module;
494 for (module = modules.begin(); module != modules.end(); ++module)
496 const std::string &tag(module->first);
497 man7File_->writeLine(formatString("\\& %s\t%s",
498 tag.c_str(), module->second));
501 man7File_->writeLine(".Ve");
504 void HelpExportMan::finishModuleGroupExport()
506 man7File_->writeLine(man7Footer_);
507 man7File_->close();
510 /********************************************************************
511 * HelpExportHtml
514 /*! \internal \brief
515 * Implements export for HTML help.
517 * \ingroup module_commandline
519 class HelpExportHtml : public HelpExportInterface
521 public:
522 //! Initializes HTML exporter.
523 explicit HelpExportHtml(const CommandLineHelpModuleImpl &helpModule);
525 virtual void startModuleExport();
526 virtual void exportModuleHelp(
527 const CommandLineModuleInterface &module,
528 const std::string &tag,
529 const std::string &displayName);
530 virtual void finishModuleExport();
532 virtual void startModuleGroupExport();
533 virtual void exportModuleGroup(const char *title,
534 const ModuleGroupContents &modules);
535 virtual void finishModuleGroupExport();
537 private:
538 void setupHeaderAndFooter();
540 void writeHtmlHeader(File *file, const std::string &title) const;
541 void writeHtmlFooter(File *file) const;
543 HelpLinks links_;
544 boost::scoped_ptr<File> indexFile_;
545 std::string header_;
546 std::string footer_;
549 HelpExportHtml::HelpExportHtml(const CommandLineHelpModuleImpl &helpModule)
550 : links_(eHelpOutputFormat_Html)
552 initProgramLinks(&links_, helpModule);
553 File linksFile("links.dat", "r");
554 std::string line;
555 while (linksFile.readLine(&line))
557 links_.addLink(line, "../online/" + line, line);
559 linksFile.close();
560 setupHeaderAndFooter();
563 void HelpExportHtml::setupHeaderAndFooter()
565 header_ = gmx::File::readToString("header.html.in");
566 header_ = replaceAll(header_, "@VERSION@", gmx_version());
567 gmx::File::writeFileFromString("header.html", header_);
568 header_ = replaceAll(header_, "@ROOTPATH@", "../");
569 footer_ = gmx::File::readToString("footer.html");
572 void HelpExportHtml::startModuleExport()
574 indexFile_.reset(new File("final/programs/byname.html", "w"));
575 writeHtmlHeader(indexFile_.get(), "GROMACS Programs by Name");
576 indexFile_->writeLine("<H3>GROMACS Programs Alphabetically</H3>");
579 void HelpExportHtml::exportModuleHelp(
580 const CommandLineModuleInterface &module,
581 const std::string &tag,
582 const std::string &displayName)
584 File file("final/programs/" + tag + ".html", "w");
585 writeHtmlHeader(&file, displayName);
587 CommandLineHelpContext context(&file, eHelpOutputFormat_Html, &links_);
588 context.setModuleDisplayName(displayName);
589 module.writeHelp(context);
591 writeHtmlFooter(&file);
592 file.close();
594 indexFile_->writeLine(formatString("<a href=\"%s.html\">%s</a> - %s<br>",
595 tag.c_str(), displayName.c_str(),
596 module.shortDescription()));
599 void HelpExportHtml::finishModuleExport()
601 writeHtmlFooter(indexFile_.get());
602 indexFile_->close();
605 void HelpExportHtml::startModuleGroupExport()
607 indexFile_.reset(new File("final/programs/bytopic.html", "w"));
608 writeHtmlHeader(indexFile_.get(), "GROMACS Programs by Topic");
609 indexFile_->writeLine("<H3>GROMACS Programs by Topic</H3>");
612 void HelpExportHtml::exportModuleGroup(const char *title,
613 const ModuleGroupContents &modules)
615 indexFile_->writeLine(formatString("<H4>%s</H4>", title));
617 ModuleGroupContents::const_iterator module;
618 for (module = modules.begin(); module != modules.end(); ++module)
620 const std::string &tag(module->first);
621 std::string displayName(tag);
622 std::replace(displayName.begin(), displayName.end(), '-', ' ');
623 indexFile_->writeLine(formatString("<a href=\"%s.html\">%s</a> - %s<br>",
624 tag.c_str(), displayName.c_str(),
625 module->second));
629 void HelpExportHtml::finishModuleGroupExport()
631 writeHtmlFooter(indexFile_.get());
632 indexFile_->close();
635 void HelpExportHtml::writeHtmlHeader(File *file, const std::string &title) const
637 file->writeLine(replaceAll(header_, "@TITLE@", title));
640 void HelpExportHtml::writeHtmlFooter(File *file) const
642 file->writeLine(footer_);
645 /********************************************************************
646 * HelpExportCompletion
649 /*! \internal \brief
650 * Implements export for command-line completion.
652 * \ingroup module_commandline
654 class HelpExportCompletion : public HelpExportInterface
656 public:
657 //! Initializes completion exporter.
658 explicit HelpExportCompletion(const CommandLineHelpModuleImpl &helpModule);
660 virtual void startModuleExport();
661 virtual void exportModuleHelp(
662 const CommandLineModuleInterface &module,
663 const std::string &tag,
664 const std::string &displayName);
665 virtual void finishModuleExport();
667 virtual void startModuleGroupExport() {}
668 virtual void exportModuleGroup(const char * /*title*/,
669 const ModuleGroupContents & /*modules*/) {}
670 virtual void finishModuleGroupExport() {}
672 private:
673 ShellCompletionWriter bashWriter_;
674 std::vector<std::string> modules_;
677 HelpExportCompletion::HelpExportCompletion(
678 const CommandLineHelpModuleImpl &helpModule)
679 : bashWriter_(helpModule.binaryName_, eShellCompletionFormat_Bash)
683 void HelpExportCompletion::startModuleExport()
685 bashWriter_.startCompletions();
688 void HelpExportCompletion::exportModuleHelp(
689 const CommandLineModuleInterface &module,
690 const std::string & /*tag*/,
691 const std::string & /*displayName*/)
693 modules_.push_back(module.name());
695 CommandLineHelpContext context(&bashWriter_);
696 // We use the display name to pass the name of the module to the
697 // completion writer.
698 context.setModuleDisplayName(module.name());
699 module.writeHelp(context);
703 void HelpExportCompletion::finishModuleExport()
705 CommandLineCommonOptionsHolder optionsHolder;
706 optionsHolder.initOptions();
707 bashWriter_.writeWrapperCompletions(modules_, *optionsHolder.options());
708 bashWriter_.finishCompletions();
711 } // namespace
713 /********************************************************************
714 * CommandLineHelpModuleImpl implementation
716 CommandLineHelpModuleImpl::CommandLineHelpModuleImpl(
717 const ProgramContextInterface &programContext,
718 const std::string &binaryName,
719 const CommandLineModuleMap &modules,
720 const CommandLineModuleGroupList &groups)
721 : rootTopic_(new RootHelpTopic(binaryName)), programContext_(programContext),
722 binaryName_(binaryName), modules_(modules), groups_(groups),
723 context_(NULL), moduleOverride_(NULL), bHidden_(false),
724 outputOverride_(NULL)
728 void CommandLineHelpModuleImpl::exportHelp(HelpExportInterface *exporter) const
730 // TODO: Would be nicer to have the file names supplied by the build system
731 // and/or export a list of files from here.
732 const char *const program = binaryName_.c_str();
734 exporter->startModuleExport();
735 CommandLineModuleMap::const_iterator module;
736 for (module = modules_.begin(); module != modules_.end(); ++module)
738 if (module->second->shortDescription() != NULL)
740 const char *const moduleName = module->first.c_str();
741 std::string tag(formatString("%s-%s", program, moduleName));
742 std::string displayName(tag);
743 std::replace(displayName.begin(), displayName.end(), '-', ' ');
744 exporter->exportModuleHelp(*module->second, tag, displayName);
747 exporter->finishModuleExport();
749 exporter->startModuleGroupExport();
750 CommandLineModuleGroupList::const_iterator group;
751 for (group = groups_.begin(); group != groups_.end(); ++group)
753 exporter->exportModuleGroup((*group)->title(), (*group)->modules());
755 exporter->finishModuleGroupExport();
758 /********************************************************************
759 * CommandLineHelpModule
762 CommandLineHelpModule::CommandLineHelpModule(
763 const ProgramContextInterface &programContext,
764 const std::string &binaryName,
765 const CommandLineModuleMap &modules,
766 const CommandLineModuleGroupList &groups)
767 : impl_(new Impl(programContext, binaryName, modules, groups))
771 CommandLineHelpModule::~CommandLineHelpModule()
775 HelpTopicPointer CommandLineHelpModule::createModuleHelpTopic(
776 const CommandLineModuleInterface &module) const
778 return HelpTopicPointer(new ModuleHelpTopic(module, *impl_));
781 void CommandLineHelpModule::addTopic(HelpTopicPointer topic)
783 impl_->rootTopic_->addSubTopic(move(topic));
786 void CommandLineHelpModule::setShowHidden(bool bHidden)
788 impl_->bHidden_ = bHidden;
791 void CommandLineHelpModule::setModuleOverride(
792 const CommandLineModuleInterface &module)
794 impl_->moduleOverride_ = &module;
797 void CommandLineHelpModule::setOutputRedirect(File *output)
799 impl_->outputOverride_ = output;
802 int CommandLineHelpModule::run(int argc, char *argv[])
804 // Add internal topics lazily here.
805 addTopic(HelpTopicPointer(new CommandsHelpTopic(*impl_)));
807 const char *const exportFormats[] = { "man", "html", "completion" };
808 std::string exportFormat;
809 Options options(NULL, NULL);
810 options.addOption(StringOption("export").store(&exportFormat)
811 .enumValue(exportFormats));
812 CommandLineParser(&options).parse(&argc, argv);
813 if (!exportFormat.empty())
815 boost::scoped_ptr<HelpExportInterface> exporter;
816 if (exportFormat == "man")
818 exporter.reset(new HelpExportMan(*impl_));
820 else if (exportFormat == "html")
822 exporter.reset(new HelpExportHtml(*impl_));
824 else if (exportFormat == "completion")
826 exporter.reset(new HelpExportCompletion(*impl_));
828 else
830 GMX_THROW(NotImplementedError("This help format is not implemented"));
832 impl_->exportHelp(exporter.get());
833 return 0;
836 File *outputFile = &File::standardOutput();
837 if (impl_->outputOverride_ != NULL)
839 outputFile = impl_->outputOverride_;
841 HelpLinks links(eHelpOutputFormat_Console);
842 initProgramLinks(&links, *impl_);
843 boost::scoped_ptr<CommandLineHelpContext> context(
844 new CommandLineHelpContext(outputFile,
845 eHelpOutputFormat_Console, &links));
846 context->setShowHidden(impl_->bHidden_);
847 if (impl_->moduleOverride_ != NULL)
849 context->setModuleDisplayName(impl_->programContext_.displayName());
850 impl_->moduleOverride_->writeHelp(*context);
851 return 0;
853 impl_->context_ = context.get();
855 HelpManager helpManager(*impl_->rootTopic_, context->writerContext());
858 for (int i = 1; i < argc; ++i)
860 helpManager.enterTopic(argv[i]);
863 catch (const InvalidInputError &ex)
865 fprintf(stderr, "%s\n", ex.what());
866 return 2;
868 helpManager.writeCurrentTopic();
869 return 0;
872 void CommandLineHelpModule::writeHelp(const CommandLineHelpContext &context) const
874 const HelpWriterContext &writerContext = context.writerContext();
875 // TODO: Implement.
876 if (writerContext.outputFormat() != eHelpOutputFormat_Console)
878 return;
880 writerContext.writeTextBlock(
881 "Usage: [PROGRAM] help [<command>|<topic> [<subtopic> [...]]]");
882 // TODO: More information.
885 } // namespace gmx