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,2021, 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"
54 class CommandLineModuleGroup;
55 class CommandLineModuleGroupData;
56 class CommandLineModuleSettings;
57 class CommandLineProgramContext;
58 class ICommandLineModule;
59 class IFileOutputRedirector;
61 //! \addtogroup module_commandline
64 //! Smart pointer type for managing a ICommandLineModule.
65 typedef std::unique_ptr<ICommandLineModule> CommandLineModulePointer;
67 /*! \libinternal \brief
68 * Implements a wrapper command-line interface for multiple modules.
72 int main(int argc, char *argv[])
74 gmx::CommandLineProgramContext &programContext = gmx::initForCommandLine(&argc, &argv);
77 gmx::CommandLineModuleManager manager("gmx", &programContext);
78 // <register all necessary modules>
79 int rc = manager.run(argc, argv);
80 gmx::finalizeForCommandLine();
83 catch (const std::exception &ex)
85 gmx::printFatalErrorMessage(stderr, ex);
86 return gmx::processExceptionAtExitForCommandLine(ex);
91 * \see page_wrapperbinary
94 class CommandLineModuleManager
97 //! Function pointer type for a C main function.
98 typedef int (*CMainFunction)(int argc, char* argv[]);
99 //! Function pointer to a settings provider.
100 typedef void (*InitSettingsFunction)(CommandLineModuleSettings* settings);
103 * Implements a main() method that runs a single module.
105 * \param argc \c argc passed to main().
106 * \param argv \c argv passed to main().
107 * \param module Module to run.
109 * This method allows for uniform behavior for binaries that only
110 * contain a single module without duplicating any of the
111 * implementation from CommandLineModuleManager (startup headers,
112 * common options etc.).
114 * The signature assumes that \p module construction does not throw
115 * (because otherwise the caller would need to duplicate all the
116 * exception handling code). It is possible to move the construction
117 * inside the try/catch in this method using an indirection similar to
118 * TrajectoryAnalysisCommandLineRunner::runAsMain(), but until that is
119 * necessary, the current approach leads to simpler code.
123 int main(int argc, char *argv[])
125 CustomCommandLineModule module;
126 return gmx::CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
130 * Does not throw. All exceptions are caught and handled internally.
132 static int runAsMainSingleModule(int argc, char* argv[], ICommandLineModule* module);
134 * Implements a main() method that runs a given function.
136 * \param argc \c argc passed to main().
137 * \param argv \c argv passed to main().
138 * \param mainFunction The main()-like method to wrap.
140 * This method creates a dummy command-line module that does its
141 * processing by calling \p mainFunction; see addModuleCMain() for
142 * details. It then runs this module with runAsMainSingleModule().
143 * This allows the resulting executable to handle common options and do
144 * other common actions (e.g., startup headers) without duplicate code
145 * in the main methods.
149 int my_main(int argc, char *argv[])
154 int main(int argc, char *argv[])
156 return gmx::CommandLineModuleManager::runAsMainCMain(argc, argv, &my_main);
160 * Does not throw. All exceptions are caught and handled internally.
162 static int runAsMainCMain(int argc, char* argv[], CMainFunction mainFunction);
164 * Implements a main() method that runs a given function with custom
167 * This method does the same as runAsMainCMain(), but additionally
168 * calls \p settingsFunction to initialize CommandLineModuleSettings.
169 * This allows specifying, e.g., a different default nice level.
171 static int runAsMainCMainWithSettings(int argc,
173 CMainFunction mainFunction,
174 InitSettingsFunction settingsFunction);
177 * Initializes a command-line module manager.
179 * \param[in] binaryName Name of the running binary
180 * (without Gromacs binary suffix or .exe on Windows).
181 * \param programContext Program information for the running binary.
182 * \throws std::bad_alloc if out of memory.
184 * \p binaryName is used to detect when the binary is run through a
185 * symlink, and automatically invoke a matching module in such a case.
187 * \p programInfo is non-const to allow the manager to amend it based
188 * on the actual module that is getting executed.
190 CommandLineModuleManager(const char* binaryName, CommandLineProgramContext* programContext);
191 ~CommandLineModuleManager();
194 * Sets the module manager to quiet mode: don't print anything.
196 * \param[in] bQuiet Whether the module manager should remain silent.
198 * Normally, the module manager prints out some information to `stderr`
199 * before it starts the module and after it finishes. This removes
200 * that output, which is useful in particular for unit tests so that
201 * they don't spam `stderr`.
203 void setQuiet(bool bQuiet);
205 * Redirects the output of the module manager to a file.
207 * \param[in] output File redirector to use for output.
209 * Normally, the module manager prints explicitly requested text such
210 * as help output to `stdout`, but this method can be used to redirect
211 * that output to a file. For exporting help from the module manager,
212 * several files are written, and can be redirected with this method as
215 * This is used for unit tests, either to keep them quiet or to verify
216 * that output. To keep implementation options open, behavior with
217 * `output == NULL` is undefined and should not be relied on.
218 * For tests, there should only be need to call this a single time,
219 * right after creating the manager.
221 void setOutputRedirector(IFileOutputRedirector* output);
224 * Makes the manager always run a single module.
226 * \param module Module to run.
228 * This method disables all mechanisms for selecting a module, and
229 * directly passes all command-line arguments to \p module.
230 * Help arguments are an exception: these are still recognized by the
231 * manager and translated into a call to
232 * ICommandLineModule::writeHelp().
234 * This is public mainly for unit testing purposes; for other code,
235 * runAsMainSingleModule() typically provides the desired
240 void setSingleModule(ICommandLineModule* module);
242 * Adds a given module to this manager.
244 * \param module Module to add.
245 * \throws std::bad_alloc if out of memory.
247 * The manager takes ownership of the object.
249 * This method is public mostly for testing purposes; for typical uses,
250 * registerModule() is a more convenient way of adding modules.
252 * \see registerModule()
254 void addModule(CommandLineModulePointer module);
256 * Adds a module that runs a given main()-like function.
258 * \param[in] name Name for the module.
259 * \param[in] shortDescription One-line description for the module.
260 * \param[in] mainFunction Main function to wrap.
261 * \throws std::bad_alloc if out of memory.
263 * There is normally no need to call this method outside the Gromacs
264 * library. User code usually wants to use runAsMainCMain().
266 * \p name and \p shortDescription should be string constants, or the
267 * caller should otherwise ensure that they stay in scope for the
268 * duration the CommandLineModuleManager object exists.
269 * \p mainFunction should call parse_common_args() to process its
270 * command-line arguments.
272 void addModuleCMain(const char* name, const char* shortDescription, CMainFunction mainFunction);
274 * Adds a module that runs a given main()-like function with custom
277 * This method does the same as runAsMainCMain(), but additionally
278 * calls \p settingsFunction to initialize CommandLineModuleSettings.
279 * This allows specifying, e.g., a different default nice level.
281 void addModuleCMainWithSettings(const char* name,
282 const char* shortDescription,
283 CMainFunction mainFunction,
284 InitSettingsFunction settingsFunction);
286 * Registers a module of a certain type to this manager.
288 * \tparam Module Type of module to register.
289 * \throws std::bad_alloc if out of memory.
291 * \p Module must be default-constructible and implement
292 * ICommandLineModule.
294 * This method is provided as a convenient alternative to addModule()
295 * for cases where each module is implemented by a different type
296 * (which should be the case for typical situations outside unit
299 template<class Module>
300 void registerModule()
302 addModule(CommandLineModulePointer(new Module));
306 * Adds a group for modules to use in help output.
308 * \param[in] title Short title for the group.
309 * \returns Handle that can be used to add modules to the group.
310 * \throws std::bad_alloc if out of memory.
312 * Creates a group that is used to structure the list of all modules in
313 * help output. Modules are added to the group using the returned
316 CommandLineModuleGroup addModuleGroup(const char* title);
319 * Makes given help topic available through the manager's help module.
321 * \param[in] topic Help topic to add.
322 * \throws std::bad_alloc if out of memory.
324 * The manager takes ownership of the help topic.
326 void addHelpTopic(HelpTopicPointer topic);
329 * Runs a module based on given command line.
331 * \param[in] argc Number of elements in \p argv.
332 * \param[in] argv Command-line arguments.
333 * \throws unspecified Throws any exception that the selected module
335 * \returns Exit code for the program.
336 * \retval 0 on successful termination.
337 * \retval 2 if no module is specified, or if the module is not found.
339 * Runs the module whose name matches \p argv[1].
341 int run(int argc, char* argv[]);
346 std::unique_ptr<Impl> impl_;
349 /*! \libinternal \brief
350 * Handle to add content to a group added with
351 * CommandLineModuleManager::addModuleGroup().
353 * This class only provides a public interface to construct a module group for
354 * CommandLineModuleManager, and has semantics similar to a pointer: copies all
355 * point to the same group. The actual state of the group is maintained in an
356 * internal implementation class.
360 class CommandLineModuleGroup
363 /*! \cond internal */
364 //! Shorthand for the implementation type that holds all the data.
365 typedef CommandLineModuleGroupData Impl;
367 //! Creates a new group (only called by CommandLineModuleManager).
368 explicit CommandLineModuleGroup(Impl* impl) : impl_(impl) {}
372 * Adds a module to this group.
374 * \param[in] name Name of the module.
375 * \throws std::bad_alloc if out of memory.
377 * This works as addModuleWithDescription(), but uses the short
378 * description of the module itself as the description.
380 * \see addModuleWithDescription()
382 void addModule(const char* name);
384 * Adds a module to this group with a custom description.
386 * \param[in] name Name of the module.
387 * \param[in] description Description of the module in this group.
388 * \throws std::bad_alloc if out of memory.
390 * \p name must name a module added into the CommandLineModuleManager.
391 * It is possible to add the same module into multiple groups.
393 void addModuleWithDescription(const char* name, const char* description);
396 //! Pointer to the data owned by CommandLineModuleManager.