2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2017 by the GROMACS development team.
5 * Copyright (c) 2018,2019,2020,2021, 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 * Declares gmx::FileNameOption and gmx::FileNameOptionInfo.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_options
44 #ifndef GMX_OPTIONS_FILENAMEOPTION_H
45 #define GMX_OPTIONS_FILENAMEOPTION_H
50 #include "gromacs/options/abstractoption.h"
51 #include "gromacs/options/optionfiletype.h"
58 class FileNameOptionInfo;
59 class FileNameOptionManager;
60 class FileNameOptionStorage;
63 * Specifies an option that provides file names.
65 * Public methods in this class do not throw.
68 * \ingroup module_options
70 class FileNameOption : public OptionTemplate<std::string, FileNameOption>
73 //! OptionInfo subclass corresponding to this option type.
74 typedef FileNameOptionInfo InfoType;
76 //! Initializes an option with the given name.
77 explicit FileNameOption(const char* name) :
79 optionType_(OptionFileType::Count),
81 defaultBasename_(nullptr),
83 bLegacyOptionalBehavior_(false),
92 * Sets the type of the file this option accepts.
94 * Either this attribute or legacyType() must be provided.
96 MyClass& filetype(OptionFileType type)
102 * Sets the type of the file from an enum in filetypes.h.
104 * New code should prefer filetype(), extending the enumeration if
107 MyClass& legacyType(int type)
113 * Changes the behavior of optional options to match old t_filenm.
115 * If this is not set, optional options return an empty string if not
116 * set. If this is set, a non-empty value is always returned.
117 * In the latter case, whether the option is set only affects the
118 * return value of OptionInfo::isSet() and Options::isSet().
120 MyClass& legacyOptionalBehavior()
122 bLegacyOptionalBehavior_ = true;
125 //! Tells that the file provided by this option is used for input only.
132 //! Tells that the file provided by this option is used for output only.
133 MyClass& outputFile()
140 * Tells that the file provided by this option is used for input and
143 MyClass& inputOutputFile()
145 bRead_ = bWrite_ = true;
149 * Sets the read/write usage for this file from boolean flags.
151 MyClass& readWriteFlags(bool bRead, bool bWrite)
158 * Tells that the file will be looked up in library directories in
159 * addition to working directory.
162 * Currently, this flag only affects the help output. Callers must
163 * take care themselves to actually search the file in the library
164 * directories. It would be nicer to do this searching within the
165 * file name option implementation.
167 MyClass& libraryFile(bool bLibrary = true)
169 bLibrary_ = bLibrary;
173 * Tells that missing file names explicitly provided by the user are
174 * valid for this input option.
176 * If this method is not called, an error will be raised if the user
177 * explicitly provides a file name that does not name an existing file,
178 * or if the default value does not resolve to a valid file name for a
179 * required option that the user has not set.
181 * This method only has effect with input files, and only if a
182 * FileNameOptionManager is being used.
184 MyClass& allowMissing(bool bAllow = true)
186 bAllowMissing_ = bAllow;
190 * Sets a default basename for the file option.
192 * Use this method instead of defaultValue() or defaultValueIfSet() to
193 * set a default value for a file name option. No extension needs to
194 * be provided; it is automatically added based on filetype() or
196 * The behavior is also adjusted based on required(): if the option is
197 * required, the value given to defaultBasename() is treated as for
198 * both defaultValue() and defaultValueIfSet(), otherwise it is treated
199 * as for defaultValueIfSet().
201 * For input files that accept multiple extensions, the extension is
202 * completed to the default extension on creation of the option or at
203 * time of parsing an option without a value.
205 * If FileNameOptionManager is used, the extension may change during
206 * Options::finish(), as this is the time when the default names are
207 * checked against the file system to provide an extension that matches
208 * an existing file if that is possible.
210 * If FileNameOptionManager is used, and
211 * FileNameOptionManager::addDefaultFileNameOption() is used, and the
212 * user provides a global default file name using that option, then the
213 * global default takes precedence over defaultBasename().
215 MyClass& defaultBasename(const char* basename)
217 defaultBasename_ = basename;
221 * Sets a default type/extension for the file option.
223 * For options that accept multiple types of files (e.g.,
224 * eftTrajectory), this method sets the default extension used
225 * for completing defaultBasename(), as well as the default extension
226 * used by FileNameOptionManager to complete various file names.
228 * The value should be one of the enumerated `ef*` values from
229 * filetypes.h, and be a valid type for the type specified with
232 MyClass& defaultType(int filetype)
234 defaultType_ = filetype;
239 // Use defaultBasename() instead.
240 using MyBase::defaultValue;
241 using MyBase::defaultValueIfSet;
243 //! Creates a FileNameOptionStorage object.
244 AbstractOptionStorage* createStorage(const OptionManagerContainer& managers) const override;
246 OptionFileType optionType_;
248 const char* defaultBasename_;
250 bool bLegacyOptionalBehavior_;
257 * Needed to initialize FileNameOptionStorage from this class without
258 * otherwise unnecessary accessors.
260 friend class FileNameOptionStorage;
264 * Wrapper class for accessing file name option information.
267 * \ingroup module_options
269 class FileNameOptionInfo : public OptionInfo
272 //! Shorthand for a list of extensions.
273 typedef std::vector<const char*> ExtensionList;
275 //! Creates an option info object for the given option.
276 explicit FileNameOptionInfo(FileNameOptionStorage* option);
278 //! Whether the option specifies an input file.
279 bool isInputFile() const;
280 //! Whether the option specifies an output file.
281 bool isOutputFile() const;
282 //! Whether the option specifies a file used for both input and output.
283 bool isInputOutputFile() const;
285 * Whether the option specifies a library file.
287 * \see FileNameOption::libraryFile()
289 bool isLibraryFile() const;
290 //! Whether the (input) option allows missing files to be provided.
291 bool allowMissing() const;
293 //! Whether the option specifies directories.
294 bool isDirectoryOption() const;
295 //! Whether the option specifies a generic trajectory file.
296 bool isTrajectoryOption() const;
297 //! Returns the default extension for this option.
298 const char* defaultExtension() const;
299 //! Returns the list of extensions this option accepts.
300 ExtensionList extensions() const;
301 //! Returns whether \p fileType (from filetypes.h) is accepted for this option.
302 bool isValidType(int fileType) const;
303 //! Returns the list of file types this option accepts.
304 ArrayRef<const int> fileTypes() const;
307 const FileNameOptionStorage& option() const;