2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016, 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"
48 #include "gromacs/commandline/cmdlinehelpwriter.h"
49 #include "gromacs/commandline/cmdlinemodulemanager.h"
50 #include "gromacs/commandline/cmdlineparser.h"
51 #include "gromacs/options/behaviorcollection.h"
52 #include "gromacs/options/filenameoptionmanager.h"
53 #include "gromacs/options/ioptionsbehavior.h"
54 #include "gromacs/options/options.h"
55 #include "gromacs/utility/arrayref.h"
56 #include "gromacs/utility/gmxassert.h"
57 #include "gromacs/utility/stringutil.h"
65 /********************************************************************
66 * CommandLineOptionsModuleSettings
69 class CommandLineOptionsModuleSettings : public ICommandLineOptionsModuleSettings
72 explicit CommandLineOptionsModuleSettings(
73 OptionsBehaviorCollection *behaviors)
74 : behaviors_(*behaviors)
78 const std::string &helpText() const { return helpText_; }
80 virtual void setHelpText(const ConstArrayRef<const char *> &help)
82 helpText_ = joinStrings(help, "\n");
84 virtual void addOptionsBehavior(const OptionsBehaviorPointer &behavior)
86 behaviors_.addBehavior(behavior);
90 std::string helpText_;
91 OptionsBehaviorCollection &behaviors_;
94 /********************************************************************
95 * CommandLineOptionsModule
98 class CommandLineOptionsModule : public ICommandLineModule
101 //! Shorthand for the factory function pointer type.
102 typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod;
104 CommandLineOptionsModule(const char *name, const char *description,
105 FactoryMethod factory)
106 : name_(name), description_(description), factory_(factory)
109 CommandLineOptionsModule(const char *name, const char *description,
110 ICommandLineOptionsModulePointer module)
111 : name_(name), description_(description), module_(std::move(module))
114 virtual const char *name() const { return name_; }
115 virtual const char *shortDescription() const { return description_; }
117 virtual void init(CommandLineModuleSettings *settings);
118 virtual int run(int argc, char *argv[]);
119 virtual void writeHelp(const CommandLineHelpContext &context) const;
122 void parseOptions(int argc, char *argv[]);
125 const char *description_;
126 FactoryMethod factory_;
127 ICommandLineOptionsModulePointer module_;
130 void CommandLineOptionsModule::init(CommandLineModuleSettings *settings)
134 GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
135 module_ = factory_();
137 module_->init(settings);
140 int CommandLineOptionsModule::run(int argc, char *argv[])
142 GMX_RELEASE_ASSERT(module_, "init() has not been called");
143 parseOptions(argc, argv);
144 return module_->run();
147 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext &context) const
149 ICommandLineOptionsModulePointer moduleGuard;
150 ICommandLineOptionsModule *module = module_.get();
153 GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
154 moduleGuard = factory_();
155 module = moduleGuard.get();
158 OptionsBehaviorCollection behaviors(&options);
159 CommandLineOptionsModuleSettings settings(&behaviors);
160 module->initOptions(&options, &settings);
161 CommandLineHelpWriter(options)
162 .setHelpText(settings.helpText())
166 void CommandLineOptionsModule::parseOptions(int argc, char *argv[])
168 FileNameOptionManager fileoptManager;
171 options.addManager(&fileoptManager);
173 OptionsBehaviorCollection behaviors(&options);
174 CommandLineOptionsModuleSettings settings(&behaviors);
175 module_->initOptions(&options, &settings);
177 CommandLineParser parser(&options);
178 parser.parse(&argc, argv);
179 behaviors.optionsFinishing();
182 module_->optionsFinished();
183 behaviors.optionsFinished();
188 /********************************************************************
189 * ICommandLineOptionsModuleSettings
192 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings()
196 /********************************************************************
197 * ICommandLineOptionsModule
200 ICommandLineOptionsModule::~ICommandLineOptionsModule()
205 std::unique_ptr<ICommandLineModule>
206 ICommandLineOptionsModule::createModule(
207 const char *name, const char *description,
208 ICommandLineOptionsModulePointer module)
210 return std::unique_ptr<ICommandLineModule>(
211 new CommandLineOptionsModule(name, description, std::move(module)));
215 int ICommandLineOptionsModule::runAsMain(
216 int argc, char *argv[], const char *name, const char *description,
217 FactoryMethod factory)
219 CommandLineOptionsModule module(name, description, factory);
220 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
224 void ICommandLineOptionsModule::registerModuleFactory(
225 CommandLineModuleManager *manager, const char *name,
226 const char *description, FactoryMethod factory)
228 CommandLineModulePointer module(
229 new CommandLineOptionsModule(name, description, factory));
230 manager->addModule(std::move(module));
234 void ICommandLineOptionsModule::registerModuleDirect(
235 CommandLineModuleManager *manager, const char *name,
236 const char *description, ICommandLineOptionsModulePointer module)
238 CommandLineModulePointer wrapperModule(
239 createModule(name, description, std::move(module)));
240 manager->addModule(std::move(wrapperModule));