1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements command line options parsing
15 #include <yaz/options.h>
17 static int arg_no = 1;
18 static size_t arg_off = 0;
19 static int eof_options = 0;
21 int options(const char *desc, char **argv, int argc, char **arg)
23 const char *opt_buf = 0;
28 return YAZ_OPTIONS_EOF;
31 while (argv[arg_no][0] == '\0')
35 return YAZ_OPTIONS_EOF;
37 if (argv[arg_no][0] != '-' || argv[arg_no][1] == '\0' || eof_options)
39 *arg = argv[arg_no++];
42 arg_off++; /* skip - */
44 /* we're in option mode */
45 if (argv[arg_no][1] == '-')
47 if (argv[arg_no][2] == '\0') /* -- : end of options */
53 return YAZ_OPTIONS_EOF;
54 *arg = argv[arg_no++];
57 opt_buf = argv[arg_no]+2;
58 arg_off = strlen(argv[arg_no]);
61 { /* single char opt */
62 ch = argv[arg_no][arg_off++];
66 int desc_char = desc[i++];
68 while (desc[i] == '{')
71 while (desc[i] && desc[i] != '}')
73 if (opt_buf && (i - i0) == strlen(opt_buf) &&
74 memcmp(opt_buf, desc+i0, i - i0) == 0)
80 { /* option with string argument */
85 if (type) /* option with argument */
87 if (argv[arg_no][arg_off])
89 *arg = argv[arg_no]+arg_off;
98 *arg = argv[arg_no++];
103 else /* option with no argument */
105 if (!argv[arg_no][arg_off])
114 *arg = argv[arg_no]+arg_off-1;
117 return YAZ_OPTIONS_ERROR;
122 * c-file-style: "Stroustrup"
123 * indent-tabs-mode: nil
125 * vim: shiftwidth=4 tabstop=8 expandtab