1 /* Actual code to parse commandline. */
2 #include <ccan/opt/opt.h>
9 /tmp/opt-example: invalid option -- 'x'
10 /tmp/opt-example: unrecognized option '--long'
11 /tmp/opt-example: option '--someflag' doesn't allow an argument
12 /tmp/opt-example: option '--s' is ambiguous
13 /tmp/opt-example: option requires an argument -- 's'
15 static int parse_err(void (*errlog)(const char *fmt, ...),
16 const char *argv0, const char *arg, unsigned len,
19 errlog("%s: %.*s: %s", argv0, len, arg, problem);
23 static void consume_option(int *argc, char *argv[], unsigned optnum)
25 memmove(&argv[optnum], &argv[optnum+1],
26 sizeof(argv[optnum]) * (*argc-optnum));
30 /* Returns 1 if argument consumed, 0 if all done, -1 on error. */
31 int parse_one(int *argc, char *argv[], unsigned *offset,
32 void (*errlog)(const char *fmt, ...))
35 const char *o, *optarg = NULL;
38 if (getenv("POSIXLY_CORRECT")) {
39 /* Don't find options after non-options. */
42 for (arg = 1; argv[arg]; arg++) {
43 if (argv[arg][0] == '-')
48 if (!argv[arg] || argv[arg][0] != '-')
51 /* Special arg terminator option. */
52 if (strcmp(argv[arg], "--") == 0) {
53 consume_option(argc, argv, arg);
57 /* Long options start with -- */
58 if (argv[arg][1] == '-') {
60 for (o = first_lopt(&i, &len); o; o = next_lopt(o, &i, &len)) {
61 if (strncmp(argv[arg] + 2, o, len) != 0)
63 if (argv[arg][2 + len] == '=')
64 optarg = argv[arg] + 2 + len + 1;
65 else if (argv[arg][2 + len] != '\0')
70 return parse_err(errlog, argv[0],
71 argv[arg], strlen(argv[arg]),
72 "unrecognized option");
73 /* For error messages, we include the leading '--' */
77 /* offset allows us to handle -abc */
78 for (o = first_sopt(&i); o; o = next_sopt(o, &i)) {
79 if (argv[arg][*offset + 1] != *o)
85 return parse_err(errlog, argv[0],
86 argv[arg], strlen(argv[arg]),
87 "unrecognized option");
88 /* For error messages, we include the leading '-' */
93 if (opt_table[i].type == OPT_NOARG) {
95 return parse_err(errlog, argv[0], o, len,
96 "doesn't allow an argument");
97 problem = opt_table[i].cb(opt_table[i].arg);
100 /* Swallow any short options as optarg, eg -afile */
101 if (*offset && argv[arg][*offset + 1]) {
102 optarg = argv[arg] + *offset + 1;
105 optarg = argv[arg+1];
108 return parse_err(errlog, argv[0], o, len,
109 "requires an argument");
110 problem = opt_table[i].cb_arg(optarg, opt_table[i].arg);
114 parse_err(errlog, argv[0], o, len, problem);
119 /* If no more letters in that short opt, reset offset. */
120 if (*offset && !argv[arg][*offset + 1])
123 /* All finished with that option? */
125 consume_option(argc, argv, arg);
126 if (optarg && optarg == argv[arg])
127 consume_option(argc, argv, arg);