SILC Runtime Toolkit 1.2 Beta 1
[runtime.git] / lib / silcutil / silcgetopt.h
1 /*
2
3   silcgetopt.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 - 2008 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 /****h* silcutil/GetOpt Interface
21  *
22  * DESCRIPTION
23  *
24  * SILC GetOpt Interface provides portable interface to handle command
25  * line options.
26  *
27  ***/
28
29 #ifndef SILCGETOPT_H
30 #define SILCGETOPT_H
31
32 /****s* silcutil/SilcGetOpt
33  *
34  * NAME
35  *
36  *    typedef struct SilcGetOptObject { ... } *SilcGetOpt, SilcGetOptStruct;
37  *
38  * DESCRIPTION
39  *
40  *    Command line option parsers structure given to silc_getopt as argument.
41  *    It contains the current parsed command line option data.
42  *
43  * SOURCE
44  */
45 typedef struct SilcGetOptObject {
46   int opt_index;                /* Current option index in argv[] array */
47   int opt_option;               /* Current option character */
48   char *opt_arg;                /* Current parsed option argument */
49   SilcBool opt_error;           /* Set this to TRUE to make silc_getopt print
50                                    errors or FALSE to suppress them. */
51
52   SilcUInt16 opt_sp;            /* Internal parser index */
53 } *SilcGetOpt, SilcGetOptStruct;
54 /***/
55
56 /****d* silcutil/SILC_GETOPT_INIT
57  *
58  * NAME
59  *
60  *    #define SILC_GETOPT_INIT ...
61  *
62  * DESCRIPTION
63  *
64  *    Macro used to initialize SilcGetOptStruct before calling silc_getopt.
65  *
66  * EXAMPLE
67  *
68  *    SilcGetOptStruct op = SILC_GETOPT_INIT;
69  *
70  ***/
71 #define SILC_GETOPT_INIT { 1, 0, NULL, TRUE, 1 }
72
73 /****f* silcutil/silc_getopt
74  *
75  * SYNOPSIS
76  *
77  *    int silc_getopt(int argc, char **argv, const char *optstring,
78  *                    SilcGetOpt op)
79  *
80  * DESCRIPTION
81  *
82  *    Parses comand line options.  This function is equivalent to getopt(3).
83  *    Returns the current parsed option, '?' if option was unknown, ':' if
84  *    required argument was missing or -1 after all options have been parsed.
85  *    If options require arguments they are available from the `op' structure,
86  *    to where the options are parsed.  The parsing is stopped immediately
87  *    when first non-option character, which is not an argument for an option,
88  *    is encountered.
89  *
90  *    The `optstring' contains the supported option characters.  One character
91  *    per option is required.  If colon (':') follows option character the
92  *    option requires an argument.  If two colons ('::') follows option
93  *    character the argument is optional.  In that case the argument must
94  *    follow the option in command line, for example -oarg, instead of -o arg.
95  *
96  * EXAMPLE
97  *
98  *    int main(int argc, char **argv)
99  *    {
100  *      SilcGetOptStruct op = SILC_GETOPT_INIT;
101  *
102  *      while ((option = silc_getopt(argc, argv, "ab:t::", &op)) != -1) {
103  *        switch (option) {
104  *          case 'a':
105  *            ...
106  *            break;
107  *          case 'b':
108  *            argument = silc_strdup(op.opt_arg);
109  *            break;
110  *          case 't':
111  *            if (op.opt_arg)
112  *              optional_argument = silc_strdup(op.opt_arg);
113  *            break;
114  *          default:
115  *            exit(1);
116  *            break;
117  *        }
118  *      }
119  *    }
120  *
121  ***/
122 int silc_getopt(int argc, char **argv, const char *optstring, SilcGetOpt op);
123
124 #endif /* SILCGETOPT_H */