5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 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.
23 /****s* silcutil/SilcGetOptAPI/SilcGetOpt
27 * typedef struct SilcGetOptObject { ... } *SilcGetOpt, SilcGetOptStruct;
31 * Command line option parsers structure given to silc_getopt as argument.
32 * It contains the current parsed command line option data.
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. */
43 SilcUInt16 opt_sp; /* Internal parser index */
44 } *SilcGetOpt, SilcGetOptStruct;
47 /****d* silcutil/SilcGetOptAPI/SILC_GETOPT_INIT
51 * #define SILC_GETOPT_INIT ...
55 * Macro used to initialize SilcGetOptStruct before calling silc_getopt.
59 * SilcGetOptStruct op = SILC_GETOPT_INIT;
62 #define SILC_GETOPT_INIT { 1, 0, NULL, TRUE, 1 }
64 /****f* silcutil/SilcGetOptAPI/silc_getopt
68 * int silc_getopt(int argc, char **argv, const char *optstring,
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,
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.
89 * int main(int argc, char **argv)
91 * SilcGetOptStruct op = SILC_GETOPT_INIT;
93 * while ((option = silc_getopt(argc, argv, "ab:t::", &op)) != -1) {
99 * argument = silc_strdup(op.opt_arg);
103 * optional_argument = silc_strdup(op.opt_arg);
113 int silc_getopt(int argc, char **argv, const char *optstring, SilcGetOpt op);
115 #endif /* SILCGETOPT_H */