2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2017, 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 * Declares gmx::ICommandLineOptionsModule and supporting routines.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_commandline
43 #ifndef GMX_COMMANDLINE_CMDLINEOPTIONSMODULE_H
44 #define GMX_COMMANDLINE_CMDLINEOPTIONSMODULE_H
49 #include "gromacs/commandline/cmdlinemodule.h"
54 template <typename T
> class ArrayRef
;
56 class CommandLineModuleManager
;
57 class ICommandLineModule
;
58 class ICommandLineOptionsModule
;
59 class IOptionsBehavior
;
60 class IOptionsContainer
;
62 //! Smart pointer to manage an ICommandLineOptionsModule.
63 typedef std::unique_ptr
<ICommandLineOptionsModule
>
64 ICommandLineOptionsModulePointer
;
67 * Settings to pass information between a CommandLineOptionsModule and generic
71 * \ingroup module_commandline
73 class ICommandLineOptionsModuleSettings
77 * Sets the help text for the module from string array.
79 * \param[in] help String array to set as the description.
80 * \throws std::bad_alloc if out of memory.
82 * Formatting for the help text is described on \ref page_onlinehelp.
86 const char *const desc[] = {
87 "This is the description",
91 settings->setHelpText(desc);
94 virtual void setHelpText(const ArrayRef
<const char *const> &help
) = 0;
96 * Adds an option behavior that performs actions before
97 * ICommandLineOptionsModule::run() is called.
99 * For now, this takes a shared_ptr to make it easier for the caller to
100 * keep a reference to the behavior, but the behavior should be treated
101 * as owned by the options module after this call.
103 virtual void addOptionsBehavior(
104 const std::shared_ptr
<IOptionsBehavior
> &behavior
) = 0;
107 // Disallow deletion through the interface.
108 // (no need for the virtual, but some compilers warn otherwise)
109 virtual ~ICommandLineOptionsModuleSettings();
113 * Module that can be run from a command line and uses gmx::Options for
114 * argument processing.
116 * This class provides a higher-level interface on top of
117 * gmx::ICommandLineModule for cases where gmx::Options will be used
118 * for declaring the command-line arguments. The module only needs to declare
119 * the options it uses, and the framework takes care of command-line parsing
120 * and help output. The module typically consists of the following parts:
121 * - init() allows for some interaction between the module and the framework
122 * when running the module; see ICommandLineModule::init(). If no
123 * such customization is necessary, an empty implementation is sufficient.
124 * - initOptions() is called both for running the module and for printing help
125 * for the module, and it should add the options that the module
126 * understands. Values provided for the options are typically stored in
128 * - optionsFinished() can be implemented in case additional processing is
129 * needed (e.g., checking whether an option was set by the user).
130 * - run() is called when running the module, after command-line options have
131 * been parsed and their values stored in the corresponding member
134 * registerModule(), runAsMain(), or createModule() can be used to use modules
135 * of this type in all contexts where a gmx::ICommandLineModule is
136 * expected. These methods create a gmx::ICommandLineModule
137 * implementation that contains the common code needed to parse command-line
138 * options and write help, based on the information provided from the methods
142 * \ingroup module_commandline
144 class ICommandLineOptionsModule
148 * Function pointer to a factory method that returns an interface of
151 * \returns Module to run.
152 * \throws std::bad_alloc if out of memory.
154 typedef std::function
<ICommandLineOptionsModulePointer()> FactoryMethod
;
157 * Creates a ICommandLineModule to run the specified module.
159 * \param[in] name Name for the module.
160 * \param[in] description Short description for the module.
161 * \param[in] module Module to run.
162 * \returns ICommandLineModule object that runs \p module module.
163 * \throws std::bad_alloc if out of memory.
165 static std::unique_ptr
<ICommandLineModule
>
166 createModule(const char *name
, const char *description
,
167 ICommandLineOptionsModulePointer module
);
169 * Implements a main() method that runs a single module.
171 * \param argc \c argc passed to main().
172 * \param argv \c argv passed to main().
173 * \param[in] name Name for the module.
174 * \param[in] description Short description for the module.
175 * \param[in] factory Factory that returns the module to run.
177 * This method allows for uniform behavior for binaries that only
178 * contain a single module without duplicating any of the
179 * implementation from CommandLineModuleManager (startup headers,
180 * common options etc.).
182 * \see runCommandLineModule()
185 runAsMain(int argc
, char *argv
[], const char *name
,
186 const char *description
, FactoryMethod factory
);
188 * Registers a module of a certain type to this manager.
190 * \param manager Manager to register to.
191 * \param[in] name Name for the module.
192 * \param[in] description Short description for the module.
193 * \param[in] factory Factory that returns the module to register.
194 * \throws std::bad_alloc if out of memory.
196 * This method internally creates a ICommandLineModule module
197 * with the given \p name and \p description, and adds that to
198 * \p manager. When run or asked to write the help, the module calls
199 * \p factory to get the actual module, and forwards the necessary
203 registerModuleFactory(CommandLineModuleManager
*manager
,
204 const char *name
, const char *description
,
205 FactoryMethod factory
);
207 * Registers a module to this manager.
209 * \param manager Manager to register to.
210 * \param[in] name Name for the module.
211 * \param[in] description Short description for the module.
212 * \param[in] module Module to register.
213 * \throws std::bad_alloc if out of memory.
215 * This method internally creates a ICommandLineModule module
216 * with the given \p name and \p description, and adds that to
219 * This method is mainly used by tests that need to have a reference to
220 * the ICommandLineOptionsModule instance (e.g., for mocking).
223 registerModuleDirect(CommandLineModuleManager
*manager
,
224 const char *name
, const char *description
,
225 ICommandLineOptionsModulePointer module
);
227 virtual ~ICommandLineOptionsModule();
229 //! \copydoc gmx::ICommandLineModule::init()
230 virtual void init(CommandLineModuleSettings
*settings
) = 0;
232 * Initializes command-line arguments understood by the module.
234 * \param[in,out] options Options object to add the options to.
235 * \param[in,out] settings Settings to communicate information
236 * to/from generic code running the module.
238 * When running the module, this method is called after init().
239 * When printing help, there is no call to init(), and this is the only
241 * In both cases, the implementation should add options understood by
242 * the module to \p options. Output values from options should be
243 * stored in member variables.
245 virtual void initOptions(IOptionsContainer
*options
,
246 ICommandLineOptionsModuleSettings
*settings
) = 0;
248 * Called after all option values have been set.
250 * When running the module, this method is called after all
251 * command-line arguments have been parsed.
253 virtual void optionsFinished() = 0;
258 * \throws unspecified May throw exceptions to indicate errors.
259 * \returns Exit code for the program.
260 * \retval 0 on successful termination.
262 * This method is called after optionsFinished() when running the
263 * module, and should do all the processing for the module.
265 virtual int run() = 0;