Some tests for 'gmx help -export rst'
[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::CommandLineOptionsModuleInterface.
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/gmxassert.h"
54
55 namespace gmx
56 {
57
58 namespace
59 {
60
61 /********************************************************************
62  * CommandLineOptionsModule
63  */
64
65 class CommandLineOptionsModule : public CommandLineModuleInterface
66 {
67     public:
68         //! Shorthand for the factory function pointer type.
69         typedef CommandLineOptionsModuleInterface::FactoryMethod FactoryMethod;
70
71         CommandLineOptionsModule(const char *name, const char *description,
72                                  FactoryMethod factory)
73             : name_(name), description_(description), factory_(factory)
74         {
75         }
76         CommandLineOptionsModule(const char *name, const char *description,
77                                  CommandLineOptionsModuleInterface *module)
78             : name_(name), description_(description), factory_(NULL),
79               module_(module)
80         {
81         }
82         virtual const char *name() const { return name_; }
83         virtual const char *shortDescription() const { return description_; }
84
85         virtual void init(CommandLineModuleSettings *settings);
86         virtual int run(int argc, char *argv[]);
87         virtual void writeHelp(const CommandLineHelpContext &context) const;
88
89     private:
90         void parseOptions(int argc, char *argv[]);
91
92         const char    *name_;
93         const char    *description_;
94         FactoryMethod  factory_;
95         boost::scoped_ptr<CommandLineOptionsModuleInterface> module_;
96 };
97
98 void CommandLineOptionsModule::init(CommandLineModuleSettings *settings)
99 {
100     if (!module_)
101     {
102         GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
103         module_.reset(factory_());
104     }
105     module_->init(settings);
106 }
107
108 int CommandLineOptionsModule::run(int argc, char *argv[])
109 {
110     GMX_RELEASE_ASSERT(module_, "init() has not been called");
111     parseOptions(argc, argv);
112     return module_->run();
113 }
114
115 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext &context) const
116 {
117     boost::scoped_ptr<CommandLineOptionsModuleInterface> moduleGuard;
118     CommandLineOptionsModuleInterface                   *module = module_.get();
119     if (!module)
120     {
121         GMX_RELEASE_ASSERT(factory_ != NULL, "Neither factory nor module provided");
122         moduleGuard.reset(factory_());
123         module = moduleGuard.get();
124     }
125     Options options(name(), shortDescription());
126     module->initOptions(&options);
127     CommandLineHelpWriter(options)
128         .setShowDescriptions(true)
129         .writeHelp(context);
130 }
131
132 void CommandLineOptionsModule::parseOptions(int argc, char *argv[])
133 {
134     FileNameOptionManager fileoptManager;
135     Options               options(name_, description_);
136
137     options.addManager(&fileoptManager);
138
139     module_->initOptions(&options);
140     {
141         CommandLineParser parser(&options);
142         parser.parse(&argc, argv);
143         options.finish();
144     }
145     module_->optionsFinished(&options);
146 }
147
148 }   // namespace
149
150 /********************************************************************
151  * CommandLineOptionsModuleInterface
152  */
153
154 CommandLineOptionsModuleInterface::~CommandLineOptionsModuleInterface()
155 {
156 }
157
158 // static
159 CommandLineModuleInterface *
160 CommandLineOptionsModuleInterface::createModule(
161         const char *name, const char *description, FactoryMethod factory)
162 {
163     return new CommandLineOptionsModule(name, description, factory);
164 }
165
166 // static
167 int CommandLineOptionsModuleInterface::runAsMain(
168         int argc, char *argv[], const char *name, const char *description,
169         FactoryMethod factory)
170 {
171     CommandLineOptionsModule module(name, description, factory);
172     return CommandLineModuleManager::runAsMainSingleModule(argc, argv, &module);
173 }
174
175 // static
176 void CommandLineOptionsModuleInterface::registerModule(
177         CommandLineModuleManager *manager, const char *name,
178         const char *description, FactoryMethod factory)
179 {
180     CommandLineModulePointer module(createModule(name, description, factory));
181     manager->addModule(move(module));
182 }
183
184 // static
185 void CommandLineOptionsModuleInterface::registerModule(
186         CommandLineModuleManager *manager, const char *name,
187         const char *description, CommandLineOptionsModuleInterface *module)
188 {
189     CommandLineModulePointer wrapperModule(
190             new CommandLineOptionsModule(name, description, module));
191     manager->addModule(move(wrapperModule));
192 }
193
194 } // namespace gmx