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::CommandLineOptionsModuleInterface.
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/gmxassert.h"
61 /********************************************************************
62 * CommandLineOptionsModule
65 class CommandLineOptionsModule : public CommandLineModuleInterface
68 //! Shorthand for the factory function pointer type.
69 typedef CommandLineOptionsModuleInterface::FactoryMethod FactoryMethod;
71 CommandLineOptionsModule(const char *name, const char *description,
72 FactoryMethod factory)
73 : name_(name), description_(description), factory_(factory)
76 CommandLineOptionsModule(const char *name, const char *description,
77 CommandLineOptionsModuleInterface *module)
78 : name_(name), description_(description), factory_(NULL),
82 virtual const char *name() const { return name_; }
83 virtual const char *shortDescription() const { return description_; }
85 virtual void init(CommandLineModuleSettings *settings);
86 virtual int run(int argc, char *argv[]);
87 virtual void writeHelp(const CommandLineHelpContext &context) const;
90 void parseOptions(int argc, char *argv[]);
93 const char *description_;
94 FactoryMethod factory_;
95 boost::scoped_ptr<CommandLineOptionsModuleInterface> module_;
98 void CommandLineOptionsModule::init(CommandLineModuleSettings *settings)
102 GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
103 module_.reset(factory_());
105 module_->init(settings);
108 int CommandLineOptionsModule::run(int argc, char *argv[])
110 GMX_RELEASE_ASSERT(module_, "init() has not been called");
111 parseOptions(argc, argv);
112 return module_->run();
115 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext &context) const
117 boost::scoped_ptr<CommandLineOptionsModuleInterface> moduleGuard;
118 CommandLineOptionsModuleInterface *module = module_.get();
121 GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
122 moduleGuard.reset(factory_());
123 module = moduleGuard.get();
125 Options options(name(), shortDescription());
126 module->initOptions(&options);
127 CommandLineHelpWriter(options)
128 .setShowDescriptions(true)
132 void CommandLineOptionsModule::parseOptions(int argc, char *argv[])
134 FileNameOptionManager fileoptManager;
135 Options options(name_, description_);
137 options.addManager(&fileoptManager);
139 module_->initOptions(&options);
141 CommandLineParser parser(&options);
142 parser.parse(&argc, argv);
145 module_->optionsFinished(&options);
150 /********************************************************************
151 * CommandLineOptionsModuleInterface
154 CommandLineOptionsModuleInterface::~CommandLineOptionsModuleInterface()
159 CommandLineModuleInterface *
160 CommandLineOptionsModuleInterface::createModule(
161 const char *name, const char *description, FactoryMethod factory)
163 return new CommandLineOptionsModule(name, description, factory);
167 int CommandLineOptionsModuleInterface::runAsMain(
168 int argc, char *argv[], const char *name, const char *description,
169 FactoryMethod factory)
171 CommandLineOptionsModule module(name, description, factory);
172 return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
176 void CommandLineOptionsModuleInterface::registerModule(
177 CommandLineModuleManager *manager, const char *name,
178 const char *description, FactoryMethod factory)
180 CommandLineModulePointer module(createModule(name, description, factory));
181 manager->addModule(move(module));
185 void CommandLineOptionsModuleInterface::registerModule(
186 CommandLineModuleManager *manager, const char *name,
187 const char *description, CommandLineOptionsModuleInterface *module)
189 CommandLineModulePointer wrapperModule(
190 new CommandLineOptionsModule(name, description, module));
191 manager->addModule(move(wrapperModule));