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