2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
38 * Implements supporting routines for gmx::ICommandLineOptionsModule.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_commandline
45 #include "cmdlineoptionsmodule.h"
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"
67 /********************************************************************
68 * CommandLineOptionsModuleSettings
71 class CommandLineOptionsModuleSettings : public ICommandLineOptionsModuleSettings
74 explicit CommandLineOptionsModuleSettings(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);
98 std::string helpText_;
99 std::vector<std::string> bugText_;
100 OptionsBehaviorCollection& behaviors_;
103 /********************************************************************
104 * CommandLineOptionsModule
107 class CommandLineOptionsModule : public ICommandLineModule
110 //! Shorthand for the factory function pointer type.
111 typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod;
113 CommandLineOptionsModule(const char* name, const char* description, FactoryMethod factory) :
114 name_(name), description_(description), factory_(std::move(factory))
117 CommandLineOptionsModule(const char* name, const char* description, ICommandLineOptionsModulePointer module) :
118 name_(name), description_(description), module_(std::move(module))
121 const char* name() const override { return name_; }
122 const char* shortDescription() const override { return description_; }
124 void init(CommandLineModuleSettings* settings) override;
125 int run(int argc, char* argv[]) override;
126 void writeHelp(const CommandLineHelpContext& context) const override;
129 void parseOptions(int argc, char* argv[]);
132 const char* description_;
133 FactoryMethod factory_;
134 ICommandLineOptionsModulePointer module_;
137 void CommandLineOptionsModule::init(CommandLineModuleSettings* settings)
141 GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
142 module_ = factory_();
144 module_->init(settings);
147 int CommandLineOptionsModule::run(int argc, char* argv[])
149 GMX_RELEASE_ASSERT(module_, "init() has not been called");
150 parseOptions(argc, argv);
151 return module_->run();
154 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext& context) const
156 ICommandLineOptionsModulePointer moduleGuard;
157 ICommandLineOptionsModule* module = module_.get();
160 GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
161 moduleGuard = factory_();
162 module = moduleGuard.get();
165 OptionsBehaviorCollection behaviors(&options);
166 CommandLineOptionsModuleSettings settings(&behaviors);
167 module->initOptions(&options, &settings);
168 CommandLineHelpWriter(options)
169 .setHelpText(settings.helpText())
170 .setKnownIssues(settings.bugText())
174 void CommandLineOptionsModule::parseOptions(int argc, char* argv[])
176 FileNameOptionManager fileoptManager;
179 options.addManager(&fileoptManager);
181 OptionsBehaviorCollection behaviors(&options);
182 CommandLineOptionsModuleSettings settings(&behaviors);
183 module_->initOptions(&options, &settings);
185 CommandLineParser parser(&options);
186 parser.parse(&argc, argv);
187 behaviors.optionsFinishing();
190 module_->optionsFinished();
191 behaviors.optionsFinished();
196 /********************************************************************
197 * ICommandLineOptionsModuleSettings
200 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings() {}
202 /********************************************************************
203 * ICommandLineOptionsModule
206 ICommandLineOptionsModule::~ICommandLineOptionsModule() {}
209 std::unique_ptr<ICommandLineModule> ICommandLineOptionsModule::createModule(const char* name,
210 const char* description,
211 ICommandLineOptionsModulePointer module)
213 return std::unique_ptr<ICommandLineModule>(
214 new CommandLineOptionsModule(name, description, std::move(module)));
218 int ICommandLineOptionsModule::runAsMain(int argc,
221 const char* description,
222 FactoryMethod factory)
224 CommandLineOptionsModule module(name, description, std::move(factory));
225 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
229 void ICommandLineOptionsModule::registerModuleFactory(CommandLineModuleManager* manager,
231 const char* description,
232 FactoryMethod factory)
234 CommandLineModulePointer module(new CommandLineOptionsModule(name, description, std::move(factory)));
235 manager->addModule(std::move(module));
239 void ICommandLineOptionsModule::registerModuleDirect(CommandLineModuleManager* manager,
241 const char* description,
242 ICommandLineOptionsModulePointer module)
244 CommandLineModulePointer wrapperModule(createModule(name, description, std::move(module)));
245 manager->addModule(std::move(wrapperModule));