5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 - 2008 Pekka Riikonen
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.
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.
20 /****h* silcutil/GetOpt Interface
24 * SILC GetOpt Interface provides portable interface to handle command
32 /****s* silcutil/SilcGetOpt
36 * typedef struct SilcGetOptObject { ... } *SilcGetOpt, SilcGetOptStruct;
40 * Command line option parsers structure given to silc_getopt as argument.
41 * It contains the current parsed command line option data.
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. */
52 SilcUInt16 opt_sp; /* Internal parser index */
53 } *SilcGetOpt, SilcGetOptStruct;
56 /****d* silcutil/SILC_GETOPT_INIT
60 * #define SILC_GETOPT_INIT ...
64 * Macro used to initialize SilcGetOptStruct before calling silc_getopt.
68 * SilcGetOptStruct op = SILC_GETOPT_INIT;
71 #define SILC_GETOPT_INIT { 1, 0, NULL, TRUE, 1 }
73 /****f* silcutil/silc_getopt
77 * int silc_getopt(int argc, char **argv, const char *optstring,
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,
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.
98 * int main(int argc, char **argv)
100 * SilcGetOptStruct op = SILC_GETOPT_INIT;
102 * while ((option = silc_getopt(argc, argv, "ab:t::", &op)) != -1) {
108 * argument = silc_strdup(op.opt_arg);
112 * optional_argument = silc_strdup(op.opt_arg);
122 int silc_getopt(int argc, char **argv, const char *optstring, SilcGetOpt op);
124 #endif /* SILCGETOPT_H */