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.
35 /*! \libinternal \file
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 "gromacs/onlinehelp/ihelptopic.h"
47 #include "gromacs/utility/classhelpers.h"
48 #include "gromacs/utility/uniqueptr.h"
53 class CommandLineModuleGroup;
54 class CommandLineModuleGroupData;
55 class CommandLineProgramContext;
56 class ICommandLineModule;
57 class IFileOutputRedirector;
59 //! \addtogroup module_commandline
62 //! Smart pointer type for managing a ICommandLineModule.
63 typedef gmx_unique_ptr<ICommandLineModule>::type
64 CommandLineModulePointer;
66 /*! \libinternal \brief
67 * Implements a wrapper command-line interface for multiple modules.
71 int main(int argc, char *argv[])
73 gmx::CommandLineProgramContext &programContext = gmx::initForCommandLine(&argc, &argv);
76 gmx::CommandLineModuleManager manager("gmx", &programContext);
77 // <register all necessary modules>
78 int rc = manager.run(argc, argv);
79 gmx::finalizeForCommandLine();
82 catch (const std::exception &ex)
84 gmx::printFatalErrorMessage(stderr, ex);
85 return gmx::processExceptionAtExitForCommandLine(ex);
90 * \see page_wrapperbinary
93 class CommandLineModuleManager
96 //! Function pointer type for a C main function.
97 typedef int (*CMainFunction)(int argc, char *argv[]);
100 * Implements a main() method that runs a single module.
102 * \param argc \c argc passed to main().
103 * \param argv \c argv passed to main().
104 * \param module Module to run.
106 * This method allows for uniform behavior for binaries that only
107 * contain a single module without duplicating any of the
108 * implementation from CommandLineModuleManager (startup headers,
109 * common options etc.).
111 * The signature assumes that \p module construction does not throw
112 * (because otherwise the caller would need to duplicate all the
113 * exception handling code). It is possible to move the construction
114 * inside the try/catch in this method using an indirection similar to
115 * TrajectoryAnalysisCommandLineRunner::runAsMain(), but until that is
116 * necessary, the current approach leads to simpler code.
120 int main(int argc, char *argv[])
122 CustomCommandLineModule module;
123 return gmx::CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
127 * Does not throw. All exceptions are caught and handled internally.
129 static int runAsMainSingleModule(int argc, char *argv[],
130 ICommandLineModule *module);
132 * Implements a main() method that runs a given function.
134 * \param argc \c argc passed to main().
135 * \param argv \c argv passed to main().
136 * \param mainFunction The main()-like method to wrap.
138 * This method creates a dummy command-line module that does its
139 * processing by calling \p mainFunction; see addModuleCMain() for
140 * details. It then runs this module with runAsMainSingleModule().
141 * This allows the resulting executable to handle common options and do
142 * other common actions (e.g., startup headers) without duplicate code
143 * in the main methods.
147 int my_main(int argc, char *argv[])
152 int main(int argc, char *argv[])
154 return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, &my_main);
158 * Does not throw. All exceptions are caught and handled internally.
160 static int runAsMainCMain(int argc, char *argv[],
161 CMainFunction mainFunction);
164 * Initializes a command-line module manager.
166 * \param[in] binaryName Name of the running binary
167 * (without Gromacs binary suffix or .exe on Windows).
168 * \param programContext Program information for the running binary.
169 * \throws std::bad_alloc if out of memory.
171 * \p binaryName is used to detect when the binary is run through a
172 * symlink, and automatically invoke a matching module in such a case.
174 * \p programInfo is non-const to allow the manager to amend it based
175 * on the actual module that is getting executed.
177 CommandLineModuleManager(const char *binaryName,
178 CommandLineProgramContext *programContext);
179 ~CommandLineModuleManager();
182 * Sets the module manager to quiet mode: don't print anything.
184 * \param[in] bQuiet Whether the module manager should remain silent.
186 * Normally, the module manager prints out some information to `stderr`
187 * before it starts the module and after it finishes. This removes
188 * that output, which is useful in particular for unit tests so that
189 * they don't spam `stderr`.
191 void setQuiet(bool bQuiet);
193 * Redirects the output of the module manager to a file.
195 * \param[in] output File redirector to use for output.
197 * Normally, the module manager prints explicitly requested text such
198 * as help output to `stdout`, but this method can be used to redirect
199 * that output to a file. For exporting help from the module manager,
200 * several files are written, and can be redirected with this method as
203 * This is used for unit tests, either to keep them quiet or to verify
204 * that output. To keep implementation options open, behavior with
205 * `output == NULL` is undefined and should not be relied on.
206 * For tests, there should only be need to call this a single time,
207 * right after creating the manager.
209 void setOutputRedirector(IFileOutputRedirector *output);
212 * Makes the manager always run a single module.
214 * \param module Module to run.
216 * This method disables all mechanisms for selecting a module, and
217 * directly passes all command-line arguments to \p module.
218 * Help arguments are an exception: these are still recognized by the
219 * manager and translated into a call to
220 * ICommandLineModule::writeHelp().
222 * This is public mainly for unit testing purposes; for other code,
223 * runAsMainSingleModule() typically provides the desired
228 void setSingleModule(ICommandLineModule *module);
230 * Adds a given module to this manager.
232 * \param module Module to add.
233 * \throws std::bad_alloc if out of memory.
235 * The manager takes ownership of the object.
237 * This method is public mostly for testing purposes; for typical uses,
238 * registerModule() is a more convenient way of adding modules.
240 * \see registerModule()
242 void addModule(CommandLineModulePointer module);
244 * Adds a module that runs a given main()-like function.
246 * \param[in] name Name for the module.
247 * \param[in] shortDescription One-line description for the module.
248 * \param[in] mainFunction Main function to wrap.
249 * \throws std::bad_alloc if out of memory.
251 * There is normally no need to call this method outside the Gromacs
252 * library. User code usually wants to use runAsMainCMain().
254 * \p name and \p shortDescription should be string constants, or the
255 * caller should otherwise ensure that they stay in scope for the
256 * duration the CommandLineModuleManager object exists.
257 * \p mainFunction should call parse_common_args() to process its
258 * command-line arguments.
260 void addModuleCMain(const char *name, const char *shortDescription,
261 CMainFunction mainFunction);
263 * Registers a module of a certain type to this manager.
265 * \tparam Module Type of module to register.
266 * \throws std::bad_alloc if out of memory.
268 * \p Module must be default-constructible and implement
269 * ICommandLineModule.
271 * This method is provided as a convenient alternative to addModule()
272 * for cases where each module is implemented by a different type
273 * (which should be the case for typical situations outside unit
276 template <class Module>
277 void registerModule()
279 addModule(CommandLineModulePointer(new Module));
283 * Adds a group for modules to use in help output.
285 * \param[in] title Short title for the group.
286 * \returns Handle that can be used to add modules to the group.
287 * \throws std::bad_alloc if out of memory.
289 * Creates a group that is used to structure the list of all modules in
290 * help output. Modules are added to the group using the returned
293 CommandLineModuleGroup addModuleGroup(const char *title);
296 * Makes given help topic available through the manager's help module.
298 * \param[in] topic Help topic to add.
299 * \throws std::bad_alloc if out of memory.
301 * The manager takes ownership of the help topic.
303 void addHelpTopic(HelpTopicPointer topic);
306 * Runs a module based on given command line.
308 * \param[in] argc Number of elements in \p argv.
309 * \param[in] argv Command-line arguments.
310 * \throws unspecified Throws any exception that the selected module
312 * \returns Exit code for the program.
313 * \retval 0 on successful termination.
314 * \retval 2 if no module is specified, or if the module is not found.
316 * Runs the module whose name matches \p argv[1].
318 int run(int argc, char *argv[]);
323 PrivateImplPointer<Impl> impl_;
326 /*! \libinternal \brief
327 * Handle to add content to a group added with
328 * CommandLineModuleManager::addModuleGroup().
330 * This class only provides a public interface to construct a module group for
331 * CommandLineModuleManager, and has semantics similar to a pointer: copies all
332 * point to the same group. The actual state of the group is maintained in an
333 * internal implementation class.
337 class CommandLineModuleGroup
340 /*! \cond internal */
341 //! Shorthand for the implementation type that holds all the data.
342 typedef CommandLineModuleGroupData Impl;
344 //! Creates a new group (only called by CommandLineModuleManager).
345 explicit CommandLineModuleGroup(Impl *impl) : impl_(impl) {}
349 * Adds a module to this group.
351 * \param[in] name Name of the module.
352 * \throws std::bad_alloc if out of memory.
354 * This works as addModuleWithDescription(), but uses the short
355 * description of the module itself as the description.
357 * \see addModuleWithDescription()
359 void addModule(const char *name);
361 * Adds a module to this group with a custom description.
363 * \param[in] name Name of the module.
364 * \param[in] description Description of the module in this group.
365 * \throws std::bad_alloc if out of memory.
367 * \p name must name a module added into the CommandLineModuleManager.
368 * It is possible to add the same module into multiple groups.
370 void addModuleWithDescription(const char *name, const char *description);
373 //! Pointer to the data owned by CommandLineModuleManager.