2c2efd60fa162d772bdb8ceb3a06fcc40208fa91
[alexxy/gromacs.git] / src / gromacs / commandline / pargs.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2017,2018,2019, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /*! \file
38  * \brief
39  * Declares \c t_pargs, parse_common_args() and related methods.
40  *
41  * \inpublicapi
42  * \ingroup module_commandline
43  */
44 #ifndef GMX_COMMANDLINE_PARGS_H
45 #define GMX_COMMANDLINE_PARGS_H
46
47 #include "gromacs/commandline/filenm.h"
48 #include "gromacs/fileio/oenv.h"
49 #include "gromacs/math/vectypes.h"
50 #include "gromacs/utility/basedefinitions.h"
51 #include "gromacs/utility/real.h"
52
53 struct gmx_output_env_t;
54
55 /*! \addtogroup module_commandline
56  * \{
57  */
58
59 /** Command line argument type. */
60 enum
61 {
62     etINT,
63     etINT64,
64     etREAL,
65     etTIME,
66     etSTR,
67     etBOOL,
68     etRVEC,
69     etENUM,
70     etNR
71 };
72
73 /*! \brief
74  * Command-line argument definition for C code.
75  *
76  * \inpublicapi
77  */
78 typedef struct
79 {
80     /** Name of the argument (with leading dash included). */
81     const char* option;
82     /** Whether the argument is set (should be initialized to `FALSE`). */
83     gmx_bool bSet;
84     /** Type of the argument (one of the enums in pargs.h). */
85     int type;
86     /*! \brief
87      * Pointer to variable that is to receive the value.
88      *
89      * The expected type depends on the value of \a type.
90      * If an argument is not set by the user, then the pointed value is not
91      * changed.  In other words, the initial value for the variable defines the
92      * default value.
93      */
94     union {
95         /*! \brief
96          * Generic pointer for operations that do not need type information.
97          *
98          * Needs to be the first member to use initialized arrays.
99          */
100         void* v;
101         /** Integer value for etINT. */
102         int* i;
103         /** Integer value for etINT64. */
104         int64_t* is;
105         /** Real value for etREAL and etTIME. */
106         real* r;
107         /*! \brief
108          * String value for etSTR and etENUM.
109          *
110          * For etSTR, this should point to a `const char *` variable, which
111          * will receive a pointer to the string value (caller should not free
112          * the string).
113          *
114          * For etENUM, this should be an array of `const char *` values, where
115          * the first and last values are `NULL`, and the other values define
116          * the allowed enum values.  The first non-NULL value is the default
117          * value.  After the arguments are parsed, the first element in the array
118          * points to the selected enum value (pointers will be equal).
119          */
120         const char** c;
121         /** Boolean value for etBOOL. */
122         gmx_bool* b;
123         /** Vector value for etRVEC. */
124         rvec* rv;
125     } u;
126     /*! \brief
127      * Description for the argument.
128      *
129      * If the string starts with `HIDDEN`, then the argument is hidden from
130      * normal help listing and shell completions.
131      */
132     const char* desc;
133 } t_pargs;
134
135 /*! \brief
136  * Returns ordinal number for an etENUM argument.
137  *
138  * \param[in] enumc  Array passed to `t_pargs` for an etENUM argument.
139  * \returns   Index of selected enum value in the array.
140  *
141  * See t_pargs::u::c for the expected format of the array, including how the
142  * first element should be initialized.
143  * Note that the return value starts at one instead of zero: if the first enum
144  * value is selected, this returns 1.
145  */
146 int nenum(const char* const enumc[]);
147
148 /*! \brief
149  * Returns value of an etINT option.
150  *
151  * \param[in] option Name of etINT argument to query.
152  * \param[in] nparg  Number of elements in \p pa.
153  * \param[in] pa     Array of arguments.
154  * \returns   Value of \p option.
155  *
156  * \p option must specify a valid argument in \p pa of the correct type.
157  */
158 int opt2parg_int(const char* option, int nparg, t_pargs pa[]);
159
160 /*! \brief
161  * Returns value of an etBOOL option.
162  *
163  * \param[in] option Name of etBOOL argument to query.
164  * \param[in] nparg  Number of elements in \p pa.
165  * \param[in] pa     Array of arguments.
166  * \returns   Value of \p option.
167  *
168  * \p option must specify a valid argument in \p pa of the correct type.
169  */
170 gmx_bool opt2parg_bool(const char* option, int nparg, t_pargs pa[]);
171
172 /*! \brief
173  * Returns value of an etREAL/etTIME option.
174  *
175  * \param[in] option Name of etREAL/etTIME argument to query.
176  * \param[in] nparg  Number of elements in \p pa.
177  * \param[in] pa     Array of arguments.
178  * \returns   Value of \p option.
179  *
180  * \p option must specify a valid argument in \p pa of the correct type.
181  */
182 real opt2parg_real(const char* option, int nparg, t_pargs pa[]);
183
184 /*! \brief
185  * Returns value of an etSTR option.
186  *
187  * \param[in] option Name of etSTR argument to query.
188  * \param[in] nparg  Number of elements in \p pa.
189  * \param[in] pa     Array of arguments.
190  * \returns   Value of \p option.
191  *
192  * \p option must specify a valid argument in \p pa of the correct type.
193  */
194 const char* opt2parg_str(const char* option, int nparg, t_pargs pa[]);
195
196 /*! \brief
197  * Returns value of an etENUM option.
198  *
199  * \param[in] option Name of etENUM argument to query.
200  * \param[in] nparg  Number of elements in \p pa.
201  * \param[in] pa     Array of arguments.
202  * \returns   Value of \p option.
203  *
204  * \p option must specify a valid argument in \p pa of the correct type.
205  */
206 const char* opt2parg_enum(const char* option, int nparg, t_pargs pa[]);
207
208 /*! \brief
209  * Returns whether an argument has been set.
210  *
211  * \param[in] option Name of argument to check.
212  * \param[in] nparg  Number of elements in \p pa.
213  * \param[in] pa     Array of arguments.
214  * \returns   `true` if \p option has been set.
215  *
216  * \p option must specify a valid argument in \p pa.
217  */
218 gmx_bool opt2parg_bSet(const char* option, int nparg, const t_pargs* pa);
219
220
221 /** Add option -w to view output files (must be implemented in program). */
222 #define PCA_CAN_VIEW (1 << 5)
223 /** Add option to set begin time for trajectory reading. */
224 #define PCA_CAN_BEGIN (1 << 6)
225 /** Add option to set end time for trajectory reading. */
226 #define PCA_CAN_END (1 << 7)
227 /** Add option to set time step for trajectory reading. */
228 #define PCA_CAN_DT (1 << 14)
229 /** Add all options for trajectory time control. */
230 #define PCA_CAN_TIME (PCA_CAN_BEGIN | PCA_CAN_END | PCA_CAN_DT)
231 /** Add option -tu to set time unit for output. */
232 #define PCA_TIME_UNIT (1 << 15)
233 /** Add option -deffnm to set default for all file options. */
234 #define PCA_CAN_SET_DEFFNM (1 << 10)
235 /** Do not raise a fatal error when invalid options are encountered. */
236 #define PCA_NOEXIT_ON_ARGS (1 << 11)
237 /** Don't do any special processing for ffREAD files */
238 #define PCA_DISABLE_INPUT_FILE_CHECKING (1 << 17)
239
240 /*! \brief
241  * Parse command-line arguments.
242  *
243  * Some common default arguments are also recognized in addition to those
244  * provided through \p pa.  The set of recognized default arguments is affected
245  * by \p Flags.
246  *
247  * Recognized arguments are removed from the list.
248  *
249  * For full functionality, this function needs to be used within a function
250  * that is passed to gmx_run_cmain().  It should be called as the first thing in
251  * that function.  Initialization code can be executed before it, but you need
252  * to be aware that if the program is executed with -h and MPI, the code before
253  * parse_common_args() only executes on the master node.
254  *
255  * If the return value is `FALSE`, the program should return immediately (this
256  * is necessary for -h and a few other cases).
257  *
258  * \see gmx_run_cmain().
259  */
260 gmx_bool parse_common_args(int*               argc,
261                            char*              argv[],
262                            unsigned long      Flags,
263                            int                nfile,
264                            t_filenm           fnm[],
265                            int                npargs,
266                            t_pargs*           pa,
267                            int                ndesc,
268                            const char**       desc,
269                            int                nbugs,
270                            const char**       bugs,
271                            gmx_output_env_t** oenv);
272
273 /*! \} */
274
275 #endif