2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015, 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.
37 * Implements gmx::CommandLineHelpModule.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
44 #include "cmdlinehelpmodule.h"
49 #include <boost/scoped_ptr.hpp>
51 #include "gromacs/commandline/cmdlinehelpcontext.h"
52 #include "gromacs/commandline/cmdlinehelpwriter.h"
53 #include "gromacs/commandline/cmdlineparser.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 #include "shellcompletions.h"
74 class HelpExportInterface;
78 /********************************************************************
79 * CommandLineHelpModuleImpl declaration
82 class CommandLineHelpModuleImpl
85 CommandLineHelpModuleImpl(const ProgramContextInterface &programContext,
86 const std::string &binaryName,
87 const CommandLineModuleMap &modules,
88 const CommandLineModuleGroupList &groups);
90 void exportHelp(HelpExportInterface *exporter) const;
92 boost::scoped_ptr<RootHelpTopic> rootTopic_;
93 const ProgramContextInterface &programContext_;
94 std::string binaryName_;
95 const CommandLineModuleMap &modules_;
96 const CommandLineModuleGroupList &groups_;
98 CommandLineHelpContext *context_;
99 const CommandLineModuleInterface *moduleOverride_;
102 File *outputOverride_;
104 GMX_DISALLOW_COPY_AND_ASSIGN(CommandLineHelpModuleImpl);
110 /********************************************************************
116 static const char name[];
117 static const char title[];
118 static const char *const text[];
121 // The first two are not used.
122 const char RootHelpText::name[] = "";
123 const char RootHelpText::title[] = "";
124 const char *const RootHelpText::text[] = { "" };
127 * Help topic that forms the root of the help tree for the help subcommand.
129 * \ingroup module_commandline
131 class RootHelpTopic : public CompositeHelpTopic<RootHelpText>
135 * Creates a root help topic.
139 explicit RootHelpTopic(const CommandLineHelpModuleImpl &helpModule)
140 : helpModule_(helpModule)
144 virtual void writeHelp(const HelpWriterContext &context) const;
147 const CommandLineHelpModuleImpl &helpModule_;
149 GMX_DISALLOW_COPY_AND_ASSIGN(RootHelpTopic);
152 void RootHelpTopic::writeHelp(const HelpWriterContext &context) const
154 if (context.outputFormat() != eHelpOutputFormat_Console)
156 // TODO: Implement once the situation with Redmine issue #969 is more
158 GMX_THROW(NotImplementedError(
159 "Root help is not implemented for this output format"));
162 CommandLineCommonOptionsHolder optionsHolder;
163 CommandLineHelpContext cmdlineContext(*helpModule_.context_);
164 cmdlineContext.setModuleDisplayName(helpModule_.binaryName_);
165 optionsHolder.initOptions();
166 // TODO: Add <command> [<args>] into the synopsis.
167 CommandLineHelpWriter(*optionsHolder.options())
168 .writeHelp(cmdlineContext);
170 // TODO: Consider printing a list of "core" commands. Would require someone
171 // to determine such a set...
172 writeSubTopicList(context,
173 "Additional help is available on the following topics:");
174 context.writeTextBlock(
175 "To access the help, use '[PROGRAM] help <topic>'.[BR]"
176 "For help on a command, use '[PROGRAM] help <command>'.");
179 /********************************************************************
184 * Help topic for listing the commands.
186 * \ingroup module_commandline
188 class CommandsHelpTopic : public HelpTopicInterface
192 * Creates a command list help topic.
194 * \param[in] helpModule Help module to get module information from.
198 explicit CommandsHelpTopic(const CommandLineHelpModuleImpl &helpModule)
199 : helpModule_(helpModule)
203 virtual const char *name() const { return "commands"; }
204 virtual const char *title() const { return "List of available commands"; }
205 virtual bool hasSubTopics() const { return false; }
206 virtual const HelpTopicInterface *findSubTopic(const char * /*name*/) const
211 virtual void writeHelp(const HelpWriterContext &context) const;
214 const CommandLineHelpModuleImpl &helpModule_;
216 GMX_DISALLOW_COPY_AND_ASSIGN(CommandsHelpTopic);
219 void CommandsHelpTopic::writeHelp(const HelpWriterContext &context) const
221 if (context.outputFormat() != eHelpOutputFormat_Console)
223 GMX_THROW(NotImplementedError(
224 "Module list is not implemented for this output format"));
226 int maxNameLength = 0;
227 const CommandLineModuleMap &modules = helpModule_.modules_;
228 CommandLineModuleMap::const_iterator module;
229 for (module = modules.begin(); module != modules.end(); ++module)
231 int nameLength = static_cast<int>(module->first.length());
232 if (module->second->shortDescription() != NULL
233 && nameLength > maxNameLength)
235 maxNameLength = nameLength;
238 context.writeTextBlock(
239 "Usage: [PROGRAM] [<options>] <command> [<args>][PAR]"
240 "Available commands:");
241 File &file = context.outputFile();
242 TextTableFormatter formatter;
243 formatter.addColumn(NULL, maxNameLength + 1, false);
244 formatter.addColumn(NULL, 72 - maxNameLength, true);
245 formatter.setFirstColumnIndent(4);
246 for (module = modules.begin(); module != modules.end(); ++module)
248 const char *name = module->first.c_str();
249 const char *description = module->second->shortDescription();
250 if (description != NULL)
253 formatter.addColumnLine(0, name);
254 formatter.addColumnLine(1, description);
255 file.writeString(formatter.formatRow());
258 context.writeTextBlock(
259 "For help on a command, use '[PROGRAM] help <command>'.");
262 /********************************************************************
267 * Help topic wrapper for a command-line module.
269 * This class implements HelpTopicInterface such that it wraps a
270 * CommandLineModuleInterface, allowing subcommand "help <command>"
271 * to produce the help for "<command>".
273 * \ingroup module_commandline
275 class ModuleHelpTopic : public HelpTopicInterface
278 //! Constructs a help topic for a specific module.
279 ModuleHelpTopic(const CommandLineModuleInterface &module,
280 const CommandLineHelpModuleImpl &helpModule)
281 : module_(module), helpModule_(helpModule)
285 virtual const char *name() const { return module_.name(); }
286 virtual const char *title() const { return NULL; }
287 virtual bool hasSubTopics() const { return false; }
288 virtual const HelpTopicInterface *findSubTopic(const char * /*name*/) const
292 virtual void writeHelp(const HelpWriterContext &context) const;
295 const CommandLineModuleInterface &module_;
296 const CommandLineHelpModuleImpl &helpModule_;
298 GMX_DISALLOW_COPY_AND_ASSIGN(ModuleHelpTopic);
301 void ModuleHelpTopic::writeHelp(const HelpWriterContext & /*context*/) const
303 CommandLineHelpContext context(*helpModule_.context_);
304 const char *const program = helpModule_.binaryName_.c_str();
305 context.setModuleDisplayName(formatString("%s %s", program, module_.name()));
306 module_.writeHelp(context);
309 /********************************************************************
310 * HelpExportInterface
314 * Callbacks for exporting help information for command-line modules.
316 * \ingroup module_commandline
318 class HelpExportInterface
321 //! Shorthand for a list of modules contained in a group.
322 typedef CommandLineModuleGroupData::ModuleList ModuleGroupContents;
324 virtual ~HelpExportInterface() {};
327 * Called once before exporting individual modules.
329 * Can, e.g., open shared output files (e.g., if the output is written
330 * into a single file, or if a separate index is required) and write
333 virtual void startModuleExport() = 0;
335 * Called to export the help for each module.
337 * \param[in] module Module for which the help should be exported.
338 * \param[in] tag Unique tag for the module (gmx-something).
339 * \param[in] displayName Display name for the module (gmx something).
341 virtual void exportModuleHelp(
342 const CommandLineModuleInterface &module,
343 const std::string &tag,
344 const std::string &displayName) = 0;
346 * Called after all modules have been exported.
348 * Can close files opened in startModuleExport(), write footers to them
351 virtual void finishModuleExport() = 0;
354 * Called once before exporting module groups.
356 * Can, e.g., open a single output file for listing all the groups.
358 virtual void startModuleGroupExport() = 0;
360 * Called to export the help for each module group.
362 * \param[in] title Title for the group.
363 * \param[in] modules List of modules in the group.
365 virtual void exportModuleGroup(const char *title,
366 const ModuleGroupContents &modules) = 0;
368 * Called after all module groups have been exported.
370 * Can close files opened in startModuleGroupExport(), write footers to them
373 virtual void finishModuleGroupExport() = 0;
377 * Adds hyperlinks to modules within this binary.
379 * \param[in,out] links Links are added here.
380 * \param[in] helpModule Help module to get module information from.
381 * \throws std::bad_alloc if out of memory.
383 * Initializes a HelpLinks object with links to modules defined in
386 * \ingroup module_commandline
388 void initProgramLinks(HelpLinks *links, const CommandLineHelpModuleImpl &helpModule)
390 const char *const program = helpModule.binaryName_.c_str();
391 CommandLineModuleMap::const_iterator module;
392 for (module = helpModule.modules_.begin();
393 module != helpModule.modules_.end();
396 if (module->second->shortDescription() != NULL)
398 std::string linkName("[gmx-" + module->first + "]");
399 const char *name = module->first.c_str();
400 std::string reference(
401 formatString(":doc:`%s %s <%s-%s>`", program, name, program, name));
402 std::string displayName(
403 formatString("[TT]%s %s[tt]", program, name));
404 links->addLink(linkName, reference, displayName);
409 /********************************************************************
415 * Implements export for HTML help.
417 * This whole class can go once docs/old-html/ no longer requires header.html
420 * \ingroup module_commandline
422 class HelpExportHtml : public HelpExportInterface
425 //! Initializes HTML exporter.
428 virtual void startModuleExport() {}
429 virtual void exportModuleHelp(
430 const CommandLineModuleInterface & /*module*/,
431 const std::string & /*tag*/,
432 const std::string & /*displayName*/) {}
433 virtual void finishModuleExport() {}
435 virtual void startModuleGroupExport() {}
436 virtual void exportModuleGroup(const char * /*title*/,
437 const ModuleGroupContents & /*modules*/) {}
438 virtual void finishModuleGroupExport() {}
441 HelpExportHtml::HelpExportHtml()
443 std::string header = gmx::File::readToString("header.html.in");
444 header = replaceAll(header, "@VERSION@", gmx_version());
445 gmx::File::writeFileFromString("header.html", header);
448 /********************************************************************
449 * HelpExportReStructuredText
453 * Implements export for web pages as reStructuredText.
455 * \ingroup module_commandline
457 class HelpExportReStructuredText : public HelpExportInterface
460 //! Initializes reST exporter.
461 explicit HelpExportReStructuredText(
462 const CommandLineHelpModuleImpl &helpModule);
464 virtual void startModuleExport();
465 virtual void exportModuleHelp(
466 const CommandLineModuleInterface &module,
467 const std::string &tag,
468 const std::string &displayName);
469 virtual void finishModuleExport();
471 virtual void startModuleGroupExport();
472 virtual void exportModuleGroup(const char *title,
473 const ModuleGroupContents &modules);
474 virtual void finishModuleGroupExport();
478 boost::scoped_ptr<File> indexFile_;
479 boost::scoped_ptr<File> manPagesFile_;
482 HelpExportReStructuredText::HelpExportReStructuredText(
483 const CommandLineHelpModuleImpl &helpModule)
484 : links_(eHelpOutputFormat_Rst)
486 File linksFile("links.dat", "r");
488 while (linksFile.readLine(&line))
490 links_.addLink("[REF]." + line + "[ref]",
491 formatString(":ref:`.%s <%s>`", line.c_str(), line.c_str()),
493 links_.addLink("[REF]" + line + "[ref]", formatString(":ref:`%s`", line.c_str()), line);
496 initProgramLinks(&links_, helpModule);
499 void HelpExportReStructuredText::startModuleExport()
501 indexFile_.reset(new File("programs/byname.rst", "w"));
502 indexFile_->writeLine("Tools by Name");
503 indexFile_->writeLine("=============");
504 manPagesFile_.reset(new File("conf-man.py", "w"));
505 manPagesFile_->writeLine("man_pages = [");
508 void HelpExportReStructuredText::exportModuleHelp(
509 const CommandLineModuleInterface &module,
510 const std::string &tag,
511 const std::string &displayName)
513 // TODO: Ideally, the file would only be touched if it really changes.
514 // This would make Sphinx reruns much faster.
515 File file("programs/" + tag + ".rst", "w");
516 file.writeLine(formatString(".. _%s:", displayName.c_str()));
518 file.writeLine(displayName);
519 file.writeLine(std::string(displayName.length(), '='));
520 CommandLineHelpContext context(&file, eHelpOutputFormat_Rst, &links_);
521 context.setModuleDisplayName(displayName);
522 module.writeHelp(context);
525 file.writeLine(".. only:: man");
527 file.writeLine(" See also");
528 file.writeLine(" --------");
530 file.writeLine(" :manpage:`gromacs(7)`");
532 file.writeLine(" More information about |Gromacs| is available at <http://www.gromacs.org/>.");
535 indexFile_->writeLine(formatString("* :doc:`%s <%s>` - %s",
536 displayName.c_str(), tag.c_str(),
537 module.shortDescription()));
538 manPagesFile_->writeLine(
539 formatString(" ('programs/%s', '%s', \"%s\", '', 1),",
540 tag.c_str(), tag.c_str(), module.shortDescription()));
543 void HelpExportReStructuredText::finishModuleExport()
547 manPagesFile_->writeLine(" ('man/gromacs.7', 'gromacs', 'molecular dynamics simulation suite', '', 7)");
548 manPagesFile_->writeLine("]");
549 manPagesFile_->close();
550 manPagesFile_.reset();
553 void HelpExportReStructuredText::startModuleGroupExport()
555 indexFile_.reset(new File("programs/bytopic.rst", "w"));
556 indexFile_->writeLine("Tools by Topic");
557 indexFile_->writeLine("==============");
558 manPagesFile_.reset(new File("man/bytopic.rst", "w"));
561 void HelpExportReStructuredText::exportModuleGroup(
563 const ModuleGroupContents &modules)
565 indexFile_->writeLine(title);
566 indexFile_->writeLine(std::string(std::strlen(title), '-'));
567 manPagesFile_->writeLine(title);
568 manPagesFile_->writeLine(std::string(std::strlen(title), '+'));
570 ModuleGroupContents::const_iterator module;
571 for (module = modules.begin(); module != modules.end(); ++module)
573 const std::string &tag(module->first);
574 std::string displayName(tag);
575 // TODO: This does not work if the binary name would contain a dash,
576 // but that is not currently the case.
577 const size_t dashPos = displayName.find('-');
578 GMX_RELEASE_ASSERT(dashPos != std::string::npos,
579 "There should always be at least one dash in the tag");
580 displayName[dashPos] = ' ';
581 indexFile_->writeLine(formatString("| :doc:`%s <%s>` - %s",
582 displayName.c_str(), tag.c_str(),
584 manPagesFile_->writeLine(formatString("| ``%s`` - %s",
588 indexFile_->writeLine();
589 manPagesFile_->writeLine();
592 void HelpExportReStructuredText::finishModuleGroupExport()
596 manPagesFile_->close();
597 manPagesFile_.reset();
600 /********************************************************************
601 * HelpExportCompletion
605 * Implements export for command-line completion.
607 * \ingroup module_commandline
609 class HelpExportCompletion : public HelpExportInterface
612 //! Initializes completion exporter.
613 explicit HelpExportCompletion(const CommandLineHelpModuleImpl &helpModule);
615 virtual void startModuleExport();
616 virtual void exportModuleHelp(
617 const CommandLineModuleInterface &module,
618 const std::string &tag,
619 const std::string &displayName);
620 virtual void finishModuleExport();
622 virtual void startModuleGroupExport() {}
623 virtual void exportModuleGroup(const char * /*title*/,
624 const ModuleGroupContents & /*modules*/) {}
625 virtual void finishModuleGroupExport() {}
628 ShellCompletionWriter bashWriter_;
629 std::vector<std::string> modules_;
632 HelpExportCompletion::HelpExportCompletion(
633 const CommandLineHelpModuleImpl &helpModule)
634 : bashWriter_(helpModule.binaryName_, eShellCompletionFormat_Bash)
638 void HelpExportCompletion::startModuleExport()
640 bashWriter_.startCompletions();
643 void HelpExportCompletion::exportModuleHelp(
644 const CommandLineModuleInterface &module,
645 const std::string & /*tag*/,
646 const std::string & /*displayName*/)
648 modules_.push_back(module.name());
650 CommandLineHelpContext context(&bashWriter_);
651 // We use the display name to pass the name of the module to the
652 // completion writer.
653 context.setModuleDisplayName(module.name());
654 module.writeHelp(context);
658 void HelpExportCompletion::finishModuleExport()
660 CommandLineCommonOptionsHolder optionsHolder;
661 optionsHolder.initOptions();
662 bashWriter_.writeWrapperCompletions(modules_, *optionsHolder.options());
663 bashWriter_.finishCompletions();
668 /********************************************************************
669 * CommandLineHelpModuleImpl implementation
671 CommandLineHelpModuleImpl::CommandLineHelpModuleImpl(
672 const ProgramContextInterface &programContext,
673 const std::string &binaryName,
674 const CommandLineModuleMap &modules,
675 const CommandLineModuleGroupList &groups)
676 : rootTopic_(new RootHelpTopic(*this)), programContext_(programContext),
677 binaryName_(binaryName), modules_(modules), groups_(groups),
678 context_(NULL), moduleOverride_(NULL), bHidden_(false),
679 outputOverride_(NULL)
683 void CommandLineHelpModuleImpl::exportHelp(HelpExportInterface *exporter) const
685 // TODO: Would be nicer to have the file names supplied by the build system
686 // and/or export a list of files from here.
687 const char *const program = binaryName_.c_str();
689 exporter->startModuleExport();
690 CommandLineModuleMap::const_iterator module;
691 for (module = modules_.begin(); module != modules_.end(); ++module)
693 if (module->second->shortDescription() != NULL)
695 const char *const moduleName = module->first.c_str();
696 std::string tag(formatString("%s-%s", program, moduleName));
697 std::string displayName(formatString("%s %s", program, moduleName));
698 exporter->exportModuleHelp(*module->second, tag, displayName);
701 exporter->finishModuleExport();
703 exporter->startModuleGroupExport();
704 CommandLineModuleGroupList::const_iterator group;
705 for (group = groups_.begin(); group != groups_.end(); ++group)
707 exporter->exportModuleGroup((*group)->title(), (*group)->modules());
709 exporter->finishModuleGroupExport();
712 /********************************************************************
713 * CommandLineHelpModule
716 CommandLineHelpModule::CommandLineHelpModule(
717 const ProgramContextInterface &programContext,
718 const std::string &binaryName,
719 const CommandLineModuleMap &modules,
720 const CommandLineModuleGroupList &groups)
721 : impl_(new Impl(programContext, binaryName, modules, groups))
725 CommandLineHelpModule::~CommandLineHelpModule()
729 HelpTopicPointer CommandLineHelpModule::createModuleHelpTopic(
730 const CommandLineModuleInterface &module) const
732 return HelpTopicPointer(new ModuleHelpTopic(module, *impl_));
735 void CommandLineHelpModule::addTopic(HelpTopicPointer topic)
737 impl_->rootTopic_->addSubTopic(move(topic));
740 void CommandLineHelpModule::setShowHidden(bool bHidden)
742 impl_->bHidden_ = bHidden;
745 void CommandLineHelpModule::setModuleOverride(
746 const CommandLineModuleInterface &module)
748 impl_->moduleOverride_ = &module;
751 void CommandLineHelpModule::setOutputRedirect(File *output)
753 impl_->outputOverride_ = output;
756 int CommandLineHelpModule::run(int argc, char *argv[])
758 // Add internal topics lazily here.
759 addTopic(HelpTopicPointer(new CommandsHelpTopic(*impl_)));
761 const char *const exportFormats[] = { "html", "rst", "completion" };
762 std::string exportFormat;
763 Options options(NULL, NULL);
764 options.addOption(StringOption("export").store(&exportFormat)
765 .enumValue(exportFormats));
766 CommandLineParser(&options).parse(&argc, argv);
767 if (!exportFormat.empty())
769 boost::scoped_ptr<HelpExportInterface> exporter;
770 if (exportFormat == "html")
772 exporter.reset(new HelpExportHtml());
774 else if (exportFormat == "rst")
776 exporter.reset(new HelpExportReStructuredText(*impl_));
778 else if (exportFormat == "completion")
780 exporter.reset(new HelpExportCompletion(*impl_));
784 GMX_THROW(NotImplementedError("This help format is not implemented"));
786 impl_->exportHelp(exporter.get());
790 File *outputFile = &File::standardOutput();
791 if (impl_->outputOverride_ != NULL)
793 outputFile = impl_->outputOverride_;
795 HelpLinks links(eHelpOutputFormat_Console);
796 initProgramLinks(&links, *impl_);
797 boost::scoped_ptr<CommandLineHelpContext> context(
798 new CommandLineHelpContext(outputFile,
799 eHelpOutputFormat_Console, &links));
800 context->setShowHidden(impl_->bHidden_);
801 if (impl_->moduleOverride_ != NULL)
803 context->setModuleDisplayName(impl_->programContext_.displayName());
804 impl_->moduleOverride_->writeHelp(*context);
807 impl_->context_ = context.get();
809 HelpManager helpManager(*impl_->rootTopic_, context->writerContext());
812 for (int i = 1; i < argc; ++i)
814 helpManager.enterTopic(argv[i]);
817 catch (const InvalidInputError &ex)
819 fprintf(stderr, "%s\n", ex.what());
822 helpManager.writeCurrentTopic();
826 void CommandLineHelpModule::writeHelp(const CommandLineHelpContext &context) const
828 const HelpWriterContext &writerContext = context.writerContext();
830 if (writerContext.outputFormat() != eHelpOutputFormat_Console)
834 writerContext.writeTextBlock(
835 "Usage: [PROGRAM] help [<command>|<topic> [<subtopic> [...]]]");
836 // TODO: More information.