2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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::CommandLineModuleManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
42 #include "cmdlinemodulemanager.h"
50 #include "gromacs/commandline/cmdlinemodule.h"
51 #include "gromacs/onlinehelp/helpformat.h"
52 #include "gromacs/onlinehelp/helpmanager.h"
53 #include "gromacs/onlinehelp/helptopic.h"
54 #include "gromacs/onlinehelp/helpwritercontext.h"
55 #include "gromacs/utility/file.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/programinfo.h"
59 #include "gromacs/utility/stringutil.h"
64 //! Container type for mapping module names to module objects.
65 typedef std::map<std::string, CommandLineModulePointer> CommandLineModuleMap;
70 /********************************************************************
76 static const char name[];
77 static const char title[];
78 static const char *const text[];
81 // The first two are not used.
82 const char RootHelpText::name[] = "";
83 const char RootHelpText::title[] = "";
84 const char *const RootHelpText::text[] = {
85 "Usage: [PROGRAM] <command> [<args>]",
89 * Help topic that forms the root of the help tree for the help subcommand.
91 * \ingroup module_commandline
93 class RootHelpTopic : public CompositeHelpTopic<RootHelpText>
97 * Creates a root help topic.
99 * \param[in] modules List of modules for to use for module listings.
103 explicit RootHelpTopic(const CommandLineModuleMap &modules)
108 virtual void writeHelp(const HelpWriterContext &context) const;
111 void printModuleList(const HelpWriterContext &context) const;
113 const CommandLineModuleMap &modules_;
115 GMX_DISALLOW_COPY_AND_ASSIGN(RootHelpTopic);
118 void RootHelpTopic::writeHelp(const HelpWriterContext &context) const
120 if (context.outputFormat() != eHelpOutputFormat_Console)
122 // TODO: Implement once the situation with Redmine issue #969 is more
124 GMX_THROW(NotImplementedError(
125 "Root help is not implemented for this output format"));
127 writeBasicHelpTopic(context, *this, helpText());
128 // TODO: If/when this list becomes long, it may be better to only print
129 // "common" commands here, and have a separate topic (e.g.,
130 // "help commands") that prints the full list.
131 printModuleList(context);
132 context.writeTextBlock(
133 "For additional help on a command, use '[PROGRAM] help <command>'");
134 writeSubTopicList(context,
135 "\nAdditional help is available on the following topics:");
136 context.writeTextBlock(
137 "To access the help, use '[PROGRAM] help <topic>'.");
140 void RootHelpTopic::printModuleList(const HelpWriterContext &context) const
142 if (context.outputFormat() != eHelpOutputFormat_Console)
144 // TODO: Implement once the situation with Redmine issue #969 is more
146 GMX_THROW(NotImplementedError(
147 "Module list is not implemented for this output format"));
149 int maxNameLength = 0;
150 CommandLineModuleMap::const_iterator module;
151 for (module = modules_.begin(); module != modules_.end(); ++module)
153 int nameLength = static_cast<int>(module->first.length());
154 if (module->second->shortDescription() != NULL
155 && nameLength > maxNameLength)
157 maxNameLength = nameLength;
160 File &file = context.outputFile();
161 TextTableFormatter formatter;
162 formatter.addColumn(NULL, maxNameLength + 1, false);
163 formatter.addColumn(NULL, 72 - maxNameLength, true);
164 formatter.setFirstColumnIndent(4);
166 file.writeLine("Available commands:");
167 for (module = modules_.begin(); module != modules_.end(); ++module)
169 const char *name = module->first.c_str();
170 const char *description = module->second->shortDescription();
171 if (description != NULL)
174 formatter.addColumnLine(0, name);
175 formatter.addColumnLine(1, description);
176 file.writeString(formatter.formatRow());
181 /********************************************************************
186 * Help topic wrapper for a command-line module.
188 * This class implements HelpTopicInterface such that it wraps a
189 * CommandLineModuleInterface, allowing subcommand "help <command>"
190 * to produce the help for "<command>".
192 * \ingroup module_commandline
194 class ModuleHelpTopic : public HelpTopicInterface
197 //! Constructs a help topic for a specific module.
198 explicit ModuleHelpTopic(const CommandLineModuleInterface &module)
203 virtual const char *name() const { return module_.name(); }
204 virtual const char *title() const { return NULL; }
205 virtual bool hasSubTopics() const { return false; }
206 virtual const HelpTopicInterface *findSubTopic(const char * /*name*/) const
210 virtual void writeHelp(const HelpWriterContext &context) const;
213 const CommandLineModuleInterface &module_;
215 GMX_DISALLOW_COPY_AND_ASSIGN(ModuleHelpTopic);
218 void ModuleHelpTopic::writeHelp(const HelpWriterContext &context) const
220 module_.writeHelp(context);
225 /********************************************************************
226 * CommandLineHelpModule
230 * Command-line module for producing help.
232 * This module implements the 'help' subcommand that is automatically added by
233 * CommandLineModuleManager.
235 * \ingroup module_commandline
237 class CommandLineHelpModule : public CommandLineModuleInterface
241 * Creates a command-line help module.
243 * \param[in] modules List of modules for to use for module listings.
244 * \throws std::bad_alloc if out of memory.
246 explicit CommandLineHelpModule(const CommandLineModuleMap &modules);
249 * Adds a top-level help topic.
251 * \param[in] topic Help topic to add.
252 * \throws std::bad_alloc if out of memory.
254 void addTopic(HelpTopicPointer topic);
256 virtual const char *name() const { return "help"; }
257 virtual const char *shortDescription() const
259 return "Print help information";
262 virtual int run(int argc, char *argv[]);
263 virtual void writeHelp(const HelpWriterContext &context) const;
265 //! Prints usage message to stderr.
266 void printUsage() const;
269 CompositeHelpTopicPointer rootTopic_;
271 GMX_DISALLOW_COPY_AND_ASSIGN(CommandLineHelpModule);
274 CommandLineHelpModule::CommandLineHelpModule(const CommandLineModuleMap &modules)
275 : rootTopic_(new RootHelpTopic(modules))
279 void CommandLineHelpModule::addTopic(HelpTopicPointer topic)
281 rootTopic_->addSubTopic(move(topic));
284 int CommandLineHelpModule::run(int argc, char *argv[])
286 HelpWriterContext context(&File::standardOutput(),
287 eHelpOutputFormat_Console);
288 HelpManager helpManager(*rootTopic_, context);
291 for (int i = 1; i < argc; ++i)
293 helpManager.enterTopic(argv[i]);
296 catch (const InvalidInputError &ex)
298 fprintf(stderr, "%s\n", ex.what());
301 helpManager.writeCurrentTopic();
302 fprintf(stderr, "\n");
306 void CommandLineHelpModule::writeHelp(const HelpWriterContext &context) const
308 context.writeTextBlock(
309 "Usage: [PROGRAM] help [<command>|<topic> [<subtopic> [...]]]");
310 // TODO: More information.
313 void CommandLineHelpModule::printUsage() const
315 HelpWriterContext context(&File::standardError(),
316 eHelpOutputFormat_Console);
317 rootTopic_->writeHelp(context);
320 /********************************************************************
321 * CommandLineModuleManager::Impl
325 * Private implementation class for CommandLineModuleManager.
327 * \ingroup module_commandline
329 class CommandLineModuleManager::Impl
334 * Initializes the implementation class.
336 * \param[in] programInfo Program information for the running binary.
338 explicit Impl(const ProgramInfo &programInfo);
341 * Finds a module that matches a name.
343 * \param[in] name Module name to find.
344 * \returns Iterator to the found module, or
345 * \c modules_.end() if not found.
349 CommandLineModuleMap::const_iterator
350 findModuleByName(const std::string &name) const;
352 * Finds a module that the name of the binary.
354 * \param[in] programInfo Program information object to use.
355 * \throws std::bad_alloc if out of memory.
356 * \returns Iterator to the found module, or
357 * \c modules_.end() if not found.
359 * Checks whether the program is invoked through a symlink whose name
360 * is different from ProgramInfo::realBinaryName(), and if so, checks
361 * if a module name matches the name of the symlink.
363 * Note that the \p programInfo parameter is currently not necessary
364 * (as the program info object is also contained as a member), but it
365 * clarifies the control flow.
367 CommandLineModuleMap::const_iterator
368 findModuleFromBinaryName(const ProgramInfo &programInfo) const;
371 * Maps module names to module objects.
373 * Owns the contained modules.
375 CommandLineModuleMap modules_;
376 //! Information about the currently running program.
377 const ProgramInfo &programInfo_;
379 * Module that implements help for the binary.
381 * The pointed module is owned by the \a modules_ container.
383 CommandLineHelpModule *helpModule_;
386 CommandLineModuleManager::Impl::Impl(const ProgramInfo &programInfo)
387 : programInfo_(programInfo)
391 CommandLineModuleMap::const_iterator
392 CommandLineModuleManager::Impl::findModuleByName(const std::string &name) const
394 // TODO: Accept unambiguous prefixes?
395 return modules_.find(name);
398 CommandLineModuleMap::const_iterator
399 CommandLineModuleManager::Impl::findModuleFromBinaryName(
400 const ProgramInfo &programInfo) const
402 std::string binaryName = programInfo.invariantProgramName();
403 if (binaryName == programInfo.realBinaryName())
405 return modules_.end();
407 if (binaryName.compare(0, 2, "g_") == 0)
409 binaryName.erase(0, 2);
411 return findModuleByName(binaryName);
414 /********************************************************************
415 * CommandLineModuleManager
418 CommandLineModuleManager::CommandLineModuleManager(const ProgramInfo &programInfo)
419 : impl_(new Impl(programInfo))
421 impl_->helpModule_ = new CommandLineHelpModule(impl_->modules_);
422 addModule(CommandLineModulePointer(impl_->helpModule_));
425 CommandLineModuleManager::~CommandLineModuleManager()
429 void CommandLineModuleManager::addModule(CommandLineModulePointer module)
431 GMX_ASSERT(impl_->modules_.find(module->name()) == impl_->modules_.end(),
432 "Attempted to register a duplicate module name");
433 HelpTopicPointer helpTopic(new ModuleHelpTopic(*module));
434 impl_->modules_.insert(std::make_pair(std::string(module->name()),
436 addHelpTopic(move(helpTopic));
439 void CommandLineModuleManager::addHelpTopic(HelpTopicPointer topic)
441 impl_->helpModule_->addTopic(move(topic));
444 int CommandLineModuleManager::run(int argc, char *argv[])
447 CommandLineModuleMap::const_iterator module
448 = impl_->findModuleFromBinaryName(impl_->programInfo_);
449 if (module == impl_->modules_.end())
453 impl_->helpModule_->printUsage();
456 module = impl_->findModuleByName(argv[1]);
459 if (module == impl_->modules_.end())
461 fprintf(stderr, "Unknown command: '%s'\n\n", argv[1]);
462 impl_->helpModule_->printUsage();
465 return module->second->run(argc - argOffset, argv + argOffset);