Apply clang-format-11
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlineoptionsmodule.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,2017,2018 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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements supporting routines for gmx::ICommandLineOptionsModule.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_commandline
42  */
43 #include "gmxpre.h"
44
45 #include "cmdlineoptionsmodule.h"
46
47 #include <memory>
48 #include <utility>
49
50 #include "gromacs/commandline/cmdlinehelpwriter.h"
51 #include "gromacs/commandline/cmdlinemodulemanager.h"
52 #include "gromacs/commandline/cmdlineparser.h"
53 #include "gromacs/options/behaviorcollection.h"
54 #include "gromacs/options/filenameoptionmanager.h"
55 #include "gromacs/options/ioptionsbehavior.h"
56 #include "gromacs/options/options.h"
57 #include "gromacs/utility/arrayref.h"
58 #include "gromacs/utility/gmxassert.h"
59 #include "gromacs/utility/stringutil.h"
60
61 namespace gmx
62 {
63
64 namespace
65 {
66
67 /********************************************************************
68  * CommandLineOptionsModuleSettings
69  */
70
71 class CommandLineOptionsModuleSettings : public ICommandLineOptionsModuleSettings
72 {
73 public:
74     explicit CommandLineOptionsModuleSettings(OptionsBehaviorCollection* behaviors) :
75         behaviors_(*behaviors)
76     {
77     }
78
79     const std::string& helpText() const { return helpText_; }
80
81     ArrayRef<const std::string> bugText() const { return bugText_; }
82
83     void setHelpText(const ArrayRef<const char* const>& help) override
84     {
85         helpText_ = joinStrings(help, "\n");
86     }
87
88     void setBugText(const ArrayRef<const char* const>& bug) override
89     {
90         bugText_ = std::vector<std::string>(bug.begin(), bug.end());
91     }
92     void addOptionsBehavior(const OptionsBehaviorPointer& behavior) override
93     {
94         behaviors_.addBehavior(behavior);
95     }
96
97 private:
98     std::string                helpText_;
99     std::vector<std::string>   bugText_;
100     OptionsBehaviorCollection& behaviors_;
101 };
102
103 /********************************************************************
104  * CommandLineOptionsModule
105  */
106
107 class CommandLineOptionsModule : public ICommandLineModule
108 {
109 public:
110     //! Shorthand for the factory function pointer type.
111     typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod;
112
113     CommandLineOptionsModule(const char* name, const char* description, FactoryMethod factory) :
114         name_(name), description_(description), factory_(std::move(factory))
115     {
116     }
117     CommandLineOptionsModule(const char* name, const char* description, ICommandLineOptionsModulePointer module) :
118         name_(name), description_(description), module_(std::move(module))
119     {
120     }
121     const char* name() const override { return name_; }
122     const char* shortDescription() const override { return description_; }
123
124     void init(CommandLineModuleSettings* settings) override;
125     int  run(int argc, char* argv[]) override;
126     void writeHelp(const CommandLineHelpContext& context) const override;
127
128 private:
129     void parseOptions(int argc, char* argv[]);
130
131     const char*                      name_;
132     const char*                      description_;
133     FactoryMethod                    factory_;
134     ICommandLineOptionsModulePointer module_;
135 };
136
137 void CommandLineOptionsModule::init(CommandLineModuleSettings* settings)
138 {
139     if (!module_)
140     {
141         GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
142         module_ = factory_();
143     }
144     module_->init(settings);
145 }
146
147 int CommandLineOptionsModule::run(int argc, char* argv[])
148 {
149     GMX_RELEASE_ASSERT(module_, "init() has not been called");
150     parseOptions(argc, argv);
151     return module_->run();
152 }
153
154 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext& context) const
155 {
156     ICommandLineOptionsModulePointer moduleGuard;
157     ICommandLineOptionsModule*       module = module_.get();
158     if (!module)
159     {
160         GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
161         moduleGuard = factory_();
162         module      = moduleGuard.get();
163     }
164     Options                          options;
165     OptionsBehaviorCollection        behaviors(&options);
166     CommandLineOptionsModuleSettings settings(&behaviors);
167     module->initOptions(&options, &settings);
168     CommandLineHelpWriter(options)
169             .setHelpText(settings.helpText())
170             .setKnownIssues(settings.bugText())
171             .writeHelp(context);
172 }
173
174 void CommandLineOptionsModule::parseOptions(int argc, char* argv[])
175 {
176     FileNameOptionManager fileoptManager;
177     Options               options;
178
179     options.addManager(&fileoptManager);
180
181     OptionsBehaviorCollection        behaviors(&options);
182     CommandLineOptionsModuleSettings settings(&behaviors);
183     module_->initOptions(&options, &settings);
184     {
185         CommandLineParser parser(&options);
186         parser.parse(&argc, argv);
187         behaviors.optionsFinishing();
188         options.finish();
189     }
190     module_->optionsFinished();
191     behaviors.optionsFinished();
192 }
193
194 } // namespace
195
196 /********************************************************************
197  * ICommandLineOptionsModuleSettings
198  */
199
200 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings() {}
201
202 /********************************************************************
203  * ICommandLineOptionsModule
204  */
205
206 ICommandLineOptionsModule::~ICommandLineOptionsModule() {}
207
208 // static
209 std::unique_ptr<ICommandLineModule> ICommandLineOptionsModule::createModule(const char* name,
210                                                                             const char* description,
211                                                                             ICommandLineOptionsModulePointer module)
212 {
213     return std::unique_ptr<ICommandLineModule>(
214             new CommandLineOptionsModule(name, description, std::move(module)));
215 }
216
217 // static
218 int ICommandLineOptionsModule::runAsMain(int           argc,
219                                          char*         argv[],
220                                          const char*   name,
221                                          const char*   description,
222                                          FactoryMethod factory)
223 {
224     CommandLineOptionsModule module(name, description, std::move(factory));
225     return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
226 }
227
228 // static
229 void ICommandLineOptionsModule::registerModuleFactory(CommandLineModuleManager* manager,
230                                                       const char*               name,
231                                                       const char*               description,
232                                                       FactoryMethod             factory)
233 {
234     CommandLineModulePointer module(new CommandLineOptionsModule(name, description, std::move(factory)));
235     manager->addModule(std::move(module));
236 }
237
238 // static
239 void ICommandLineOptionsModule::registerModuleDirect(CommandLineModuleManager*        manager,
240                                                      const char*                      name,
241                                                      const char*                      description,
242                                                      ICommandLineOptionsModulePointer module)
243 {
244     CommandLineModulePointer wrapperModule(createModule(name, description, std::move(module)));
245     manager->addModule(std::move(wrapperModule));
246 }
247
248 } // namespace gmx