| 219 | } |
| 220 | |
| 221 | int |
| 222 | argparse_parse(struct argparse *self, int argc, const char **argv) |
| 223 | { |
| 224 | self->argc = argc - 1; |
| 225 | self->argv = argv + 1; |
| 226 | self->out = argv; |
| 227 | |
| 228 | argparse_options_check(self->options); |
| 229 | |
| 230 | for (; self->argc; self->argc--, self->argv++) { |
| 231 | const char *arg = self->argv[0]; |
| 232 | if (arg[0] != '-' || !arg[1]) { |
| 233 | if (self->flags & ARGPARSE_STOP_AT_NON_OPTION) { |
| 234 | goto end; |
| 235 | } |
| 236 | // if it's not option or is a single char '-', copy verbatim |
| 237 | self->out[self->cpidx++] = self->argv[0]; |
| 238 | continue; |
| 239 | } |
| 240 | // short option |
| 241 | if (arg[1] != '-') { |
| 242 | self->optvalue = arg + 1; |
| 243 | switch (argparse_short_opt(self, self->options)) { |
| 244 | case -1: |
| 245 | break; |
| 246 | case -2: |
| 247 | goto unknown; |
| 248 | } |
| 249 | while (self->optvalue) { |
| 250 | switch (argparse_short_opt(self, self->options)) { |
| 251 | case -1: |
| 252 | break; |
| 253 | case -2: |
| 254 | goto unknown; |
| 255 | } |
| 256 | } |
| 257 | continue; |
| 258 | } |
| 259 | // if '--' presents |
| 260 | if (!arg[2]) { |
| 261 | self->argc--; |
| 262 | self->argv++; |
| 263 | break; |
| 264 | } |
| 265 | // long option |
| 266 | switch (argparse_long_opt(self, self->options)) { |
| 267 | case -1: |
| 268 | break; |
| 269 | case -2: |
| 270 | goto unknown; |
| 271 | } |
| 272 | continue; |
| 273 | |
| 274 | unknown: |
| 275 | fprintf(stderr, "error: unknown option `%s`\n", self->argv[0]); |
| 276 | argparse_usage(self); |
| 277 | if (!(self->flags & ARGPARSE_IGNORE_UNKNOWN_ARGS)) { |
| 278 | exit(EXIT_FAILURE); |