f6a2ab680cc0f5f23605ab15365b06aa5816e237
[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, 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 "../legacyheaders/oenv.h"
48 #include "../fileio/filenm.h"
49 #include "../math/vectypes.h"
50 #include "../utility/basedefinitions.h"
51 #include "../utility/real.h"
52
53 #ifdef __cplusplus
54 extern "C"
55 {
56 #endif
57
58 /*! \addtogroup module_commandline
59  * \{
60  */
61
62 /** Command line argument type. */
63 enum
64 {
65     etINT, etINT64, etREAL, etTIME, etSTR, etBOOL, etRVEC, etENUM, etNR
66 };
67
68 /*! \brief
69  * Command-line argument definition for C code.
70  *
71  * \inpublicapi
72  */
73 typedef struct
74 {
75     /** Name of the argument (with leading dash included). */
76     const char *option;
77     /** Whether the argument is set (should be initialized to `FALSE`). */
78     gmx_bool    bSet;
79     /** Type of the argument (one of the enums in pargs.h). */
80     int         type;
81     /*! \brief
82      * Pointer to variable that is to receive the value.
83      *
84      * The expected type depends on the value of \a type.
85      * If an argument is not set by the user, then the pointed value is not
86      * changed.  In other words, the initial value for the variable defines the
87      * default value.
88      */
89     union
90     {
91         /*! \brief
92          * Generic pointer for operations that do not need type information.
93          *
94          * Needs to be the first member to use initialized arrays.
95          */
96         void            *v;
97         /** Integer value for etINT. */
98         int             *i;
99         /** Integer value for etINT64. */
100         gmx_int64_t     *is;
101         /** Real value for etREAL and etTIME. */
102         real            *r;
103         /*! \brief
104          * String value for etSTR and etENUM.
105          *
106          * For etSTR, this should point to a `const char *` variable, which
107          * will receive a pointer to the string value (caller should not free
108          * the string).
109          *
110          * For etENUM, this should be an array of `const char *` values, where
111          * the first and last values are `NULL`, and the other values define
112          * the allowed enum values.  The first non-NULL value is the default
113          * value.  After the arguments are parsed, the first element in the array
114          * points to the selected enum value (pointers will be equal).
115          */
116         const char     **c;
117         /** Boolean value for etBOOL. */
118         gmx_bool        *b;
119         /** Vector value for etRVEC. */
120         rvec            *rv;
121     }           u;
122     /*! \brief
123      * Description for the argument.
124      *
125      * If the string starts with `HIDDEN`, then the argument is hidden from
126      * normal help listing and shell completions.
127      */
128     const char *desc;
129 } t_pargs;
130
131 /*! \brief
132  * Returns ordinal number for an etENUM argument.
133  *
134  * \param[in] enumc  Array passed to `t_pargs` for an etENUM argument.
135  * \returns   Index of selected enum value in the array.
136  *
137  * See t_pargs::u::c for the expected format of the array, including how the
138  * first element should be initialized.
139  * Note that the return value starts at one instead of zero: if the first enum
140  * value is selected, this returns 1.
141  */
142 int nenum(const char *const enumc[]);
143
144 /*! \brief
145  * Returns value of an etINT option.
146  *
147  * \param[in] option Name of etINT argument to query.
148  * \param[in] nparg  Number of elements in \p pa.
149  * \param[in] pa     Array of arguments.
150  * \returns   Value of \p option.
151  *
152  * \p option must specify a valid argument in \p pa of the correct type.
153  */
154 int opt2parg_int(const char *option, int nparg, t_pargs pa[]);
155
156 /*! \brief
157  * Returns value of an etBOOL option.
158  *
159  * \param[in] option Name of etBOOL argument to query.
160  * \param[in] nparg  Number of elements in \p pa.
161  * \param[in] pa     Array of arguments.
162  * \returns   Value of \p option.
163  *
164  * \p option must specify a valid argument in \p pa of the correct type.
165  */
166 gmx_bool opt2parg_bool(const char *option, int nparg, t_pargs pa[]);
167
168 /*! \brief
169  * Returns value of an etREAL/etTIME option.
170  *
171  * \param[in] option Name of etREAL/etTIME argument to query.
172  * \param[in] nparg  Number of elements in \p pa.
173  * \param[in] pa     Array of arguments.
174  * \returns   Value of \p option.
175  *
176  * \p option must specify a valid argument in \p pa of the correct type.
177  */
178 real opt2parg_real(const char *option, int nparg, t_pargs pa[]);
179
180 /*! \brief
181  * Returns value of an etSTR option.
182  *
183  * \param[in] option Name of etSTR argument to query.
184  * \param[in] nparg  Number of elements in \p pa.
185  * \param[in] pa     Array of arguments.
186  * \returns   Value of \p option.
187  *
188  * \p option must specify a valid argument in \p pa of the correct type.
189  */
190 const char *opt2parg_str(const char *option, int nparg, t_pargs pa[]);
191
192 /*! \brief
193  * Returns value of an etENUM option.
194  *
195  * \param[in] option Name of etENUM argument to query.
196  * \param[in] nparg  Number of elements in \p pa.
197  * \param[in] pa     Array of arguments.
198  * \returns   Value of \p option.
199  *
200  * \p option must specify a valid argument in \p pa of the correct type.
201  */
202 const char *opt2parg_enum(const char *option, int nparg, t_pargs pa[]);
203
204 /*! \brief
205  * Returns whether an argument has been set.
206  *
207  * \param[in] option Name of argument to check.
208  * \param[in] nparg  Number of elements in \p pa.
209  * \param[in] pa     Array of arguments.
210  * \returns   `true` if \p option has been set.
211  *
212  * \p option must specify a valid argument in \p pa.
213  */
214 gmx_bool opt2parg_bSet(const char *option, int nparg, t_pargs pa[]);
215
216
217 /** Add option -w to view output files (must be implemented in program). */
218 #define PCA_CAN_VIEW       (1<<5)
219 /** Add option to set begin time for trajectory reading. */
220 #define PCA_CAN_BEGIN      (1<<6)
221 /** Add option to set end time for trajectory reading. */
222 #define PCA_CAN_END        (1<<7)
223 /** Add option to set time step for trajectory reading. */
224 #define PCA_CAN_DT         (1<<14)
225 /** Add all options for trajectory time control. */
226 #define PCA_CAN_TIME       (PCA_CAN_BEGIN | PCA_CAN_END | PCA_CAN_DT)
227 /** Add option -tu to set time unit for output. */
228 #define PCA_TIME_UNIT      (1<<15)
229 /** Add option -deffnm to set default for all file options. */
230 #define PCA_CAN_SET_DEFFNM (1<<10)
231 /** Do not raise a fatal error when invalid options are encountered. */
232 #define PCA_NOEXIT_ON_ARGS (1<<11)
233 /** Is this node not reading: for parallel all nodes but the master */
234 #define PCA_NOT_READ_NODE  (1<<16)
235 /** Don't do any special processing for ffREAD files */
236 #define PCA_DISABLE_INPUT_FILE_CHECKING (1<<17)
237
238 /*! \brief
239  * Parse command-line arguments.
240  *
241  * Some common default arguments are also recognized in addition to those
242  * provided through \p pa.  The set of recognized default arguments is affected
243  * by \p Flags.
244  *
245  * Recognized arguments are removed from the list.
246  *
247  * For full functionality, this function needs to be used within a function
248  * that is passed to gmx_run_cmain().  It should be called as the first thing in
249  * that function.  Initialization code can be executed before it, but you need
250  * to be aware that if the program is executed with -h and MPI, the code before
251  * parse_common_args() only executes on the master node.
252  *
253  * If the return value is `FALSE`, the program should return immediately (this
254  * is necessary for -h and a few other cases).
255  *
256  * \see gmx_run_cmain().
257  */
258 gmx_bool parse_common_args(int *argc, char *argv[], unsigned long Flags,
259                            int nfile, t_filenm fnm[], int npargs, t_pargs *pa,
260                            int ndesc, const char **desc,
261                            int nbugs, const char **bugs,
262                            output_env_t *oenv);
263
264 /*! \} */
265
266 #ifdef __cplusplus
267 }
268 #endif
269
270 #endif