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.
37 * Implements supporting routines for gmx::ICommandLineOptionsModule.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
44 #include "cmdlineoptionsmodule.h"
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"
66 /********************************************************************
67 * CommandLineOptionsModuleSettings
70 class CommandLineOptionsModuleSettings : public ICommandLineOptionsModuleSettings
73 explicit CommandLineOptionsModuleSettings(OptionsBehaviorCollection* behaviors) :
74 behaviors_(*behaviors)
78 const std::string& helpText() const { return helpText_; }
80 ArrayRef<const std::string> bugText() const { return bugText_; }
82 void setHelpText(const ArrayRef<const char* const>& help) override
84 helpText_ = joinStrings(help, "\n");
87 void setBugText(const ArrayRef<const char* const>& bug) override
89 bugText_ = std::vector<std::string>(bug.begin(), bug.end());
91 void addOptionsBehavior(const OptionsBehaviorPointer& behavior) override
93 behaviors_.addBehavior(behavior);
97 std::string helpText_;
98 std::vector<std::string> bugText_;
99 OptionsBehaviorCollection& behaviors_;
102 /********************************************************************
103 * CommandLineOptionsModule
106 class CommandLineOptionsModule : public ICommandLineModule
109 //! Shorthand for the factory function pointer type.
110 typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod;
112 CommandLineOptionsModule(const char* name, const char* description, FactoryMethod factory) :
114 description_(description),
115 factory_(std::move(factory))
118 CommandLineOptionsModule(const char* name, const char* description, ICommandLineOptionsModulePointer module) :
120 description_(description),
121 module_(std::move(module))
124 const char* name() const override { return name_; }
125 const char* shortDescription() const override { return description_; }
127 void init(CommandLineModuleSettings* settings) override;
128 int run(int argc, char* argv[]) override;
129 void writeHelp(const CommandLineHelpContext& context) const override;
132 void parseOptions(int argc, char* argv[]);
135 const char* description_;
136 FactoryMethod factory_;
137 ICommandLineOptionsModulePointer module_;
140 void CommandLineOptionsModule::init(CommandLineModuleSettings* settings)
144 GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
145 module_ = factory_();
147 module_->init(settings);
150 int CommandLineOptionsModule::run(int argc, char* argv[])
152 GMX_RELEASE_ASSERT(module_, "init() has not been called");
153 parseOptions(argc, argv);
154 return module_->run();
157 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext& context) const
159 ICommandLineOptionsModulePointer moduleGuard;
160 ICommandLineOptionsModule* module = module_.get();
163 GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
164 moduleGuard = factory_();
165 module = moduleGuard.get();
168 OptionsBehaviorCollection behaviors(&options);
169 CommandLineOptionsModuleSettings settings(&behaviors);
170 module->initOptions(&options, &settings);
171 CommandLineHelpWriter(options)
172 .setHelpText(settings.helpText())
173 .setKnownIssues(settings.bugText())
177 void CommandLineOptionsModule::parseOptions(int argc, char* argv[])
179 FileNameOptionManager fileoptManager;
182 options.addManager(&fileoptManager);
184 OptionsBehaviorCollection behaviors(&options);
185 CommandLineOptionsModuleSettings settings(&behaviors);
186 module_->initOptions(&options, &settings);
188 CommandLineParser parser(&options);
189 parser.parse(&argc, argv);
190 behaviors.optionsFinishing();
193 module_->optionsFinished();
194 behaviors.optionsFinished();
199 /********************************************************************
200 * ICommandLineOptionsModuleSettings
203 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings() {}
205 /********************************************************************
206 * ICommandLineOptionsModule
209 ICommandLineOptionsModule::~ICommandLineOptionsModule() {}
212 std::unique_ptr<ICommandLineModule> ICommandLineOptionsModule::createModule(const char* name,
213 const char* description,
214 ICommandLineOptionsModulePointer module)
216 return std::unique_ptr<ICommandLineModule>(
217 new CommandLineOptionsModule(name, description, std::move(module)));
221 int ICommandLineOptionsModule::runAsMain(int argc,
224 const char* description,
225 FactoryMethod factory)
227 CommandLineOptionsModule module(name, description, std::move(factory));
228 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
232 void ICommandLineOptionsModule::registerModuleFactory(CommandLineModuleManager* manager,
234 const char* description,
235 FactoryMethod factory)
237 CommandLineModulePointer module(new CommandLineOptionsModule(name, description, std::move(factory)));
238 manager->addModule(std::move(module));
242 void ICommandLineOptionsModule::registerModuleDirect(CommandLineModuleManager* manager,
244 const char* description,
245 ICommandLineOptionsModulePointer module)
247 CommandLineModulePointer wrapperModule(createModule(name, description, std::move(module)));
248 manager->addModule(std::move(wrapperModule));