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.
37 * Implements gmx::CommandLineModuleManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
44 #include "cmdlinemodulemanager.h"
53 #include "gromacs/commandline/cmdlinehelpcontext.h"
54 #include "gromacs/commandline/cmdlineinit.h"
55 #include "gromacs/commandline/cmdlinemodule.h"
56 #include "gromacs/commandline/cmdlineparser.h"
57 #include "gromacs/commandline/cmdlineprogramcontext.h"
58 #include "gromacs/legacyheaders/copyrite.h"
59 #include "gromacs/math/utilities.h"
60 #include "gromacs/options/basicoptions.h"
61 #include "gromacs/options/options.h"
62 #include "gromacs/utility/basenetwork.h"
63 #include "gromacs/utility/exceptions.h"
64 #include "gromacs/utility/fatalerror.h"
65 #include "gromacs/utility/futil.h"
66 #include "gromacs/utility/gmxassert.h"
67 #include "gromacs/utility/stringutil.h"
68 #include "gromacs/utility/sysinfo.h"
70 #include "cmdlinehelpmodule.h"
71 #include "cmdlinemodulemanager-impl.h"
79 //! \addtogroup module_commandline
82 /********************************************************************
83 * CMainCommandLineModule
87 * Implements a CommandLineModuleInterface, given a function with C/C++ main()
90 class CMainCommandLineModule : public CommandLineModuleInterface
93 //! \copydoc gmx::CommandLineModuleManager::CMainFunction
94 typedef CommandLineModuleManager::CMainFunction CMainFunction;
97 * Creates a wrapper module for the given main function.
99 * \param[in] name Name for the module.
100 * \param[in] shortDescription One-line description for the module.
101 * \param[in] mainFunction Main function to wrap.
103 * Does not throw. This is essential for correct implementation of
104 * CommandLineModuleManager::runAsMainCMain().
106 CMainCommandLineModule(const char *name, const char *shortDescription,
107 CMainFunction mainFunction)
108 : name_(name), shortDescription_(shortDescription),
109 mainFunction_(mainFunction)
113 virtual const char *name() const
117 virtual const char *shortDescription() const
119 return shortDescription_;
122 virtual void init(CommandLineModuleSettings * /*settings*/)
125 virtual int run(int argc, char *argv[])
127 return mainFunction_(argc, argv);
129 virtual void writeHelp(const CommandLineHelpContext &context) const
131 writeCommandLineHelpCMain(context, name_, mainFunction_);
136 const char *shortDescription_;
137 CMainFunction mainFunction_;
144 /********************************************************************
145 * CommandLineCommonOptionsHolder
148 CommandLineCommonOptionsHolder::CommandLineCommonOptionsHolder()
149 : options_(NULL, NULL), bHelp_(false), bHidden_(false),
150 bQuiet_(false), bVersion_(false), bCopyright_(true),
151 niceLevel_(19), bBackup_(true), bFpexcept_(false), debugLevel_(0)
153 binaryInfoSettings_.copyright(true);
156 CommandLineCommonOptionsHolder::~CommandLineCommonOptionsHolder()
160 void CommandLineCommonOptionsHolder::initOptions()
162 options_.addOption(BooleanOption("h").store(&bHelp_)
163 .description("Print help and quit"));
164 options_.addOption(BooleanOption("hidden").store(&bHidden_)
166 .description("Show hidden options in help"));
167 options_.addOption(BooleanOption("quiet").store(&bQuiet_)
168 .description("Do not print common startup info or quotes"));
169 options_.addOption(BooleanOption("version").store(&bVersion_)
170 .description("Print extended version information and quit"));
171 options_.addOption(BooleanOption("copyright").store(&bCopyright_)
172 .description("Print copyright information on startup"));
173 options_.addOption(IntegerOption("nice").store(&niceLevel_)
174 .description("Set the nicelevel (default depends on command)"));
175 options_.addOption(BooleanOption("backup").store(&bBackup_)
176 .description("Write backups if output files exist"));
177 options_.addOption(BooleanOption("fpexcept").store(&bFpexcept_)
178 .hidden().description("Enable floating-point exceptions"));
179 options_.addOption(IntegerOption("debug").store(&debugLevel_)
180 .hidden().defaultValueIfSet(1)
181 .description("Write file with debug information, "
182 "1: short (default), 2: also x and f"));
185 bool CommandLineCommonOptionsHolder::finishOptions()
188 binaryInfoSettings_.extendedInfo(bVersion_);
189 // The latter condition suppresses the copyright with
191 binaryInfoSettings_.copyright(bCopyright_ && !bQuiet_);
195 void CommandLineCommonOptionsHolder::adjustFromSettings(
196 const CommandLineModuleSettings &settings)
198 if (!options_.isSet("nice"))
200 niceLevel_ = settings.defaultNiceLevel();
204 /********************************************************************
205 * CommandLineModuleManager::Impl
209 * Private implementation class for CommandLineModuleManager.
211 * \ingroup module_commandline
213 class CommandLineModuleManager::Impl
217 * Initializes the implementation class.
219 * \param[in] binaryName Name of the running binary
220 * (without Gromacs binary suffix or .exe on Windows).
221 * \param programContext Program information for the running binary.
223 Impl(const char *binaryName, CommandLineProgramContext *programContext);
226 * Helper method that adds a given module to the module manager.
228 * \throws std::bad_alloc if out of memory.
230 void addModule(CommandLineModulePointer module);
232 * Creates the help module if it does not yet exist.
234 * \throws std::bad_alloc if out of memory.
236 * This method should be called before accessing \a helpModule_.
238 void ensureHelpModuleExists();
241 * Finds a module that matches a name.
243 * \param[in] name Module name to find.
244 * \returns Iterator to the found module, or
245 * \c modules_.end() if not found.
249 CommandLineModuleMap::const_iterator
250 findModuleByName(const std::string &name) const;
252 * Finds a module that the name of the binary.
254 * \param[in] invokedName Name by which the program was invoked.
255 * \throws std::bad_alloc if out of memory.
256 * \returns Iterator to the found module, or
257 * \c modules_.end() if not found.
259 * Checks whether the program is invoked through a symlink whose name
260 * is different from \a binaryName_, and if so, checks
261 * if a module name matches the name of the symlink.
263 * Note that the \p invokedName parameter is currently not necessary
264 * (as the program context object is also available and provides this
265 * value), but it clarifies the control flow.
267 CommandLineModuleMap::const_iterator
268 findModuleFromBinaryName(const char *invokedName) const;
271 * Processes command-line options for the wrapper binary.
273 * \param[in,out] optionsHolder Common options.
274 * \param[in,out] argc On input, argc passed to run().
275 * On output, argc to be passed to the module.
276 * \param[in,out] argv On input, argv passed to run().
277 * On output, argv to be passed to the module.
278 * \throws InvalidInputError if there are invalid options.
279 * \returns The module that should be run.
281 * Handles command-line options that affect the wrapper binary
282 * (potentially changing the members of \c this in response to the
283 * options). Also finds the module that should be run and the
284 * arguments that should be passed to it.
286 CommandLineModuleInterface *
287 processCommonOptions(CommandLineCommonOptionsHolder *optionsHolder,
288 int *argc, char ***argv);
291 * Maps module names to module objects.
293 * Owns the contained modules.
295 CommandLineModuleMap modules_;
297 * List of groupings for modules for help output.
299 * Owns the contained module group data objects.
300 * CommandLineModuleGroup objects point to the data objects contained
303 CommandLineModuleGroupList moduleGroups_;
304 //! Information about the currently running program.
305 CommandLineProgramContext &programContext_;
306 //! Name of the binary.
307 std::string binaryName_;
309 * Module that implements help for the binary.
311 * The pointed module is owned by the \a modules_ container.
313 CommandLineHelpModule *helpModule_;
314 //! If non-NULL, run this module in single-module mode.
315 CommandLineModuleInterface *singleModule_;
316 //! Stores the value set with setQuiet().
320 GMX_DISALLOW_COPY_AND_ASSIGN(Impl);
323 CommandLineModuleManager::Impl::Impl(const char *binaryName,
324 CommandLineProgramContext *programContext)
325 : programContext_(*programContext),
326 binaryName_(binaryName != NULL ? binaryName : ""),
327 helpModule_(NULL), singleModule_(NULL),
330 GMX_RELEASE_ASSERT(binaryName_.find('-') == std::string::npos,
331 "Help export does not currently work with binary names with dashes");
334 void CommandLineModuleManager::Impl::addModule(CommandLineModulePointer module)
336 GMX_ASSERT(modules_.find(module->name()) == modules_.end(),
337 "Attempted to register a duplicate module name");
338 ensureHelpModuleExists();
339 HelpTopicPointer helpTopic(helpModule_->createModuleHelpTopic(*module));
340 modules_.insert(std::make_pair(std::string(module->name()),
342 helpModule_->addTopic(move(helpTopic));
345 void CommandLineModuleManager::Impl::ensureHelpModuleExists()
347 if (helpModule_ == NULL)
349 helpModule_ = new CommandLineHelpModule(programContext_, binaryName_,
350 modules_, moduleGroups_);
351 addModule(CommandLineModulePointer(helpModule_));
355 CommandLineModuleMap::const_iterator
356 CommandLineModuleManager::Impl::findModuleByName(const std::string &name) const
358 // TODO: Accept unambiguous prefixes?
359 return modules_.find(name);
362 CommandLineModuleMap::const_iterator
363 CommandLineModuleManager::Impl::findModuleFromBinaryName(
364 const char *invokedName) const
366 std::string moduleName = invokedName;
367 #ifdef GMX_BINARY_SUFFIX
368 moduleName = stripSuffixIfPresent(moduleName, GMX_BINARY_SUFFIX);
370 if (moduleName == binaryName_)
372 return modules_.end();
374 if (startsWith(moduleName, "g_"))
376 moduleName.erase(0, 2);
378 if (startsWith(moduleName, "gmx"))
380 moduleName.erase(0, 3);
382 return findModuleByName(moduleName);
385 CommandLineModuleInterface *
386 CommandLineModuleManager::Impl::processCommonOptions(
387 CommandLineCommonOptionsHolder *optionsHolder, int *argc, char ***argv)
389 // Check if we are directly invoking a certain module.
390 CommandLineModuleInterface *module = singleModule_;
393 // Also check for invokation through named symlinks.
394 CommandLineModuleMap::const_iterator moduleIter
395 = findModuleFromBinaryName(programContext_.programName());
396 if (moduleIter != modules_.end())
398 module = moduleIter->second.get();
402 // TODO: It would be nice to propagate at least the -quiet option to
403 // the modules so that they can also be quiet in response to this.
407 // If not in single-module mode, process options to the wrapper binary.
408 // TODO: Ideally, this could be done by CommandLineParser.
409 int argcForWrapper = 1;
410 while (argcForWrapper < *argc && (*argv)[argcForWrapper][0] == '-')
414 if (argcForWrapper > 1)
416 CommandLineParser(optionsHolder->options())
417 .parse(&argcForWrapper, *argv);
419 // If no action requested and there is a module specified, process it.
420 if (argcForWrapper < *argc && !optionsHolder->shouldIgnoreActualModule())
422 const char *moduleName = (*argv)[argcForWrapper];
423 CommandLineModuleMap::const_iterator moduleIter
424 = findModuleByName(moduleName);
425 if (moduleIter == modules_.end())
427 std::string message =
428 formatString("'%s' is not a GROMACS command.", moduleName);
429 GMX_THROW(InvalidInputError(message));
431 module = moduleIter->second.get();
432 *argc -= argcForWrapper;
433 *argv += argcForWrapper;
434 // After this point, argc and argv are the same independent of
435 // which path is taken: (*argv)[0] is the module name.
440 if (singleModule_ == NULL)
442 programContext_.setDisplayName(binaryName_ + " " + module->name());
444 // Recognize the common options also after the module name.
445 // TODO: It could be nicer to only recognize -h/-hidden if module is not
447 CommandLineParser(optionsHolder->options())
448 .skipUnknown(true).parse(argc, *argv);
450 if (!optionsHolder->finishOptions())
454 // If no module specified and no other action, show the help.
455 // Also explicitly specifying -h for the wrapper binary goes here.
456 if (module == NULL || optionsHolder->shouldShowHelp())
458 ensureHelpModuleExists();
461 helpModule_->setModuleOverride(*module);
464 module = helpModule_;
466 if (module == helpModule_)
468 helpModule_->setShowHidden(optionsHolder->shouldShowHidden());
473 /********************************************************************
474 * CommandLineModuleManager
477 CommandLineModuleManager::CommandLineModuleManager(
478 const char *binaryName, CommandLineProgramContext *programContext)
479 : impl_(new Impl(binaryName, programContext))
483 CommandLineModuleManager::~CommandLineModuleManager()
487 void CommandLineModuleManager::setQuiet(bool bQuiet)
489 impl_->bQuiet_ = bQuiet;
492 void CommandLineModuleManager::setOutputRedirect(File *output)
494 impl_->ensureHelpModuleExists();
495 impl_->helpModule_->setOutputRedirect(output);
498 void CommandLineModuleManager::setSingleModule(CommandLineModuleInterface *module)
500 impl_->singleModule_ = module;
503 void CommandLineModuleManager::addModule(CommandLineModulePointer module)
505 impl_->addModule(move(module));
508 void CommandLineModuleManager::addModuleCMain(
509 const char *name, const char *shortDescription,
510 CMainFunction mainFunction)
512 CommandLineModulePointer module(
513 new CMainCommandLineModule(name, shortDescription, mainFunction));
514 addModule(move(module));
517 CommandLineModuleGroup CommandLineModuleManager::addModuleGroup(
520 const char *const binaryName = impl_->binaryName_.c_str();
521 CommandLineModuleGroupDataPointer group(
522 new CommandLineModuleGroupData(impl_->modules_, binaryName, title));
523 impl_->moduleGroups_.push_back(move(group));
524 return CommandLineModuleGroup(impl_->moduleGroups_.back().get());
527 void CommandLineModuleManager::addHelpTopic(HelpTopicPointer topic)
529 impl_->ensureHelpModuleExists();
530 impl_->helpModule_->addTopic(move(topic));
533 int CommandLineModuleManager::run(int argc, char *argv[])
535 CommandLineModuleInterface *module;
536 const bool bMaster = (gmx_node_rank() == 0);
537 bool bQuiet = impl_->bQuiet_ || !bMaster;
538 CommandLineCommonOptionsHolder optionsHolder;
541 optionsHolder.initOptions();
542 module = impl_->processCommonOptions(&optionsHolder, &argc, &argv);
544 catch (const std::exception &)
546 bQuiet |= optionsHolder.shouldBeQuiet();
549 printBinaryInformation(stderr, impl_->programContext_,
550 optionsHolder.binaryInfoSettings());
554 bQuiet |= optionsHolder.shouldBeQuiet();
557 FILE *out = optionsHolder.startupInfoFile();
558 printBinaryInformation(out, impl_->programContext_,
559 optionsHolder.binaryInfoSettings());
567 CommandLineModuleSettings settings;
568 module->init(&settings);
569 optionsHolder.adjustFromSettings(settings);
571 gmx_set_max_backup_count(optionsHolder.shouldBackup() ? -1 : 0);
573 // Open the debug file.
574 if (optionsHolder.debugLevel() > 0)
576 std::string filename(impl_->programContext_.programName());
577 if (gmx_node_num() > 1)
579 filename.append(formatString("%d", gmx_node_rank()));
581 filename.append(".debug");
583 fprintf(stderr, "Will write debug log file: %s\n", filename.c_str());
584 gmx_init_debug(optionsHolder.debugLevel(), filename.c_str());
586 // Set the nice level unless disabled in the configuration.
587 if (optionsHolder.niceLevel() != 0)
589 static bool bNiceSet = false; // Only set it once.
592 // TODO: Diagnostic if this fails and the user explicitly requested it.
593 gmx_set_nice(optionsHolder.niceLevel());
597 if (optionsHolder.enableFPExceptions())
599 //TODO: currently it is always enabled for mdrun (verlet) and tests.
600 gmx_feenableexcept();
604 if (!(module == impl_->helpModule_ && !bMaster))
606 rc = module->run(argc, argv);
616 int CommandLineModuleManager::runAsMainSingleModule(
617 int argc, char *argv[], CommandLineModuleInterface *module)
619 CommandLineProgramContext &programContext = gmx::initForCommandLine(&argc, &argv);
622 CommandLineModuleManager manager(NULL, &programContext);
623 manager.setSingleModule(module);
624 int rc = manager.run(argc, argv);
625 gmx::finalizeForCommandLine();
628 catch (const std::exception &ex)
630 printFatalErrorMessage(stderr, ex);
631 return processExceptionAtExitForCommandLine(ex);
636 int CommandLineModuleManager::runAsMainCMain(
637 int argc, char *argv[], CMainFunction mainFunction)
639 CMainCommandLineModule module(argv[0], NULL, mainFunction);
640 return runAsMainSingleModule(argc, argv, &module);
643 /********************************************************************
644 * CommandLineModuleGroupData
647 void CommandLineModuleGroupData::addModule(const char *name,
648 const char *description)
650 CommandLineModuleMap::const_iterator moduleIter = allModules_.find(name);
651 GMX_RELEASE_ASSERT(moduleIter != allModules_.end(),
652 "Non-existent module added to a group");
653 if (description == NULL)
655 description = moduleIter->second->shortDescription();
656 GMX_RELEASE_ASSERT(description != NULL,
657 "Module without a description added to a group");
659 std::string tag(formatString("%s-%s", binaryName_, name));
660 modules_.push_back(std::make_pair(tag, description));
663 /********************************************************************
664 * CommandLineModuleGroup
667 void CommandLineModuleGroup::addModule(const char *name)
669 impl_->addModule(name, NULL);
672 void CommandLineModuleGroup::addModuleWithDescription(const char *name,
673 const char *description)
675 impl_->addModule(name, description);