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 * Declares gmx::CommandLineModuleManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_commandline
43 #ifndef GMX_COMMANDLINE_CMDLINEMODULEMANAGER_H
44 #define GMX_COMMANDLINE_CMDLINEMODULEMANAGER_H
46 #include "../onlinehelp/helptopicinterface.h"
47 #include "../utility/common.h"
48 #include "../utility/uniqueptr.h"
53 class CommandLineModuleInterface;
56 //! Smart pointer type for managing a CommandLineModuleInterface.
57 typedef gmx_unique_ptr<CommandLineModuleInterface>::type
58 CommandLineModulePointer;
61 * Implements a wrapper command-line interface for multiple modules.
65 int main(int argc, char *argv[])
67 const gmx::ProgramInfo &programInfo =
68 gmx::ProgramInfo::init("gmx", argc, argv);
71 gmx::CommandLineModuleManager manager(programInfo);
72 // <register all necessary modules>
73 return manager.run(argc, argv);
75 catch (const std::exception &ex)
77 gmx::printFatalErrorMessage(stderr, ex);
84 * \ingroup module_commandline
86 class CommandLineModuleManager
90 * Implements a main() method that runs a single module.
92 * \param argc \c argc passed to main().
93 * \param argv \c argv passed to main().
94 * \param module Module to run.
96 * This method allows for uniform behavior for binaries that only
97 * contain a single module without duplicating any of the
98 * implementation from CommandLineModuleManager (startup headers,
99 * common options etc.).
101 * The signature assumes that \p module construction does not throw
102 * (because otherwise the caller would need to duplicate all the
103 * exception handling code). It is possible to move the construction
104 * inside the try/catch in this method using an indirection similar to
105 * TrajectoryAnalysisCommandLineRunner::runAsMain(), but until that is
106 * necessary, the current approach leads to simpler code.
110 int main(int argc, char *argv[])
112 CustomCommandLineModule module;
113 return gmx::CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
117 * Does not throw. All exceptions are caught and handled internally.
119 static int runAsMainSingleModule(int argc, char *argv[],
120 CommandLineModuleInterface *module);
123 * Initializes a command-line module manager.
125 * \param programInfo Program information for the running binary.
126 * \throws std::bad_alloc if out of memory.
128 * The binary name is used to detect when the binary is run through a
129 * symlink, and automatically invoke a matching module in such a case.
131 * \p programInfo is non-const to allow the manager to amend it based
132 * on the actual module that is getting executed.
134 explicit CommandLineModuleManager(ProgramInfo *programInfo);
135 ~CommandLineModuleManager();
138 * Sets the module manager to quiet mode: don't print anything.
140 * \param[in] bQuiet Whether the module manager should remain silent.
142 * Normally, the module manager prints out some information to stderr
143 * before it starts the module and after it finishes. This removes
144 * that output, which is useful in particular for unit tests so that
145 * they don't spam stderr.
147 void setQuiet(bool bQuiet);
150 * Adds a given module to this manager.
152 * \param module Module to add.
153 * \throws std::bad_alloc if out of memory.
155 * The manager takes ownership of the object.
157 * This method is public mostly for testing purposes; for typical uses,
158 * registerModule() is a more convenient way of adding modules.
160 * \see registerModule()
162 void addModule(CommandLineModulePointer module);
164 * Registers a module of a certain type to this manager.
166 * \tparam Module Type of module to register.
167 * \throws std::bad_alloc if out of memory.
169 * \p Module must be default-constructible and implement
170 * CommandLineModuleInterface.
172 * This method is provided as a convenient alternative to addModule()
173 * for cases where each module is implemented by a different type
174 * (which should be the case for typical situations outside unit
177 template <class Module>
178 void registerModule()
180 addModule(CommandLineModulePointer(new Module));
184 * Make given help topic available through the manager's help module.
186 * \param[in] topic Help topic to add.
187 * \throws std::bad_alloc if out of memory.
189 * The manager takes ownership of the help topic.
191 void addHelpTopic(HelpTopicPointer topic);
194 * Runs a module based on given command line.
196 * \param[in] argc Number of elements in \p argv.
197 * \param[in] argv Command-line arguments.
198 * \throws unspecified Throws any exception that the selected module
200 * \returns Exit code for the program.
201 * \retval 0 on successful termination.
202 * \retval 2 if no module is specified, or if the module is not found.
204 * Runs the module whose name matches \p argv[1].
206 int run(int argc, char *argv[]);
211 PrivateImplPointer<Impl> impl_;