Split lines with many copyright years
[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, 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),
115         description_(description),
116         factory_(std::move(factory))
117     {
118     }
119     CommandLineOptionsModule(const char* name, const char* description, ICommandLineOptionsModulePointer module) :
120         name_(name),
121         description_(description),
122         module_(std::move(module))
123     {
124     }
125     const char* name() const override { return name_; }
126     const char* shortDescription() const override { return description_; }
127
128     void init(CommandLineModuleSettings* settings) override;
129     int  run(int argc, char* argv[]) override;
130     void writeHelp(const CommandLineHelpContext& context) const override;
131
132 private:
133     void parseOptions(int argc, char* argv[]);
134
135     const char*                      name_;
136     const char*                      description_;
137     FactoryMethod                    factory_;
138     ICommandLineOptionsModulePointer module_;
139 };
140
141 void CommandLineOptionsModule::init(CommandLineModuleSettings* settings)
142 {
143     if (!module_)
144     {
145         GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
146         module_ = factory_();
147     }
148     module_->init(settings);
149 }
150
151 int CommandLineOptionsModule::run(int argc, char* argv[])
152 {
153     GMX_RELEASE_ASSERT(module_, "init() has not been called");
154     parseOptions(argc, argv);
155     return module_->run();
156 }
157
158 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext& context) const
159 {
160     ICommandLineOptionsModulePointer moduleGuard;
161     ICommandLineOptionsModule*       module = module_.get();
162     if (!module)
163     {
164         GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
165         moduleGuard = factory_();
166         module      = moduleGuard.get();
167     }
168     Options                          options;
169     OptionsBehaviorCollection        behaviors(&options);
170     CommandLineOptionsModuleSettings settings(&behaviors);
171     module->initOptions(&options, &settings);
172     CommandLineHelpWriter(options)
173             .setHelpText(settings.helpText())
174             .setKnownIssues(settings.bugText())
175             .writeHelp(context);
176 }
177
178 void CommandLineOptionsModule::parseOptions(int argc, char* argv[])
179 {
180     FileNameOptionManager fileoptManager;
181     Options               options;
182
183     options.addManager(&fileoptManager);
184
185     OptionsBehaviorCollection        behaviors(&options);
186     CommandLineOptionsModuleSettings settings(&behaviors);
187     module_->initOptions(&options, &settings);
188     {
189         CommandLineParser parser(&options);
190         parser.parse(&argc, argv);
191         behaviors.optionsFinishing();
192         options.finish();
193     }
194     module_->optionsFinished();
195     behaviors.optionsFinished();
196 }
197
198 } // namespace
199
200 /********************************************************************
201  * ICommandLineOptionsModuleSettings
202  */
203
204 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings() {}
205
206 /********************************************************************
207  * ICommandLineOptionsModule
208  */
209
210 ICommandLineOptionsModule::~ICommandLineOptionsModule() {}
211
212 // static
213 std::unique_ptr<ICommandLineModule> ICommandLineOptionsModule::createModule(const char* name,
214                                                                             const char* description,
215                                                                             ICommandLineOptionsModulePointer module)
216 {
217     return std::unique_ptr<ICommandLineModule>(
218             new CommandLineOptionsModule(name, description, std::move(module)));
219 }
220
221 // static
222 int ICommandLineOptionsModule::runAsMain(int           argc,
223                                          char*         argv[],
224                                          const char*   name,
225                                          const char*   description,
226                                          FactoryMethod factory)
227 {
228     CommandLineOptionsModule module(name, description, std::move(factory));
229     return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
230 }
231
232 // static
233 void ICommandLineOptionsModule::registerModuleFactory(CommandLineModuleManager* manager,
234                                                       const char*               name,
235                                                       const char*               description,
236                                                       FactoryMethod             factory)
237 {
238     CommandLineModulePointer module(new CommandLineOptionsModule(name, description, std::move(factory)));
239     manager->addModule(std::move(module));
240 }
241
242 // static
243 void ICommandLineOptionsModule::registerModuleDirect(CommandLineModuleManager*        manager,
244                                                      const char*                      name,
245                                                      const char*                      description,
246                                                      ICommandLineOptionsModulePointer module)
247 {
248     CommandLineModulePointer wrapperModule(createModule(name, description, std::move(module)));
249     manager->addModule(std::move(wrapperModule));
250 }
251
252 } // namespace gmx