d4746133a685431b1b966cc477510c8f5e5c3f2d
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlinehelpmodule.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements gmx::CommandLineHelpModule.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_commandline
41  */
42 #include "gromacs/commandline/cmdlinehelpmodule.h"
43
44 #include <algorithm>
45 #include <string>
46
47 #include <boost/scoped_ptr.hpp>
48
49 #include "gromacs/legacyheaders/copyrite.h"
50
51 #include "gromacs/commandline/cmdlinehelpcontext.h"
52 #include "gromacs/commandline/cmdlineparser.h"
53 #include "gromacs/onlinehelp/helpformat.h"
54 #include "gromacs/onlinehelp/helpmanager.h"
55 #include "gromacs/onlinehelp/helptopic.h"
56 #include "gromacs/onlinehelp/helpwritercontext.h"
57 #include "gromacs/options/basicoptions.h"
58 #include "gromacs/options/options.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/file.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/programinfo.h"
63 #include "gromacs/utility/stringutil.h"
64
65 namespace gmx
66 {
67
68 namespace
69 {
70 class HelpExportInterface;
71 class RootHelpTopic;
72 }   // namespace
73
74 /********************************************************************
75  * CommandLineHelpModuleImpl declaration
76  */
77
78 class CommandLineHelpModuleImpl
79 {
80     public:
81         CommandLineHelpModuleImpl(const ProgramInfo                &programInfo,
82                                   const std::string                &binaryName,
83                                   const CommandLineModuleMap       &modules,
84                                   const CommandLineModuleGroupList &groups);
85
86         void exportHelp(HelpExportInterface *exporter) const;
87
88         boost::scoped_ptr<RootHelpTopic>  rootTopic_;
89         const ProgramInfo                &programInfo_;
90         std::string                       binaryName_;
91         const CommandLineModuleMap       &modules_;
92         const CommandLineModuleGroupList &groups_;
93
94         CommandLineHelpContext           *context_;
95         const CommandLineModuleInterface *moduleOverride_;
96         bool                              bHidden_;
97
98         GMX_DISALLOW_COPY_AND_ASSIGN(CommandLineHelpModuleImpl);
99 };
100
101 namespace
102 {
103
104 /********************************************************************
105  * RootHelpTopic
106  */
107
108 struct RootHelpText
109 {
110     static const char        name[];
111     static const char        title[];
112     static const char *const text[];
113 };
114
115 // The first two are not used.
116 const char        RootHelpText::name[]  = "";
117 const char        RootHelpText::title[] = "";
118 const char *const RootHelpText::text[]  = {
119     "Usage: [PROGRAM] <command> [<args>]",
120 };
121
122 /*! \brief
123  * Help topic that forms the root of the help tree for the help subcommand.
124  *
125  * \ingroup module_commandline
126  */
127 class RootHelpTopic : public CompositeHelpTopic<RootHelpText>
128 {
129     public:
130         /*! \brief
131          * Creates a root help topic.
132          *
133          * \param[in] modules  List of modules for to use for module listings.
134          *
135          * Does not throw.
136          */
137         explicit RootHelpTopic(const CommandLineModuleMap &modules)
138             : modules_(modules)
139         {
140         }
141
142         virtual void writeHelp(const HelpWriterContext &context) const;
143
144     private:
145         void printModuleList(const HelpWriterContext &context) const;
146
147         const CommandLineModuleMap &modules_;
148
149         GMX_DISALLOW_COPY_AND_ASSIGN(RootHelpTopic);
150 };
151
152 void RootHelpTopic::writeHelp(const HelpWriterContext &context) const
153 {
154     if (context.outputFormat() != eHelpOutputFormat_Console)
155     {
156         // TODO: Implement once the situation with Redmine issue #969 is more
157         // clear.
158         GMX_THROW(NotImplementedError(
159                           "Root help is not implemented for this output format"));
160     }
161     writeBasicHelpTopic(context, *this, helpText());
162     // TODO: If/when this list becomes long, it may be better to only print
163     // "common" commands here, and have a separate topic (e.g.,
164     // "help commands") that prints the full list.
165     printModuleList(context);
166     context.writeTextBlock(
167             "For additional help on a command, use '[PROGRAM] help <command>'");
168     writeSubTopicList(context,
169                       "\nAdditional help is available on the following topics:");
170     context.writeTextBlock(
171             "To access the help, use '[PROGRAM] help <topic>'.");
172 }
173
174 void RootHelpTopic::printModuleList(const HelpWriterContext &context) const
175 {
176     if (context.outputFormat() != eHelpOutputFormat_Console)
177     {
178         // TODO: Implement once the situation with Redmine issue #969 is more
179         // clear.
180         GMX_THROW(NotImplementedError(
181                           "Module list is not implemented for this output format"));
182     }
183     int maxNameLength = 0;
184     CommandLineModuleMap::const_iterator module;
185     for (module = modules_.begin(); module != modules_.end(); ++module)
186     {
187         int nameLength = static_cast<int>(module->first.length());
188         if (module->second->shortDescription() != NULL
189             && nameLength > maxNameLength)
190         {
191             maxNameLength = nameLength;
192         }
193     }
194     File              &file = context.outputFile();
195     TextTableFormatter formatter;
196     formatter.addColumn(NULL, maxNameLength + 1, false);
197     formatter.addColumn(NULL, 72 - maxNameLength, true);
198     formatter.setFirstColumnIndent(4);
199     file.writeLine();
200     file.writeLine("Available commands:");
201     for (module = modules_.begin(); module != modules_.end(); ++module)
202     {
203         const char *name        = module->first.c_str();
204         const char *description = module->second->shortDescription();
205         if (description != NULL)
206         {
207             formatter.clear();
208             formatter.addColumnLine(0, name);
209             formatter.addColumnLine(1, description);
210             file.writeString(formatter.formatRow());
211         }
212     }
213 }
214
215 /********************************************************************
216  * ModuleHelpTopic
217  */
218
219 /*! \brief
220  * Help topic wrapper for a command-line module.
221  *
222  * This class implements HelpTopicInterface such that it wraps a
223  * CommandLineModuleInterface, allowing subcommand "help <command>"
224  * to produce the help for "<command>".
225  *
226  * \ingroup module_commandline
227  */
228 class ModuleHelpTopic : public HelpTopicInterface
229 {
230     public:
231         //! Constructs a help topic for a specific module.
232         ModuleHelpTopic(const CommandLineModuleInterface &module,
233                         const CommandLineHelpModuleImpl  &helpModule)
234             : module_(module), helpModule_(helpModule)
235         {
236         }
237
238         virtual const char *name() const { return module_.name(); }
239         virtual const char *title() const { return NULL; }
240         virtual bool hasSubTopics() const { return false; }
241         virtual const HelpTopicInterface *findSubTopic(const char * /*name*/) const
242         {
243             return NULL;
244         }
245         virtual void writeHelp(const HelpWriterContext &context) const;
246
247     private:
248         const CommandLineModuleInterface &module_;
249         const CommandLineHelpModuleImpl  &helpModule_;
250
251         GMX_DISALLOW_COPY_AND_ASSIGN(ModuleHelpTopic);
252 };
253
254 void ModuleHelpTopic::writeHelp(const HelpWriterContext & /*context*/) const
255 {
256     CommandLineHelpContext context(*helpModule_.context_);
257     const char *const      program = helpModule_.binaryName_.c_str();
258     context.setModuleDisplayName(formatString("%s %s", program, module_.name()));
259     module_.writeHelp(context);
260 }
261
262 /********************************************************************
263  * HelpExportInterface
264  */
265
266 /*! \brief
267  * Callbacks for exporting help information for command-line modules.
268  *
269  * \ingroup module_commandline
270  */
271 class HelpExportInterface
272 {
273     public:
274         //! Shorthand for a list of modules contained in a group.
275         typedef CommandLineModuleGroupData::ModuleList ModuleGroupContents;
276
277         virtual ~HelpExportInterface() {};
278
279         /*! \brief
280          * Called once before exporting individual modules.
281          *
282          * Can, e.g., open shared output files (e.g., if the output is written
283          * into a single file, or if a separate index is required) and write
284          * headers into them.
285          */
286         virtual void startModuleExport() = 0;
287         /*! \brief
288          * Called to export the help for each module.
289          *
290          * \param[in] module      Module for which the help should be exported.
291          * \param[in] tag         Unique tag for the module (gmx-something).
292          * \param[in] displayName Display name for the module (gmx something).
293          */
294         virtual void exportModuleHelp(
295             const CommandLineModuleInterface &module,
296             const std::string                &tag,
297             const std::string                &displayName) = 0;
298         /*! \brief
299          * Called after all modules have been exported.
300          *
301          * Can close files opened in startModuleExport(), write footers to them
302          * etc.
303          */
304         virtual void finishModuleExport() = 0;
305
306         /*! \brief
307          * Called once before exporting module groups.
308          *
309          * Can, e.g., open a single output file for listing all the groups.
310          */
311         virtual void startModuleGroupExport() = 0;
312         /*! \brief
313          * Called to export the help for each module group.
314          *
315          * \param[in] title    Title for the group.
316          * \param[in] modules  List of modules in the group.
317          */
318         virtual void exportModuleGroup(const char                *title,
319                                        const ModuleGroupContents &modules) = 0;
320         /*! \brief
321          * Called after all module groups have been exported.
322          *
323          * Can close files opened in startModuleGroupExport(), write footers to them
324          * etc.
325          */
326         virtual void finishModuleGroupExport() = 0;
327 };
328
329 /*! \internal \brief
330  * Adds hyperlinks to modules within this binary.
331  *
332  * \param[in,out] links      Links are added here.
333  * \param[in]     helpModule Help module to get module information from.
334  * \throws        std::bad_alloc if out of memory.
335  *
336  * Initializes a HelpLinks object with links to modules defined in
337  * \p helpModule.
338  *
339  * \ingroup module_commandline
340  */
341 void initProgramLinks(HelpLinks *links, const CommandLineHelpModuleImpl &helpModule)
342 {
343     const char *const                    program = helpModule.binaryName_.c_str();
344     CommandLineModuleMap::const_iterator module;
345     for (module = helpModule.modules_.begin();
346          module != helpModule.modules_.end();
347          ++module)
348     {
349         if (module->second->shortDescription() != NULL)
350         {
351             std::string linkName("[gmx-" + module->first + "]");
352             std::string targetName(
353                     formatString("%s-%s", program, module->first.c_str()));
354             std::string displayName(
355                     formatString("[TT]%s %s[tt]", program, module->first.c_str()));
356             links->addLink(linkName, targetName, displayName);
357         }
358     }
359 }
360
361 /********************************************************************
362  * HelpExportMan
363  */
364
365 /*! \internal \brief
366  * Implements export for man pages.
367  *
368  * \ingroup module_commandline
369  */
370 class HelpExportMan : public HelpExportInterface
371 {
372     public:
373         //! Initializes man page exporter.
374         explicit HelpExportMan(const CommandLineHelpModuleImpl &helpModule)
375             : links_(eHelpOutputFormat_Man)
376         {
377             initProgramLinks(&links_, helpModule);
378         }
379
380         virtual void startModuleExport() {}
381         virtual void exportModuleHelp(
382             const CommandLineModuleInterface &module,
383             const std::string                &tag,
384             const std::string                &displayName);
385         virtual void finishModuleExport() {}
386
387         virtual void startModuleGroupExport();
388         virtual void exportModuleGroup(const char                *title,
389                                        const ModuleGroupContents &modules);
390         virtual void finishModuleGroupExport();
391
392     private:
393         HelpLinks                links_;
394         boost::scoped_ptr<File>  man7File_;
395         std::string              man7Footer_;
396 };
397
398 void HelpExportMan::exportModuleHelp(
399         const CommandLineModuleInterface &module,
400         const std::string                &tag,
401         const std::string                &displayName)
402 {
403     File file("man1/" + tag + ".1", "w");
404
405     // TODO: It would be nice to remove the VERSION prefix from the version
406     // string to make it shorter.
407     file.writeLine(formatString(".TH %s 1 \"\" \"%s\" \"GROMACS Manual\"\n",
408                                 tag.c_str(),
409                                 GromacsVersion()));
410     file.writeLine(".SH NAME");
411     file.writeLine(formatString("%s - %s", tag.c_str(),
412                                 module.shortDescription()));
413     file.writeLine();
414
415     CommandLineHelpContext context(&file, eHelpOutputFormat_Man, &links_);
416     context.setModuleDisplayName(displayName);
417     module.writeHelp(context);
418
419     file.writeLine(".SH SEE ALSO");
420     file.writeLine(".BR gromacs(7)");
421     file.writeLine();
422     file.writeLine("More information about \\fBGROMACS\\fR is available at <\\fIhttp://www.gromacs.org/\\fR>.");
423 }
424
425 void HelpExportMan::startModuleGroupExport()
426 {
427     const char *const programListPlaceholder = "@PROGMANPAGES@";
428
429     const std::string man7Template = gmx::File::readToString("man7/gromacs.7.in");
430     const size_t      index        = man7Template.find(programListPlaceholder);
431     GMX_RELEASE_ASSERT(index != std::string::npos,
432                        "gromacs.7.in must contain a @PROGMANPAGES@ line");
433     std::string header = man7Template.substr(0, index);
434     man7Footer_ = man7Template.substr(index + std::strlen(programListPlaceholder));
435     header      = replaceAll(header, "@VERSION@", GromacsVersion());
436     man7File_.reset(new File("man7/gromacs.7", "w"));
437     man7File_->writeLine(header);
438 }
439
440 void HelpExportMan::exportModuleGroup(const char                *title,
441                                       const ModuleGroupContents &modules)
442 {
443     man7File_->writeLine(formatString(".Sh \"%s\"", title));
444     man7File_->writeLine(formatString(".IX Subsection \"%s\"", title));
445     man7File_->writeLine(".Vb");
446     man7File_->writeLine(".ta 16n");
447
448     ModuleGroupContents::const_iterator module;
449     for (module = modules.begin(); module != modules.end(); ++module)
450     {
451         const std::string &tag(module->first);
452         man7File_->writeLine(formatString("\\&  %s\t%s",
453                                           tag.c_str(), module->second));
454     }
455
456     man7File_->writeLine(".Ve");
457 }
458
459 void HelpExportMan::finishModuleGroupExport()
460 {
461     man7File_->writeLine(man7Footer_);
462     man7File_->close();
463 }
464
465 /********************************************************************
466  * HelpExportHtml
467  */
468
469 /*! \internal \brief
470  * Implements export for HTML help.
471  *
472  * \ingroup module_commandline
473  */
474 class HelpExportHtml : public HelpExportInterface
475 {
476     public:
477         //! Initializes HTML exporter.
478         explicit HelpExportHtml(const CommandLineHelpModuleImpl &helpModule);
479
480         virtual void startModuleExport();
481         virtual void exportModuleHelp(
482             const CommandLineModuleInterface &module,
483             const std::string                &tag,
484             const std::string                &displayName);
485         virtual void finishModuleExport();
486
487         virtual void startModuleGroupExport();
488         virtual void exportModuleGroup(const char                *title,
489                                        const ModuleGroupContents &modules);
490         virtual void finishModuleGroupExport();
491
492     private:
493         void setupHeaderAndFooter();
494
495         void writeHtmlHeader(File *file, const std::string &title) const;
496         void writeHtmlFooter(File *file) const;
497
498         HelpLinks                links_;
499         boost::scoped_ptr<File>  indexFile_;
500         std::string              header_;
501         std::string              footer_;
502 };
503
504 HelpExportHtml::HelpExportHtml(const CommandLineHelpModuleImpl &helpModule)
505     : links_(eHelpOutputFormat_Html)
506 {
507     initProgramLinks(&links_, helpModule);
508     File             linksFile("links.dat", "r");
509     std::string      line;
510     while (linksFile.readLine(&line))
511     {
512         links_.addLink(line, "../online/" + line, line);
513     }
514     linksFile.close();
515     setupHeaderAndFooter();
516 }
517
518 void HelpExportHtml::setupHeaderAndFooter()
519 {
520     header_ = gmx::File::readToString("header.html.in");
521     header_ = replaceAll(header_, "@VERSION@", GromacsVersion());
522     gmx::File::writeFileFromString("header.html", header_);
523     header_ = replaceAll(header_, "@ROOTPATH@", "../");
524     footer_ = gmx::File::readToString("footer.html");
525 }
526
527 void HelpExportHtml::startModuleExport()
528 {
529     indexFile_.reset(new File("final/programs/byname.html", "w"));
530     writeHtmlHeader(indexFile_.get(), "GROMACS Programs by Name");
531     indexFile_->writeLine("<H3>GROMACS Programs Alphabetically</H3>");
532 }
533
534 void HelpExportHtml::exportModuleHelp(
535         const CommandLineModuleInterface &module,
536         const std::string                &tag,
537         const std::string                &displayName)
538 {
539     File file("final/programs/" + tag + ".html", "w");
540     writeHtmlHeader(&file, displayName);
541
542     CommandLineHelpContext context(&file, eHelpOutputFormat_Html, &links_);
543     context.setModuleDisplayName(displayName);
544     module.writeHelp(context);
545
546     writeHtmlFooter(&file);
547     file.close();
548
549     indexFile_->writeLine(formatString("<a href=\"%s.html\">%s</a> - %s<br>",
550                                        tag.c_str(), displayName.c_str(),
551                                        module.shortDescription()));
552 }
553
554 void HelpExportHtml::finishModuleExport()
555 {
556     writeHtmlFooter(indexFile_.get());
557     indexFile_->close();
558 }
559
560 void HelpExportHtml::startModuleGroupExport()
561 {
562     indexFile_.reset(new File("final/programs/bytopic.html", "w"));
563     writeHtmlHeader(indexFile_.get(), "GROMACS Programs by Topic");
564     indexFile_->writeLine("<H3>GROMACS Programs by Topic</H3>");
565 }
566
567 void HelpExportHtml::exportModuleGroup(const char                *title,
568                                        const ModuleGroupContents &modules)
569 {
570     indexFile_->writeLine(formatString("<H4>%s</H4>", title));
571
572     ModuleGroupContents::const_iterator module;
573     for (module = modules.begin(); module != modules.end(); ++module)
574     {
575         const std::string     &tag(module->first);
576         std::string            displayName(tag);
577         std::replace(displayName.begin(), displayName.end(), '-', ' ');
578         indexFile_->writeLine(formatString("<a href=\"%s.html\">%s</a> - %s<br>",
579                                            tag.c_str(), displayName.c_str(),
580                                            module->second));
581     }
582 }
583
584 void HelpExportHtml::finishModuleGroupExport()
585 {
586     writeHtmlFooter(indexFile_.get());
587     indexFile_->close();
588 }
589
590 void HelpExportHtml::writeHtmlHeader(File *file, const std::string &title) const
591 {
592     file->writeLine(replaceAll(header_, "@TITLE@", title));
593 }
594
595 void HelpExportHtml::writeHtmlFooter(File *file) const
596 {
597     file->writeLine(footer_);
598 }
599
600 }   // namespace
601
602 /********************************************************************
603  * CommandLineHelpModuleImpl implementation
604  */
605 CommandLineHelpModuleImpl::CommandLineHelpModuleImpl(
606         const ProgramInfo                &programInfo,
607         const std::string                &binaryName,
608         const CommandLineModuleMap       &modules,
609         const CommandLineModuleGroupList &groups)
610     : rootTopic_(new RootHelpTopic(modules)), programInfo_(programInfo),
611       binaryName_(binaryName), modules_(modules), groups_(groups),
612       context_(NULL), moduleOverride_(NULL), bHidden_(false)
613 {
614 }
615
616 void CommandLineHelpModuleImpl::exportHelp(HelpExportInterface *exporter) const
617 {
618     // TODO: Would be nicer to have the file names supplied by the build system
619     // and/or export a list of files from here.
620     const char *const program = binaryName_.c_str();
621
622     exporter->startModuleExport();
623     CommandLineModuleMap::const_iterator module;
624     for (module = modules_.begin(); module != modules_.end(); ++module)
625     {
626         if (module->second->shortDescription() != NULL)
627         {
628             const char *const moduleName = module->first.c_str();
629             std::string       tag(formatString("%s-%s", program, moduleName));
630             std::string       displayName(tag);
631             std::replace(displayName.begin(), displayName.end(), '-', ' ');
632             exporter->exportModuleHelp(*module->second, tag, displayName);
633         }
634     }
635     exporter->finishModuleExport();
636
637     exporter->startModuleGroupExport();
638     CommandLineModuleGroupList::const_iterator group;
639     for (group = groups_.begin(); group != groups_.end(); ++group)
640     {
641         exporter->exportModuleGroup((*group)->title(), (*group)->modules());
642     }
643     exporter->finishModuleGroupExport();
644 }
645
646 /********************************************************************
647  * CommandLineHelpModule
648  */
649
650 CommandLineHelpModule::CommandLineHelpModule(
651         const ProgramInfo                &programInfo,
652         const std::string                &binaryName,
653         const CommandLineModuleMap       &modules,
654         const CommandLineModuleGroupList &groups)
655     : impl_(new Impl(programInfo, binaryName, modules, groups))
656 {
657 }
658
659 CommandLineHelpModule::~CommandLineHelpModule()
660 {
661 }
662
663 HelpTopicPointer CommandLineHelpModule::createModuleHelpTopic(
664         const CommandLineModuleInterface &module) const
665 {
666     return HelpTopicPointer(new ModuleHelpTopic(module, *impl_));
667 }
668
669 void CommandLineHelpModule::addTopic(HelpTopicPointer topic)
670 {
671     impl_->rootTopic_->addSubTopic(move(topic));
672 }
673
674 void CommandLineHelpModule::setShowHidden(bool bHidden)
675 {
676     impl_->bHidden_ = bHidden;
677 }
678
679 void CommandLineHelpModule::setModuleOverride(
680         const CommandLineModuleInterface &module)
681 {
682     impl_->moduleOverride_ = &module;
683 }
684
685 int CommandLineHelpModule::run(int argc, char *argv[])
686 {
687     const char *const exportFormats[] = { "man", "html", "completion" };
688     std::string       exportFormat;
689     Options           options(NULL, NULL);
690     options.addOption(StringOption("export").store(&exportFormat)
691                           .enumValue(exportFormats));
692     CommandLineParser(&options).parse(&argc, argv);
693     if (!exportFormat.empty())
694     {
695         boost::scoped_ptr<HelpExportInterface> exporter;
696         if (exportFormat == "man")
697         {
698             exporter.reset(new HelpExportMan(*impl_));
699         }
700         else if (exportFormat == "html")
701         {
702             exporter.reset(new HelpExportHtml(*impl_));
703         }
704         else
705         {
706             GMX_THROW(NotImplementedError("This help format is not implemented"));
707         }
708         impl_->exportHelp(exporter.get());
709         return 0;
710     }
711
712     HelpLinks                                 links(eHelpOutputFormat_Console);
713     initProgramLinks(&links, *impl_);
714     boost::scoped_ptr<CommandLineHelpContext> context(
715             new CommandLineHelpContext(&File::standardOutput(),
716                                        eHelpOutputFormat_Console, &links));
717     context->setShowHidden(impl_->bHidden_);
718     if (impl_->moduleOverride_ != NULL)
719     {
720         context->setModuleDisplayName(impl_->programInfo_.displayName());
721         impl_->moduleOverride_->writeHelp(*context);
722         return 0;
723     }
724     impl_->context_ = context.get();
725
726     HelpManager helpManager(*impl_->rootTopic_, context->writerContext());
727     try
728     {
729         for (int i = 1; i < argc; ++i)
730         {
731             helpManager.enterTopic(argv[i]);
732         }
733     }
734     catch (const InvalidInputError &ex)
735     {
736         fprintf(stderr, "%s\n", ex.what());
737         return 2;
738     }
739     helpManager.writeCurrentTopic();
740     return 0;
741 }
742
743 void CommandLineHelpModule::writeHelp(const CommandLineHelpContext &context) const
744 {
745     const HelpWriterContext &writerContext = context.writerContext();
746     // TODO: Implement.
747     if (writerContext.outputFormat() != eHelpOutputFormat_Console)
748     {
749         return;
750     }
751     writerContext.writeTextBlock(
752             "Usage: [PROGRAM] help [<command>|<topic> [<subtopic> [...]]]");
753     // TODO: More information.
754 }
755
756 } // namespace gmx