1 #include <ccan/opt/opt.h>
12 struct opt_table *opt_table;
13 unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long;
14 const char *opt_argv0;
16 /* Returns string after first '-'. */
17 static const char *first_name(const char *names, unsigned *len)
19 *len = strcspn(names + 1, "|= ");
23 static const char *next_name(const char *names, unsigned *len)
26 if (names[0] == ' ' || names[0] == '=' || names[0] == '\0')
28 return first_name(names + 1, len);
31 static const char *first_opt(unsigned *i, unsigned *len)
33 for (*i = 0; *i < opt_count; (*i)++) {
34 if (opt_table[*i].type == OPT_SUBTABLE)
36 return first_name(opt_table[*i].names, len);
41 static const char *next_opt(const char *p, unsigned *i, unsigned *len)
43 for (; *i < opt_count; (*i)++) {
44 if (opt_table[*i].type == OPT_SUBTABLE)
47 return first_name(opt_table[*i].names, len);
48 p = next_name(p, len);
55 const char *first_lopt(unsigned *i, unsigned *len)
58 for (p = first_opt(i, len); p; p = next_opt(p, i, len)) {
60 /* Skip leading "-" */
69 const char *next_lopt(const char *p, unsigned *i, unsigned *len)
71 for (p = next_opt(p, i, len); p; p = next_opt(p, i, len)) {
73 /* Skip leading "-" */
82 const char *first_sopt(unsigned *i)
85 unsigned int len = 0 /* GCC bogus warning */;
87 for (p = first_opt(i, &len); p; p = next_opt(p, i, &len)) {
94 const char *next_sopt(const char *p, unsigned *i)
97 for (p = next_opt(p, i, &len); p; p = next_opt(p, i, &len)) {
104 static void check_opt(const struct opt_table *entry)
109 if (entry->type != OPT_HASARG && entry->type != OPT_NOARG)
110 errx(1, "Option %s: unknown entry type %u",
111 entry->names, entry->type);
114 errx(1, "Option %s: description cannot be NULL", entry->names);
117 if (entry->names[0] != '-')
118 errx(1, "Option %s: does not begin with '-'", entry->names);
120 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
123 errx(1, "Option %s: invalid long option '--'",
128 errx(1, "Option %s: invalid short option"
129 " '%.*s'", entry->names, len+1, p-1);
131 if (entry->type == OPT_HASARG)
134 /* Don't document args unless there are some. */
135 if (entry->type == OPT_NOARG) {
136 if (p[len] == ' ' || p[len] == '=')
137 errx(1, "Option %s: does not take arguments"
138 " '%s'", entry->names, p+len+1);
143 static void add_opt(const struct opt_table *entry)
145 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
146 opt_table[opt_count++] = *entry;
149 void _opt_register(const char *names, enum opt_type type,
150 char *(*cb)(void *arg),
151 char *(*cb_arg)(const char *optarg, void *arg),
152 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
153 void *arg, const char *desc)
155 struct opt_table opt;
167 void opt_register_table(const struct opt_table entry[], const char *desc)
169 unsigned int i, start = opt_count;
172 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
175 for (i = 0; entry[i].type != OPT_END; i++) {
176 if (entry[i].type == OPT_SUBTABLE)
177 opt_register_table(subtable_of(&entry[i]),
180 check_opt(&entry[i]);
184 /* We store the table length in arg ptr. */
186 opt_table[start].arg = (void *)(intptr_t)(opt_count - start);
189 /* Parse your arguments. */
190 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
195 /* This helps opt_usage. */
198 while ((ret = parse_one(argc, argv, &offset, errlog)) == 1);
200 /* parse_one returns 0 on finish, -1 on error */
204 void opt_log_stderr(const char *fmt, ...)
209 vfprintf(stderr, fmt, ap);
210 fprintf(stderr, "\n");
214 void opt_log_stderr_exit(const char *fmt, ...)
219 vfprintf(stderr, fmt, ap);
220 fprintf(stderr, "\n");
225 char *opt_invalid_argument(const char *arg)
227 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
228 sprintf(str, "Invalid argument '%s'", arg);