1 /* Licensed under GPLv3+ - see LICENSE file for details */
2 #include <ccan/opt/opt.h>
13 struct opt_table *opt_table;
14 unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long;
15 const char *opt_argv0;
17 /* Returns string after first '-'. */
18 static const char *first_name(const char *names, unsigned *len)
20 *len = strcspn(names + 1, "|= ");
24 static const char *next_name(const char *names, unsigned *len)
27 if (names[0] == ' ' || names[0] == '=' || names[0] == '\0')
29 return first_name(names + 1, len);
32 static const char *first_opt(unsigned *i, unsigned *len)
34 for (*i = 0; *i < opt_count; (*i)++) {
35 if (opt_table[*i].type == OPT_SUBTABLE)
37 return first_name(opt_table[*i].names, len);
42 static const char *next_opt(const char *p, unsigned *i, unsigned *len)
44 for (; *i < opt_count; (*i)++) {
45 if (opt_table[*i].type == OPT_SUBTABLE)
48 return first_name(opt_table[*i].names, len);
49 p = next_name(p, len);
56 const char *first_lopt(unsigned *i, unsigned *len)
59 for (p = first_opt(i, len); p; p = next_opt(p, i, len)) {
61 /* Skip leading "-" */
70 const char *next_lopt(const char *p, unsigned *i, unsigned *len)
72 for (p = next_opt(p, i, len); p; p = next_opt(p, i, len)) {
74 /* Skip leading "-" */
83 const char *first_sopt(unsigned *i)
86 unsigned int len = 0 /* GCC bogus warning */;
88 for (p = first_opt(i, &len); p; p = next_opt(p, i, &len)) {
95 const char *next_sopt(const char *p, unsigned *i)
98 for (p = next_opt(p, i, &len); p; p = next_opt(p, i, &len)) {
105 static void check_opt(const struct opt_table *entry)
110 if (entry->type != OPT_HASARG && entry->type != OPT_NOARG)
111 errx(1, "Option %s: unknown entry type %u",
112 entry->names, entry->type);
115 errx(1, "Option %s: description cannot be NULL", entry->names);
118 if (entry->names[0] != '-')
119 errx(1, "Option %s: does not begin with '-'", entry->names);
121 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
124 errx(1, "Option %s: invalid long option '--'",
129 errx(1, "Option %s: invalid short option"
130 " '%.*s'", entry->names, len+1, p-1);
132 if (entry->type == OPT_HASARG)
135 /* Don't document args unless there are some. */
136 if (entry->type == OPT_NOARG) {
137 if (p[len] == ' ' || p[len] == '=')
138 errx(1, "Option %s: does not take arguments"
139 " '%s'", entry->names, p+len+1);
144 static void add_opt(const struct opt_table *entry)
146 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
147 opt_table[opt_count++] = *entry;
150 void _opt_register(const char *names, enum opt_type type,
151 char *(*cb)(void *arg),
152 char *(*cb_arg)(const char *optarg, void *arg),
153 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
154 const void *arg, const char *desc)
156 struct opt_table opt;
168 void opt_register_table(const struct opt_table entry[], const char *desc)
170 unsigned int i, start = opt_count;
173 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
176 for (i = 0; entry[i].type != OPT_END; i++) {
177 if (entry[i].type == OPT_SUBTABLE)
178 opt_register_table(subtable_of(&entry[i]),
181 check_opt(&entry[i]);
185 /* We store the table length in arg ptr. */
187 opt_table[start].u.tlen = (opt_count - start);
190 /* Parse your arguments. */
191 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
196 /* This helps opt_usage. */
199 while ((ret = parse_one(argc, argv, &offset, errlog)) == 1);
201 /* parse_one returns 0 on finish, -1 on error */
205 void opt_free_table(void)
211 void opt_log_stderr(const char *fmt, ...)
216 vfprintf(stderr, fmt, ap);
217 fprintf(stderr, "\n");
221 void opt_log_stderr_exit(const char *fmt, ...)
226 vfprintf(stderr, fmt, ap);
227 fprintf(stderr, "\n");
232 char *opt_invalid_argument(const char *arg)
234 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
235 sprintf(str, "Invalid argument '%s'", arg);