Introduce SimulatorBuilder
[gromacs.git] / src / gromacs / commandline / cmdlineoptionsmodule.cpp
blobdc842307e91437c0823140aff0a47a02e62517e5
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018,2019, 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 /*! \internal \file
36 * \brief
37 * Implements supporting routines for gmx::ICommandLineOptionsModule.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
42 #include "gmxpre.h"
44 #include "cmdlineoptionsmodule.h"
46 #include <memory>
47 #include <utility>
49 #include "gromacs/commandline/cmdlinehelpwriter.h"
50 #include "gromacs/commandline/cmdlinemodulemanager.h"
51 #include "gromacs/commandline/cmdlineparser.h"
52 #include "gromacs/options/behaviorcollection.h"
53 #include "gromacs/options/filenameoptionmanager.h"
54 #include "gromacs/options/ioptionsbehavior.h"
55 #include "gromacs/options/options.h"
56 #include "gromacs/utility/arrayref.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/stringutil.h"
60 namespace gmx
63 namespace
66 /********************************************************************
67 * CommandLineOptionsModuleSettings
70 class CommandLineOptionsModuleSettings : public ICommandLineOptionsModuleSettings
72 public:
73 explicit CommandLineOptionsModuleSettings(
74 OptionsBehaviorCollection *behaviors)
75 : behaviors_(*behaviors)
79 const std::string &helpText() const { return helpText_; }
81 ArrayRef<const std::string> bugText() const { return bugText_; }
83 void setHelpText(const ArrayRef<const char *const> &help) override
85 helpText_ = joinStrings(help, "\n");
88 void setBugText(const ArrayRef<const char *const> &bug) override
90 bugText_ = std::vector<std::string>(bug.begin(), bug.end());
92 void addOptionsBehavior(const OptionsBehaviorPointer &behavior) override
94 behaviors_.addBehavior(behavior);
97 private:
98 std::string helpText_;
99 std::vector<std::string> bugText_;
100 OptionsBehaviorCollection &behaviors_;
103 /********************************************************************
104 * CommandLineOptionsModule
107 class CommandLineOptionsModule : public ICommandLineModule
109 public:
110 //! Shorthand for the factory function pointer type.
111 typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod;
113 CommandLineOptionsModule(const char *name, const char *description,
114 FactoryMethod factory)
115 : name_(name), description_(description), factory_(std::move(factory))
118 CommandLineOptionsModule(const char *name, const char *description,
119 ICommandLineOptionsModulePointer module)
120 : name_(name), description_(description), module_(std::move(module))
123 const char *name() const override { return name_; }
124 const char *shortDescription() const override { return description_; }
126 void init(CommandLineModuleSettings *settings) override;
127 int run(int argc, char *argv[]) override;
128 void writeHelp(const CommandLineHelpContext &context) const override;
130 private:
131 void parseOptions(int argc, char *argv[]);
133 const char *name_;
134 const char *description_;
135 FactoryMethod factory_;
136 ICommandLineOptionsModulePointer module_;
139 void CommandLineOptionsModule::init(CommandLineModuleSettings *settings)
141 if (!module_)
143 GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
144 module_ = factory_();
146 module_->init(settings);
149 int CommandLineOptionsModule::run(int argc, char *argv[])
151 GMX_RELEASE_ASSERT(module_, "init() has not been called");
152 parseOptions(argc, argv);
153 return module_->run();
156 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext &context) const
158 ICommandLineOptionsModulePointer moduleGuard;
159 ICommandLineOptionsModule *module = module_.get();
160 if (!module)
162 GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
163 moduleGuard = factory_();
164 module = moduleGuard.get();
166 Options options;
167 OptionsBehaviorCollection behaviors(&options);
168 CommandLineOptionsModuleSettings settings(&behaviors);
169 module->initOptions(&options, &settings);
170 CommandLineHelpWriter(options)
171 .setHelpText(settings.helpText())
172 .setKnownIssues(settings.bugText())
173 .writeHelp(context);
176 void CommandLineOptionsModule::parseOptions(int argc, char *argv[])
178 FileNameOptionManager fileoptManager;
179 Options options;
181 options.addManager(&fileoptManager);
183 OptionsBehaviorCollection behaviors(&options);
184 CommandLineOptionsModuleSettings settings(&behaviors);
185 module_->initOptions(&options, &settings);
187 CommandLineParser parser(&options);
188 parser.parse(&argc, argv);
189 behaviors.optionsFinishing();
190 options.finish();
192 module_->optionsFinished();
193 behaviors.optionsFinished();
196 } // namespace
198 /********************************************************************
199 * ICommandLineOptionsModuleSettings
202 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings()
206 /********************************************************************
207 * ICommandLineOptionsModule
210 ICommandLineOptionsModule::~ICommandLineOptionsModule()
214 // static
215 std::unique_ptr<ICommandLineModule>
216 ICommandLineOptionsModule::createModule(
217 const char *name, const char *description,
218 ICommandLineOptionsModulePointer module)
220 return std::unique_ptr<ICommandLineModule>(
221 new CommandLineOptionsModule(name, description, std::move(module)));
224 // static
225 int ICommandLineOptionsModule::runAsMain(
226 int argc, char *argv[], const char *name, const char *description,
227 FactoryMethod factory)
229 CommandLineOptionsModule module(name, description, std::move(factory));
230 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
233 // static
234 void ICommandLineOptionsModule::registerModuleFactory(
235 CommandLineModuleManager *manager, const char *name,
236 const char *description, FactoryMethod factory)
238 CommandLineModulePointer module(
239 new CommandLineOptionsModule(name, description, std::move(factory)));
240 manager->addModule(std::move(module));
243 // static
244 void ICommandLineOptionsModule::registerModuleDirect(
245 CommandLineModuleManager *manager, const char *name,
246 const char *description, ICommandLineOptionsModulePointer module)
248 CommandLineModulePointer wrapperModule(
249 createModule(name, description, std::move(module)));
250 manager->addModule(std::move(wrapperModule));
253 } // namespace gmx