2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \libinternal \file
38 * Declares gmx::CommandLineModuleManager.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_commandline
44 #ifndef GMX_COMMANDLINE_CMDLINEMODULEMANAGER_H
45 #define GMX_COMMANDLINE_CMDLINEMODULEMANAGER_H
49 #include "gromacs/onlinehelp/ihelptopic.h"
50 #include "gromacs/utility/classhelpers.h"
55 class CommandLineModuleGroup
;
56 class CommandLineModuleGroupData
;
57 class CommandLineModuleSettings
;
58 class CommandLineProgramContext
;
59 class ICommandLineModule
;
60 class IFileOutputRedirector
;
62 //! \addtogroup module_commandline
65 //! Smart pointer type for managing a ICommandLineModule.
66 typedef std::unique_ptr
<ICommandLineModule
> CommandLineModulePointer
;
68 /*! \libinternal \brief
69 * Implements a wrapper command-line interface for multiple modules.
73 int main(int argc, char *argv[])
75 gmx::CommandLineProgramContext &programContext = gmx::initForCommandLine(&argc, &argv);
78 gmx::CommandLineModuleManager manager("gmx", &programContext);
79 // <register all necessary modules>
80 int rc = manager.run(argc, argv);
81 gmx::finalizeForCommandLine();
84 catch (const std::exception &ex)
86 gmx::printFatalErrorMessage(stderr, ex);
87 return gmx::processExceptionAtExitForCommandLine(ex);
92 * \see page_wrapperbinary
95 class CommandLineModuleManager
98 //! Function pointer type for a C main function.
99 typedef int (*CMainFunction
)(int argc
, char* argv
[]);
100 //! Function pointer to a settings provider.
101 typedef void (*InitSettingsFunction
)(CommandLineModuleSettings
* settings
);
104 * Implements a main() method that runs a single module.
106 * \param argc \c argc passed to main().
107 * \param argv \c argv passed to main().
108 * \param module Module to run.
110 * This method allows for uniform behavior for binaries that only
111 * contain a single module without duplicating any of the
112 * implementation from CommandLineModuleManager (startup headers,
113 * common options etc.).
115 * The signature assumes that \p module construction does not throw
116 * (because otherwise the caller would need to duplicate all the
117 * exception handling code). It is possible to move the construction
118 * inside the try/catch in this method using an indirection similar to
119 * TrajectoryAnalysisCommandLineRunner::runAsMain(), but until that is
120 * necessary, the current approach leads to simpler code.
124 int main(int argc, char *argv[])
126 CustomCommandLineModule module;
127 return gmx::CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
131 * Does not throw. All exceptions are caught and handled internally.
133 static int runAsMainSingleModule(int argc
, char* argv
[], ICommandLineModule
* module
);
135 * Implements a main() method that runs a given function.
137 * \param argc \c argc passed to main().
138 * \param argv \c argv passed to main().
139 * \param mainFunction The main()-like method to wrap.
141 * This method creates a dummy command-line module that does its
142 * processing by calling \p mainFunction; see addModuleCMain() for
143 * details. It then runs this module with runAsMainSingleModule().
144 * This allows the resulting executable to handle common options and do
145 * other common actions (e.g., startup headers) without duplicate code
146 * in the main methods.
150 int my_main(int argc, char *argv[])
155 int main(int argc, char *argv[])
157 return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, &my_main);
161 * Does not throw. All exceptions are caught and handled internally.
163 static int runAsMainCMain(int argc
, char* argv
[], CMainFunction mainFunction
);
165 * Implements a main() method that runs a given function with custom
168 * This method does the same as runAsMainCMain(), but additionally
169 * calls \p settingsFunction to initialize CommandLineModuleSettings.
170 * This allows specifying, e.g., a different default nice level.
172 static int runAsMainCMainWithSettings(int argc
,
174 CMainFunction mainFunction
,
175 InitSettingsFunction settingsFunction
);
178 * Initializes a command-line module manager.
180 * \param[in] binaryName Name of the running binary
181 * (without Gromacs binary suffix or .exe on Windows).
182 * \param programContext Program information for the running binary.
183 * \throws std::bad_alloc if out of memory.
185 * \p binaryName is used to detect when the binary is run through a
186 * symlink, and automatically invoke a matching module in such a case.
188 * \p programInfo is non-const to allow the manager to amend it based
189 * on the actual module that is getting executed.
191 CommandLineModuleManager(const char* binaryName
, CommandLineProgramContext
* programContext
);
192 ~CommandLineModuleManager();
195 * Sets the module manager to quiet mode: don't print anything.
197 * \param[in] bQuiet Whether the module manager should remain silent.
199 * Normally, the module manager prints out some information to `stderr`
200 * before it starts the module and after it finishes. This removes
201 * that output, which is useful in particular for unit tests so that
202 * they don't spam `stderr`.
204 void setQuiet(bool bQuiet
);
206 * Redirects the output of the module manager to a file.
208 * \param[in] output File redirector to use for output.
210 * Normally, the module manager prints explicitly requested text such
211 * as help output to `stdout`, but this method can be used to redirect
212 * that output to a file. For exporting help from the module manager,
213 * several files are written, and can be redirected with this method as
216 * This is used for unit tests, either to keep them quiet or to verify
217 * that output. To keep implementation options open, behavior with
218 * `output == NULL` is undefined and should not be relied on.
219 * For tests, there should only be need to call this a single time,
220 * right after creating the manager.
222 void setOutputRedirector(IFileOutputRedirector
* output
);
225 * Makes the manager always run a single module.
227 * \param module Module to run.
229 * This method disables all mechanisms for selecting a module, and
230 * directly passes all command-line arguments to \p module.
231 * Help arguments are an exception: these are still recognized by the
232 * manager and translated into a call to
233 * ICommandLineModule::writeHelp().
235 * This is public mainly for unit testing purposes; for other code,
236 * runAsMainSingleModule() typically provides the desired
241 void setSingleModule(ICommandLineModule
* module
);
243 * Adds a given module to this manager.
245 * \param module Module to add.
246 * \throws std::bad_alloc if out of memory.
248 * The manager takes ownership of the object.
250 * This method is public mostly for testing purposes; for typical uses,
251 * registerModule() is a more convenient way of adding modules.
253 * \see registerModule()
255 void addModule(CommandLineModulePointer module
);
257 * Adds a module that runs a given main()-like function.
259 * \param[in] name Name for the module.
260 * \param[in] shortDescription One-line description for the module.
261 * \param[in] mainFunction Main function to wrap.
262 * \throws std::bad_alloc if out of memory.
264 * There is normally no need to call this method outside the Gromacs
265 * library. User code usually wants to use runAsMainCMain().
267 * \p name and \p shortDescription should be string constants, or the
268 * caller should otherwise ensure that they stay in scope for the
269 * duration the CommandLineModuleManager object exists.
270 * \p mainFunction should call parse_common_args() to process its
271 * command-line arguments.
273 void addModuleCMain(const char* name
, const char* shortDescription
, CMainFunction mainFunction
);
275 * Adds a module that runs a given main()-like function with custom
278 * This method does the same as runAsMainCMain(), but additionally
279 * calls \p settingsFunction to initialize CommandLineModuleSettings.
280 * This allows specifying, e.g., a different default nice level.
282 void addModuleCMainWithSettings(const char* name
,
283 const char* shortDescription
,
284 CMainFunction mainFunction
,
285 InitSettingsFunction settingsFunction
);
287 * Registers a module of a certain type to this manager.
289 * \tparam Module Type of module to register.
290 * \throws std::bad_alloc if out of memory.
292 * \p Module must be default-constructible and implement
293 * ICommandLineModule.
295 * This method is provided as a convenient alternative to addModule()
296 * for cases where each module is implemented by a different type
297 * (which should be the case for typical situations outside unit
300 template<class Module
>
301 void registerModule()
303 addModule(CommandLineModulePointer(new Module
));
307 * Adds a group for modules to use in help output.
309 * \param[in] title Short title for the group.
310 * \returns Handle that can be used to add modules to the group.
311 * \throws std::bad_alloc if out of memory.
313 * Creates a group that is used to structure the list of all modules in
314 * help output. Modules are added to the group using the returned
317 CommandLineModuleGroup
addModuleGroup(const char* title
);
320 * Makes given help topic available through the manager's help module.
322 * \param[in] topic Help topic to add.
323 * \throws std::bad_alloc if out of memory.
325 * The manager takes ownership of the help topic.
327 void addHelpTopic(HelpTopicPointer topic
);
330 * Runs a module based on given command line.
332 * \param[in] argc Number of elements in \p argv.
333 * \param[in] argv Command-line arguments.
334 * \throws unspecified Throws any exception that the selected module
336 * \returns Exit code for the program.
337 * \retval 0 on successful termination.
338 * \retval 2 if no module is specified, or if the module is not found.
340 * Runs the module whose name matches \p argv[1].
342 int run(int argc
, char* argv
[]);
347 PrivateImplPointer
<Impl
> impl_
;
350 /*! \libinternal \brief
351 * Handle to add content to a group added with
352 * CommandLineModuleManager::addModuleGroup().
354 * This class only provides a public interface to construct a module group for
355 * CommandLineModuleManager, and has semantics similar to a pointer: copies all
356 * point to the same group. The actual state of the group is maintained in an
357 * internal implementation class.
361 class CommandLineModuleGroup
364 /*! \cond internal */
365 //! Shorthand for the implementation type that holds all the data.
366 typedef CommandLineModuleGroupData Impl
;
368 //! Creates a new group (only called by CommandLineModuleManager).
369 explicit CommandLineModuleGroup(Impl
* impl
) : impl_(impl
) {}
373 * Adds a module to this group.
375 * \param[in] name Name of the module.
376 * \throws std::bad_alloc if out of memory.
378 * This works as addModuleWithDescription(), but uses the short
379 * description of the module itself as the description.
381 * \see addModuleWithDescription()
383 void addModule(const char* name
);
385 * Adds a module to this group with a custom description.
387 * \param[in] name Name of the module.
388 * \param[in] description Description of the module in this group.
389 * \throws std::bad_alloc if out of memory.
391 * \p name must name a module added into the CommandLineModuleManager.
392 * It is possible to add the same module into multiple groups.
394 void addModuleWithDescription(const char* name
, const char* description
);
397 //! Pointer to the data owned by CommandLineModuleManager.