#include <gromacs/commandline/cmdlineoptionsmodule.h>
Inherited by gmx::test::MockOptionsModule.
Module that can be run from a command line and uses gmx::Options for argument processing.
This class provides a higher-level interface on top of gmx::CommandLineModuleInterface for cases where gmx::Options will be used for declaring the command-line arguments. The module only needs to declare the options it uses, and the framework takes care of command-line parsing and help output. The module typically consists of the following parts:
- init() allows for some interaction between the module and the framework when running the module; see CommandLineModuleInterface::init(). If no such customization is necessary, an empty implementation is sufficient.
- initOptions() is called both for running the module and for printing help for the module, and it should add the options that the module understands. Values provided for the options are typically stored in member variables.
- optionsFinished() can be implemented in case additional processing is needed (e.g., checking whether an option was set by the user).
- run() is called when running the module, after command-line options have been parsed and their values stored in the corresponding member variables.
registerModule(), runAsMain(), or createModule() can be used to use modules of this type in all contexts where a gmx::CommandLineModuleInterface is expected. These methods create a gmx::CommandLineModuleInterface implementation that contains the common code needed to parse command-line options and write help, based on the information provided from the methods in this class.
|
static CommandLineModuleInterface * | createModule (const char *name, const char *description, FactoryMethod factory) |
| Creates a CommandLineModuleInterface to run the specified module. More...
|
|
static int | runAsMain (int argc, char *argv[], const char *name, const char *description, FactoryMethod factory) |
| Implements a main() method that runs a single module. More...
|
|
static void | registerModule (CommandLineModuleManager *manager, const char *name, const char *description, FactoryMethod factory) |
| Registers a module of a certain type to this manager. More...
|
|
static void | registerModule (CommandLineModuleManager *manager, const char *name, const char *description, CommandLineOptionsModuleInterface *module) |
| Registers a module to this manager. More...
|
|
Function pointer to a factory method that returns an interface of this type.
- Returns
- Module to run (should be allocated with
new
).
- Exceptions
-
std::bad_alloc | if out of memory. |
The caller takes responsibility to delete
the returned pointer.
Creates a CommandLineModuleInterface to run the specified module.
- Parameters
-
[in] | name | Name for the module. |
[in] | description | Short description for the module. |
[in] | factory | Factory that returns the module to run. |
- Returns
- CommandLineModuleInterface object that runs the module returned by
factory
. Caller must delete
the object.
- Exceptions
-
std::bad_alloc | if out of memory. |
This is mainly used by tests that want to bypass CommandLineModuleManager.
Initializes the module and provides settings for the runner.
This will be called before run(), and can be used to adjust initialization that the runner does.
This method is currently not called when writing the help.
virtual void gmx::CommandLineOptionsModuleInterface::initOptions |
( |
Options * |
options | ) |
|
|
pure virtual |
Initializes command-line arguments understood by the module.
- Parameters
-
[in,out] | options | Options object to add the options to. |
When running the module, this method is called after init(). When printing help, there is no call to init(), and this is the only method called. In both cases, the implementation should add options understood by the module to options
. Output values from options should be stored in member variables.
virtual void gmx::CommandLineOptionsModuleInterface::optionsFinished |
( |
Options * |
options | ) |
|
|
pure virtual |
Called after all option values have been set.
- Parameters
-
[in,out] | options | Options object in which options are stored. |
When running the module, this method is called after all command-line arguments have been parsed, but while the Options object still exists.
If the module needs to call, e.g., Options::isSet(), this is the place to do that.
void gmx::CommandLineOptionsModuleInterface::registerModule |
( |
CommandLineModuleManager * |
manager, |
|
|
const char * |
name, |
|
|
const char * |
description, |
|
|
FactoryMethod |
factory |
|
) |
| |
|
static |
Registers a module of a certain type to this manager.
- Parameters
-
| manager | Manager to register to. |
[in] | name | Name for the module. |
[in] | description | Short description for the module. |
[in] | factory | Factory that returns the module to register. |
- Exceptions
-
std::bad_alloc | if out of memory. |
This method internally creates a CommandLineModuleInterface module with the given name
and description
, and adds that to manager
. When run or asked to write the help, the module calls factory
to get the actual module, and forwards the necessary calls.
void gmx::CommandLineOptionsModuleInterface::registerModule |
( |
CommandLineModuleManager * |
manager, |
|
|
const char * |
name, |
|
|
const char * |
description, |
|
|
CommandLineOptionsModuleInterface * |
module |
|
) |
| |
|
static |
Registers a module to this manager.
- Parameters
-
| manager | Manager to register to. |
[in] | name | Name for the module. |
[in] | description | Short description for the module. |
[in] | module | Module to register. The method takes ownership (must have been allocated with new ). |
- Exceptions
-
std::bad_alloc | if out of memory. |
This method internally creates a CommandLineModuleInterface module with the given name
and description
, and adds that to manager
.
This method is mainly used by tests that need to have a reference to the CommandLineOptionsModuleInterface instance (e.g., for mocking).
virtual int gmx::CommandLineOptionsModuleInterface::run |
( |
| ) |
|
|
pure virtual |
Runs the module.
- Exceptions
-
unspecified | May throw exceptions to indicate errors. |
- Returns
- Exit code for the program.
- Return values
-
0 | on successful termination. |
This method is called after optionsFinished() when running the module, and should do all the processing for the module.
int gmx::CommandLineOptionsModuleInterface::runAsMain |
( |
int |
argc, |
|
|
char * |
argv[], |
|
|
const char * |
name, |
|
|
const char * |
description, |
|
|
FactoryMethod |
factory |
|
) |
| |
|
static |
Implements a main() method that runs a single module.
- Parameters
-
| argc | argc passed to main(). |
| argv | argv passed to main(). |
[in] | name | Name for the module. |
[in] | description | Short description for the module. |
[in] | factory | Factory that returns the module to run. |
This method allows for uniform behavior for binaries that only contain a single module without duplicating any of the implementation from CommandLineModuleManager (startup headers, common options etc.).
- See Also
- runCommandLineModule()
The documentation for this class was generated from the following files: