1 /* Licensed under GPLv3+ - see LICENSE file for details */
2 #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 /* Avoids dependency on err.h or ccan/err */
106 #define failmsg(fmt, ...) \
107 do { fprintf(stderr, fmt, __VA_ARGS__); exit(1); } while(0)
110 static void check_opt(const struct opt_table *entry)
115 if (entry->type != OPT_HASARG && entry->type != OPT_NOARG
116 && entry->type != (OPT_EARLY|OPT_HASARG)
117 && entry->type != (OPT_EARLY|OPT_NOARG))
118 failmsg("Option %s: unknown entry type %u",
119 entry->names, entry->type);
122 failmsg("Option %s: description cannot be NULL", entry->names);
125 if (entry->names[0] != '-')
126 failmsg("Option %s: does not begin with '-'", entry->names);
128 for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
131 failmsg("Option %s: invalid long option '--'",
136 failmsg("Option %s: invalid short option"
137 " '%.*s'", entry->names, len+1, p-1);
139 if (entry->type == OPT_HASARG)
142 /* Don't document args unless there are some. */
143 if (entry->type == OPT_NOARG) {
144 if (p[len] == ' ' || p[len] == '=')
145 failmsg("Option %s: does not take arguments"
146 " '%s'", entry->names, p+len+1);
151 static void add_opt(const struct opt_table *entry)
153 opt_table = realloc(opt_table, sizeof(opt_table[0]) * (opt_count+1));
154 opt_table[opt_count++] = *entry;
157 void _opt_register(const char *names, enum opt_type type,
158 char *(*cb)(void *arg),
159 char *(*cb_arg)(const char *optarg, void *arg),
160 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
161 const void *arg, const char *desc)
163 struct opt_table opt;
175 void opt_register_table(const struct opt_table entry[], const char *desc)
177 unsigned int i, start = opt_count;
180 struct opt_table heading = OPT_SUBTABLE(NULL, desc);
183 for (i = 0; entry[i].type != OPT_END; i++) {
184 if (entry[i].type == OPT_SUBTABLE)
185 opt_register_table(subtable_of(&entry[i]),
188 check_opt(&entry[i]);
192 /* We store the table length in arg ptr. */
194 opt_table[start].u.tlen = (opt_count - start);
197 /* Parse your arguments. */
198 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
203 /* This helps opt_usage. */
206 while ((ret = parse_one(argc, argv, 0, &offset, errlog)) == 1);
208 /* parse_one returns 0 on finish, -1 on error */
212 bool opt_early_parse(int argc, char *argv[],
213 void (*errlog)(const char *fmt, ...))
217 char **tmpargv = malloc(sizeof(argv[0]) * (argc + 1));
219 /* We could avoid a copy and skip instead, but this is simple. */
220 memcpy(tmpargv, argv, sizeof(argv[0]) * (argc + 1));
222 /* This helps opt_usage. */
225 while ((ret = parse_one(&argc, tmpargv, OPT_EARLY, &off, errlog)) == 1);
229 /* parse_one returns 0 on finish, -1 on error */
233 void opt_free_table(void)
237 opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
240 void opt_log_stderr(const char *fmt, ...)
245 vfprintf(stderr, fmt, ap);
246 fprintf(stderr, "\n");
250 void opt_log_stderr_exit(const char *fmt, ...)
255 vfprintf(stderr, fmt, ap);
256 fprintf(stderr, "\n");
261 char *opt_invalid_argument(const char *arg)
263 char *str = malloc(sizeof("Invalid argument '%s'") + strlen(arg));
264 sprintf(str, "Invalid argument '%s'", arg);