Added silc_getopt.
[silc.git] / lib / silcutil / silcgetopt.h
1 /*
2
3   silcgetopt.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef SILCGETOPT_H
21 #define SILCGETOPT_H
22
23 /****s* silcutil/SilcGetOptAPI/SilcGetOpt
24  *
25  * NAME
26  *
27  *    typedef struct SilcGetOptObject { ... } *SilcGetOpt, SilcGetOptStruct;
28  *
29  * DESCRIPTION
30  *
31  *    Command line option parsers structure given to silc_getopt as argument.
32  *    It contains the current parsed command line option data.
33  *
34  * SOURCE
35  */
36 typedef struct SilcGetOptObject {
37   int opt_index;                /* Current option index in argv[] array */
38   int opt_option;               /* Current option character */
39   char *opt_arg;                /* Current parsed option argument */
40   SilcBool opt_error;           /* Set this to TRUE to make silc_getopt print
41                                    errors or FALSE to suppress them. */
42
43   SilcUInt16 opt_sp;            /* Internal parser index */
44 } *SilcGetOpt, SilcGetOptStruct;
45 /***/
46
47 /****d* silcutil/SilcGetOptAPI/SILC_GETOPT_INIT
48  *
49  * NAME
50  *
51  *    #define SILC_GETOPT_INIT ...
52  *
53  * DESCRIPTION
54  *
55  *    Macro used to initialize SilcGetOptStruct before calling silc_getopt.
56  *
57  * EXAMPLE
58  *
59  *    SilcGetOptStruct op = SILC_GETOPT_INIT;
60  *
61  ***/
62 #define SILC_GETOPT_INIT { 1, 0, NULL, TRUE, 1 }
63
64 /****f* silcutil/SilcGetOptAPI/silc_getopt
65  *
66  * SYNOPSIS
67  *
68  *    int silc_getopt(int argc, char **argv, const char *optstring,
69  *                    SilcGetOpt op)
70  *
71  * DESCRIPTION
72  *
73  *    Parses comand line options.  This function is equivalent to getopt(3).
74  *    Returns the current parsed option, '?' if option was unknown, ':' if
75  *    required argument was missing or -1 after all options have been parsed.
76  *    If options require arguments they are available from the `op' structure,
77  *    to where the options are parsed.  The parsing is stopped immediately
78  *    when first non-option character, which is not an argument for an option,
79  *    is encountered.
80  *
81  *    The `optstring' contains the supported option characters.  One character
82  *    per option is required.  If colon (':') follows option character the
83  *    option requires an argument.  If two colons ('::') follows option
84  *    character the argument is optional.  In that case the argument must
85  *    follow the option in command line, for example -oarg, instead of -o arg.
86  *
87  * EXAMPLE
88  *
89  *    int main(int argc, char **argv)
90  *    {
91  *      SilcGetOptStruct op = SILC_GETOPT_INIT;
92  *
93  *      while ((option = silc_getopt(argc, argv, "ab:t::", &op)) != -1) {
94  *        switch (option) {
95  *          case 'a':
96  *            ...
97  *            break;
98  *          case 'b':
99  *            argument = silc_strdup(op.opt_arg);
100  *            break;
101  *          case 't':
102  *            if (op.opt_arg)
103  *              optional_argument = silc_strdup(op.opt_arg);
104  *            break;
105  *          default:
106  *            exit(1);
107  *            break;
108  *        }
109  *      }
110  *    }
111  *
112  ***/
113 int silc_getopt(int argc, char **argv, const char *optstring, SilcGetOpt op);
114
115 #endif /* SILCGETOPT_H */