1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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;
20 int options (const char *desc, char **argv, int argc, char **arg)
22 const char *opt_buf = 0;
27 return YAZ_OPTIONS_EOF;
30 while (argv[arg_no][0] == '\0')
34 return YAZ_OPTIONS_EOF;
36 if (argv[arg_no][0] != '-' || argv[arg_no][1] == '\0')
38 *arg = argv[arg_no++];
41 arg_off++; /* skip - */
43 if (argv[arg_no][1] == '-')
45 opt_buf = argv[arg_no]+2;
46 arg_off = strlen(argv[arg_no]);
49 { /* single char opt */
50 ch = argv[arg_no][arg_off++];
54 int desc_char = desc[i++];
56 while (desc[i] == '{')
59 while (desc[i] && desc[i] != '}')
61 if (opt_buf && (i - i0) == strlen(opt_buf) &&
62 memcmp(opt_buf, desc+i0, i - i0) == 0)
68 { /* option with string argument */
73 if (type) /* option with argument */
75 if (argv[arg_no][arg_off])
77 *arg = argv[arg_no]+arg_off;
86 *arg = argv[arg_no++];
91 else /* option with no argument */
93 if (!argv[arg_no][arg_off])
102 *arg = argv[arg_no]+arg_off-1;
105 return YAZ_OPTIONS_ERROR;
110 * c-file-style: "Stroustrup"
111 * indent-tabs-mode: nil
113 * vim: shiftwidth=4 tabstop=8 expandtab