2 * This file is part of the GROMACS molecular simulation package.
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.
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(
74 OptionsBehaviorCollection *behaviors)
75 : behaviors_(*behaviors)
79 const std::string &helpText() const { return helpText_; }
81 void setHelpText(const ArrayRef<const char *const> &help) override
83 helpText_ = joinStrings(help, "\n");
85 void addOptionsBehavior(const OptionsBehaviorPointer &behavior) override
87 behaviors_.addBehavior(behavior);
91 std::string helpText_;
92 OptionsBehaviorCollection &behaviors_;
95 /********************************************************************
96 * CommandLineOptionsModule
99 class CommandLineOptionsModule : public ICommandLineModule
102 //! Shorthand for the factory function pointer type.
103 typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod;
105 CommandLineOptionsModule(const char *name, const char *description,
106 FactoryMethod factory)
107 : name_(name), description_(description), factory_(std::move(factory))
110 CommandLineOptionsModule(const char *name, const char *description,
111 ICommandLineOptionsModulePointer module)
112 : name_(name), description_(description), module_(std::move(module))
115 const char *name() const override { return name_; }
116 const char *shortDescription() const override { return description_; }
118 void init(CommandLineModuleSettings *settings) override;
119 int run(int argc, char *argv[]) override;
120 void writeHelp(const CommandLineHelpContext &context) const override;
123 void parseOptions(int argc, char *argv[]);
126 const char *description_;
127 FactoryMethod factory_;
128 ICommandLineOptionsModulePointer module_;
131 void CommandLineOptionsModule::init(CommandLineModuleSettings *settings)
135 GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
136 module_ = factory_();
138 module_->init(settings);
141 int CommandLineOptionsModule::run(int argc, char *argv[])
143 GMX_RELEASE_ASSERT(module_, "init() has not been called");
144 parseOptions(argc, argv);
145 return module_->run();
148 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext &context) const
150 ICommandLineOptionsModulePointer moduleGuard;
151 ICommandLineOptionsModule *module = module_.get();
154 GMX_RELEASE_ASSERT(factory_ != nullptr, "Neither factory nor module provided");
155 moduleGuard = factory_();
156 module = moduleGuard.get();
159 OptionsBehaviorCollection behaviors(&options);
160 CommandLineOptionsModuleSettings settings(&behaviors);
161 module->initOptions(&options, &settings);
162 CommandLineHelpWriter(options)
163 .setHelpText(settings.helpText())
167 void CommandLineOptionsModule::parseOptions(int argc, char *argv[])
169 FileNameOptionManager fileoptManager;
172 options.addManager(&fileoptManager);
174 OptionsBehaviorCollection behaviors(&options);
175 CommandLineOptionsModuleSettings settings(&behaviors);
176 module_->initOptions(&options, &settings);
178 CommandLineParser parser(&options);
179 parser.parse(&argc, argv);
180 behaviors.optionsFinishing();
183 module_->optionsFinished();
184 behaviors.optionsFinished();
189 /********************************************************************
190 * ICommandLineOptionsModuleSettings
193 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings()
197 /********************************************************************
198 * ICommandLineOptionsModule
201 ICommandLineOptionsModule::~ICommandLineOptionsModule()
206 std::unique_ptr<ICommandLineModule>
207 ICommandLineOptionsModule::createModule(
208 const char *name, const char *description,
209 ICommandLineOptionsModulePointer module)
211 return std::unique_ptr<ICommandLineModule>(
212 new CommandLineOptionsModule(name, description, std::move(module)));
216 int ICommandLineOptionsModule::runAsMain(
217 int argc, char *argv[], const char *name, const char *description,
218 FactoryMethod factory)
220 CommandLineOptionsModule module(name, description, std::move(factory));
221 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
225 void ICommandLineOptionsModule::registerModuleFactory(
226 CommandLineModuleManager *manager, const char *name,
227 const char *description, FactoryMethod factory)
229 CommandLineModulePointer module(
230 new CommandLineOptionsModule(name, description, std::move(factory)));
231 manager->addModule(std::move(module));
235 void ICommandLineOptionsModule::registerModuleDirect(
236 CommandLineModuleManager *manager, const char *name,
237 const char *description, ICommandLineOptionsModulePointer module)
239 CommandLineModulePointer wrapperModule(
240 createModule(name, description, std::move(module)));
241 manager->addModule(std::move(wrapperModule));