Shell completions through Options
[alexxy/gromacs.git] / src / gromacs / options / filenameoption.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, 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 /*! \file
36  * \brief
37  * Declares gmx::FileNameOption and gmx::FileNameOptionInfo.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_options
42  */
43 #ifndef GMX_OPTIONS_FILENAMEOPTION_H
44 #define GMX_OPTIONS_FILENAMEOPTION_H
45
46 #include <string>
47
48 #include "abstractoption.h"
49 #include "optionfiletype.h"
50
51 namespace gmx
52 {
53
54 template <typename T> class ConstArrayRef;
55 class FileNameOptionInfo;
56 class FileNameOptionStorage;
57
58 /*! \brief
59  * Specifies an option that provides file names.
60  *
61  * Public methods in this class do not throw.
62  *
63  * \inpublicapi
64  * \ingroup module_options
65  */
66 class FileNameOption : public OptionTemplate<std::string, FileNameOption>
67 {
68     public:
69         //! OptionInfo subclass corresponding to this option type.
70         typedef FileNameOptionInfo InfoType;
71
72         //! Initializes an option with the given name.
73         explicit FileNameOption(const char *name)
74             : MyBase(name), filetype_(eftUnknown), legacyType_(-1),
75               defaultBasename_(NULL),
76               bRead_(false), bWrite_(false), bLibrary_(false)
77         {
78         }
79
80         /*! \brief
81          * Sets the type of the file this option accepts.
82          *
83          * Either this attribute or legacyType() must be provided.
84          */
85         MyClass &filetype(OptionFileType type)
86         { filetype_ = type; return me(); }
87         /*! \brief
88          * Sets the type of the file from an enum in filenm.h.
89          *
90          * New code should prefer filetype(), extending the enumeration if
91          * necessary.
92          */
93         MyClass &legacyType(int type)
94         { legacyType_ = type; return me(); }
95         //! Tells that the file provided by this option is used for input only.
96         MyClass &inputFile()
97         { bRead_ = true; bWrite_ = false; return me(); }
98         //! Tells that the file provided by this option is used for output only.
99         MyClass &outputFile()
100         { bRead_ = false; bWrite_ = true; return me(); }
101         /*! \brief
102          * Tells that the file provided by this option is used for input and
103          * output both.
104          */
105         MyClass &inputOutputFile()
106         { bRead_ = bWrite_ = true; return me(); }
107         /*! \brief
108          * Sets the read/write usage for this file from boolean flags.
109          */
110         MyClass &readWriteFlags(bool bRead, bool bWrite)
111         { bRead_ = bRead; bWrite_ = bWrite; return me(); }
112         /*! \brief
113          * Tells that the file will be looked up in library directories in
114          * addition to working directory.
115          *
116          * \todo
117          * Currently, this flag only affects the help output.  Callers must
118          * take care themselves to actually search the file in the library
119          * directories.  It would be nicer to do this searching within the
120          * file name option implementation.
121          */
122         MyClass &libraryFile(bool bLibrary = true)
123         { bLibrary_ = bLibrary; return me(); }
124         /*! \brief
125          * Sets a default basename for the file option.
126          *
127          * Use this method instead of defaultValue() or defaultValueIfSet() to
128          * set a default value for a file name option.  No extension needs to
129          * be provided; it is automatically added based on filetype().
130          * The behavior is also adjusted based on required(): if the option is
131          * required, the value given to defaultBasename() is treated as for
132          * both defaultValue() and defaultValueIfSet(), otherwise it is treated
133          * as for defaultValueIfSet().
134          */
135         MyClass &defaultBasename(const char *basename)
136         { defaultBasename_ = basename; return me(); }
137
138     private:
139         // Use defaultBasename() instead.
140         using MyBase::defaultValue;
141         using MyBase::defaultValueIfSet;
142
143         //! Creates a FileNameOptionStorage object.
144         virtual AbstractOptionStoragePointer createStorage() const;
145
146         OptionFileType          filetype_;
147         int                     legacyType_;
148         const char             *defaultBasename_;
149         bool                    bRead_;
150         bool                    bWrite_;
151         bool                    bLibrary_;
152
153         /*! \brief
154          * Needed to initialize FileNameOptionStorage from this class without
155          * otherwise unnecessary accessors.
156          */
157         friend class FileNameOptionStorage;
158 };
159
160 /*! \brief
161  * Wrapper class for accessing file name option information.
162  *
163  * \inpublicapi
164  * \ingroup module_options
165  */
166 class FileNameOptionInfo : public OptionInfo
167 {
168     public:
169         //! Shorthand for a list of extensions.
170         typedef ConstArrayRef<const char *> ExtensionList;
171
172         //! Creates an option info object for the given option.
173         explicit FileNameOptionInfo(FileNameOptionStorage *option);
174
175         //! Whether the option specifies an input file.
176         bool isInputFile() const;
177         //! Whether the option specifies an output file.
178         bool isOutputFile() const;
179         //! Whether the option specifies a file used for both input and output.
180         bool isInputOutputFile() const;
181         /*! \brief
182          * Whether the option specifies a library file.
183          *
184          * \see FileNameOption::libraryFile()
185          */
186         bool isLibraryFile() const;
187
188         //! Whether the option specifies directories.
189         bool isDirectoryOption() const;
190         //! Returns the list of extensions this option accepts.
191         ExtensionList extensions() const;
192
193     private:
194         const FileNameOptionStorage &option() const;
195 };
196
197 } // namespace gmx
198
199 #endif