2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
38 * Implements supporting routines for gmx::ICommandLineOptionsModule.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_commandline
45 #include "cmdlineoptionsmodule.h"
50 #include "gromacs/commandline/cmdlinehelpwriter.h"
51 #include "gromacs/commandline/cmdlinemodulemanager.h"
52 #include "gromacs/commandline/cmdlineparser.h"
53 #include "gromacs/options/behaviorcollection.h"
54 #include "gromacs/options/filenameoptionmanager.h"
55 #include "gromacs/options/ioptionsbehavior.h"
56 #include "gromacs/options/options.h"
57 #include "gromacs/utility/arrayref.h"
58 #include "gromacs/utility/gmxassert.h"
59 #include "gromacs/utility/stringutil.h"
67 /********************************************************************
68 * CommandLineOptionsModuleSettings
71 class CommandLineOptionsModuleSettings
: public ICommandLineOptionsModuleSettings
74 explicit CommandLineOptionsModuleSettings(OptionsBehaviorCollection
* behaviors
) :
75 behaviors_(*behaviors
)
79 const std::string
& helpText() const { return helpText_
; }
81 ArrayRef
<const std::string
> bugText() const { return bugText_
; }
83 void setHelpText(const ArrayRef
<const char* const>& help
) override
85 helpText_
= joinStrings(help
, "\n");
88 void setBugText(const ArrayRef
<const char* const>& bug
) override
90 bugText_
= std::vector
<std::string
>(bug
.begin(), bug
.end());
92 void addOptionsBehavior(const OptionsBehaviorPointer
& behavior
) override
94 behaviors_
.addBehavior(behavior
);
98 std::string helpText_
;
99 std::vector
<std::string
> bugText_
;
100 OptionsBehaviorCollection
& behaviors_
;
103 /********************************************************************
104 * CommandLineOptionsModule
107 class CommandLineOptionsModule
: public ICommandLineModule
110 //! Shorthand for the factory function pointer type.
111 typedef ICommandLineOptionsModule::FactoryMethod FactoryMethod
;
113 CommandLineOptionsModule(const char* name
, const char* description
, FactoryMethod factory
) :
115 description_(description
),
116 factory_(std::move(factory
))
119 CommandLineOptionsModule(const char* name
, const char* description
, ICommandLineOptionsModulePointer module
) :
121 description_(description
),
122 module_(std::move(module
))
125 const char* name() const override
{ return name_
; }
126 const char* shortDescription() const override
{ return description_
; }
128 void init(CommandLineModuleSettings
* settings
) override
;
129 int run(int argc
, char* argv
[]) override
;
130 void writeHelp(const CommandLineHelpContext
& context
) const override
;
133 void parseOptions(int argc
, char* argv
[]);
136 const char* description_
;
137 FactoryMethod factory_
;
138 ICommandLineOptionsModulePointer module_
;
141 void CommandLineOptionsModule::init(CommandLineModuleSettings
* settings
)
145 GMX_RELEASE_ASSERT(factory_
!= nullptr, "Neither factory nor module provided");
146 module_
= factory_();
148 module_
->init(settings
);
151 int CommandLineOptionsModule::run(int argc
, char* argv
[])
153 GMX_RELEASE_ASSERT(module_
, "init() has not been called");
154 parseOptions(argc
, argv
);
155 return module_
->run();
158 void CommandLineOptionsModule::writeHelp(const CommandLineHelpContext
& context
) const
160 ICommandLineOptionsModulePointer moduleGuard
;
161 ICommandLineOptionsModule
* module
= module_
.get();
164 GMX_RELEASE_ASSERT(factory_
!= nullptr, "Neither factory nor module provided");
165 moduleGuard
= factory_();
166 module
= moduleGuard
.get();
169 OptionsBehaviorCollection
behaviors(&options
);
170 CommandLineOptionsModuleSettings
settings(&behaviors
);
171 module
->initOptions(&options
, &settings
);
172 CommandLineHelpWriter(options
)
173 .setHelpText(settings
.helpText())
174 .setKnownIssues(settings
.bugText())
178 void CommandLineOptionsModule::parseOptions(int argc
, char* argv
[])
180 FileNameOptionManager fileoptManager
;
183 options
.addManager(&fileoptManager
);
185 OptionsBehaviorCollection
behaviors(&options
);
186 CommandLineOptionsModuleSettings
settings(&behaviors
);
187 module_
->initOptions(&options
, &settings
);
189 CommandLineParser
parser(&options
);
190 parser
.parse(&argc
, argv
);
191 behaviors
.optionsFinishing();
194 module_
->optionsFinished();
195 behaviors
.optionsFinished();
200 /********************************************************************
201 * ICommandLineOptionsModuleSettings
204 ICommandLineOptionsModuleSettings::~ICommandLineOptionsModuleSettings() {}
206 /********************************************************************
207 * ICommandLineOptionsModule
210 ICommandLineOptionsModule::~ICommandLineOptionsModule() {}
213 std::unique_ptr
<ICommandLineModule
> ICommandLineOptionsModule::createModule(const char* name
,
214 const char* description
,
215 ICommandLineOptionsModulePointer module
)
217 return std::unique_ptr
<ICommandLineModule
>(
218 new CommandLineOptionsModule(name
, description
, std::move(module
)));
222 int ICommandLineOptionsModule::runAsMain(int argc
,
225 const char* description
,
226 FactoryMethod factory
)
228 CommandLineOptionsModule
module(name
, description
, std::move(factory
));
229 return CommandLineModuleManager::runAsMainSingleModule(argc
, argv
, &module
);
233 void ICommandLineOptionsModule::registerModuleFactory(CommandLineModuleManager
* manager
,
235 const char* description
,
236 FactoryMethod factory
)
238 CommandLineModulePointer
module(new CommandLineOptionsModule(name
, description
, std::move(factory
)));
239 manager
->addModule(std::move(module
));
243 void ICommandLineOptionsModule::registerModuleDirect(CommandLineModuleManager
* manager
,
245 const char* description
,
246 ICommandLineOptionsModulePointer module
)
248 CommandLineModulePointer
wrapperModule(createModule(name
, description
, std::move(module
)));
249 manager
->addModule(std::move(wrapperModule
));