2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015, 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"
46 #include <boost/scoped_ptr.hpp>
48 #include "gromacs/commandline/cmdlinehelpwriter.h"
49 #include "gromacs/commandline/cmdlinemodulemanager.h"
50 #include "gromacs/commandline/cmdlineparser.h"
51 #include "gromacs/options/filenameoptionmanager.h"
52 #include "gromacs/options/options.h"
53 #include "gromacs/utility/arrayref.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/stringutil.h"
63 /********************************************************************
64 * CommandLineOptionsModuleSettings
67 class CommandLineOptionsModuleSettings : public ICommandLineOptionsModuleSettings
70 const std::string &helpText() const { return helpText_; }
72 virtual void setHelpText(const ConstArrayRef<const char *> &help)
74 helpText_ = joinStrings(help, "\n");
78 std::string helpText_;
81 /********************************************************************
82 * CommandLineOptionsModule
85 class CommandLineOptionsModule : public ICommandLineModule
88 //! Shorthand for the factory function pointer type.
89 typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod;
91 CommandLineOptionsModule(const char *name, const char *description,
92 FactoryMethod factory)
93 : name_(name), description_(description), factory_(factory)
96 CommandLineOptionsModule(const char *name, const char *description,
97 ICommandLineOptionsModule *module)
98 : name_(name), description_(description), factory_(NULL),
102 virtual const char *name() const { return name_; }
103 virtual const char *shortDescription() const { return description_; }
105 virtual void init(CommandLineModuleSettings *settings);
106 virtual int run(int argc, char *argv[]);
107 virtual void writeHelp(const CommandLineHelpContext &context) const;
110 void parseOptions(int argc, char *argv[]);
113 const char *description_;
114 FactoryMethod factory_;
115 boost::scoped_ptr<ICommandLineOptionsModule> module_;
118 void CommandLineOptionsModule::init(CommandLineModuleSettings *settings)
122 GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
123 module_.reset(factory_());
125 module_->init(settings);
128 int CommandLineOptionsModule::run(int argc, char *argv[])
130 GMX_RELEASE_ASSERT(module_, "init() has not been called");
131 parseOptions(argc, argv);
132 return module_->run();
135 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext &context) const
137 boost::scoped_ptr<ICommandLineOptionsModule> moduleGuard;
138 ICommandLineOptionsModule *module = module_.get();
141 GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
142 moduleGuard.reset(factory_());
143 module = moduleGuard.get();
145 Options options(name(), shortDescription());
146 CommandLineOptionsModuleSettings settings;
147 module->initOptions(&options, &settings);
148 CommandLineHelpWriter(options)
149 .setHelpText(settings.helpText())
153 void CommandLineOptionsModule::parseOptions(int argc, char *argv[])
155 FileNameOptionManager fileoptManager;
156 Options options(name_, description_);
158 options.addManager(&fileoptManager);
160 CommandLineOptionsModuleSettings settings;
161 module_->initOptions(&options, &settings);
163 CommandLineParser parser(&options);
164 parser.parse(&argc, argv);
167 module_->optionsFinished();
172 /********************************************************************
173 * ICommandLineOptionsModuleSettings
176 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings()
180 /********************************************************************
181 * ICommandLineOptionsModule
184 ICommandLineOptionsModule::~ICommandLineOptionsModule()
190 ICommandLineOptionsModule::createModule(
191 const char *name, const char *description, FactoryMethod factory)
193 return new CommandLineOptionsModule(name, description, factory);
197 int ICommandLineOptionsModule::runAsMain(
198 int argc, char *argv[], const char *name, const char *description,
199 FactoryMethod factory)
201 CommandLineOptionsModule module(name, description, factory);
202 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
206 void ICommandLineOptionsModule::registerModule(
207 CommandLineModuleManager *manager, const char *name,
208 const char *description, FactoryMethod factory)
210 CommandLineModulePointer module(createModule(name, description, factory));
211 manager->addModule(move(module));
215 void ICommandLineOptionsModule::registerModule(
216 CommandLineModuleManager *manager, const char *name,
217 const char *description, ICommandLineOptionsModule *module)
219 CommandLineModulePointer wrapperModule(
220 new CommandLineOptionsModule(name, description, module));
221 manager->addModule(move(wrapperModule));