581174ffba00eb5a11f7ab4de73bbe094be03eb9
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlineoptionsmodule.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Implements supporting routines for gmx::ICommandLineOptionsModule.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_commandline
41  */
42 #include "gmxpre.h"
43
44 #include "cmdlineoptionsmodule.h"
45
46 #include <boost/scoped_ptr.hpp>
47
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"
56
57 namespace gmx
58 {
59
60 namespace
61 {
62
63 /********************************************************************
64  * CommandLineOptionsModuleSettings
65  */
66
67 class CommandLineOptionsModuleSettings : public ICommandLineOptionsModuleSettings
68 {
69     public:
70         const std::string &helpText() const { return helpText_; }
71
72         virtual void setHelpText(const ConstArrayRef<const char *> &help)
73         {
74             helpText_ = joinStrings(help, "\n");
75         }
76
77     private:
78         std::string helpText_;
79 };
80
81 /********************************************************************
82  * CommandLineOptionsModule
83  */
84
85 class CommandLineOptionsModule : public ICommandLineModule
86 {
87     public:
88         //! Shorthand for the factory function pointer type.
89         typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod;
90
91         CommandLineOptionsModule(const char *name, const char *description,
92                                  FactoryMethod factory)
93             : name_(name), description_(description), factory_(factory)
94         {
95         }
96         CommandLineOptionsModule(const char *name, const char *description,
97                                  ICommandLineOptionsModule *module)
98             : name_(name), description_(description), factory_(NULL),
99               module_(module)
100         {
101         }
102         virtual const char *name() const { return name_; }
103         virtual const char *shortDescription() const { return description_; }
104
105         virtual void init(CommandLineModuleSettings *settings);
106         virtual int run(int argc, char *argv[]);
107         virtual void writeHelp(const CommandLineHelpContext &context) const;
108
109     private:
110         void parseOptions(int argc, char *argv[]);
111
112         const char    *name_;
113         const char    *description_;
114         FactoryMethod  factory_;
115         boost::scoped_ptr<ICommandLineOptionsModule> module_;
116 };
117
118 void CommandLineOptionsModule::init(CommandLineModuleSettings *settings)
119 {
120     if (!module_)
121     {
122         GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
123         module_.reset(factory_());
124     }
125     module_->init(settings);
126 }
127
128 int CommandLineOptionsModule::run(int argc, char *argv[])
129 {
130     GMX_RELEASE_ASSERT(module_, "init() has not been called");
131     parseOptions(argc, argv);
132     return module_->run();
133 }
134
135 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext &context) const
136 {
137     boost::scoped_ptr<ICommandLineOptionsModule> moduleGuard;
138     ICommandLineOptionsModule                   *module = module_.get();
139     if (!module)
140     {
141         GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
142         moduleGuard.reset(factory_());
143         module = moduleGuard.get();
144     }
145     Options                          options(name(), shortDescription());
146     CommandLineOptionsModuleSettings settings;
147     module->initOptions(&options, &settings);
148     CommandLineHelpWriter(options)
149         .setHelpText(settings.helpText())
150         .writeHelp(context);
151 }
152
153 void CommandLineOptionsModule::parseOptions(int argc, char *argv[])
154 {
155     FileNameOptionManager fileoptManager;
156     Options               options(name_, description_);
157
158     options.addManager(&fileoptManager);
159
160     CommandLineOptionsModuleSettings settings;
161     module_->initOptions(&options, &settings);
162     {
163         CommandLineParser parser(&options);
164         parser.parse(&argc, argv);
165         options.finish();
166     }
167     module_->optionsFinished();
168 }
169
170 }   // namespace
171
172 /********************************************************************
173  * ICommandLineOptionsModuleSettings
174  */
175
176 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings()
177 {
178 }
179
180 /********************************************************************
181  * ICommandLineOptionsModule
182  */
183
184 ICommandLineOptionsModule::~ICommandLineOptionsModule()
185 {
186 }
187
188 // static
189 ICommandLineModule *
190 ICommandLineOptionsModule::createModule(
191         const char *name, const char *description, FactoryMethod factory)
192 {
193     return new CommandLineOptionsModule(name, description, factory);
194 }
195
196 // static
197 int ICommandLineOptionsModule::runAsMain(
198         int argc, char *argv[], const char *name, const char *description,
199         FactoryMethod factory)
200 {
201     CommandLineOptionsModule module(name, description, factory);
202     return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
203 }
204
205 // static
206 void ICommandLineOptionsModule::registerModule(
207         CommandLineModuleManager *manager, const char *name,
208         const char *description, FactoryMethod factory)
209 {
210     CommandLineModulePointer module(createModule(name, description, factory));
211     manager->addModule(move(module));
212 }
213
214 // static
215 void ICommandLineOptionsModule::registerModule(
216         CommandLineModuleManager *manager, const char *name,
217         const char *description, ICommandLineOptionsModule *module)
218 {
219     CommandLineModulePointer wrapperModule(
220             new CommandLineOptionsModule(name, description, module));
221     manager->addModule(move(wrapperModule));
222 }
223
224 } // namespace gmx