@matheusbsbs / matheus-bagliano / commits / ff43ec3e2d
Fork 0

parse-opt: create parse_options_step.

For now it's unable to stop at unknown options, this commit merely reorganize some code around. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Jun 23, 2008 at 22:38 UTC ff43ec3e2d2f71482fe17ba9ba5f4e8074cc54ee
2 files changed +56 -39
parse-options.c
+52 -39
@@ -250,63 +250,78 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
250 ctx->flags = flags;
251 }
252
253 -int parse_options_end(struct parse_opt_ctx_t *ctx)
254 -{
255 - memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
256 - ctx->out[ctx->cpidx + ctx->argc] = NULL;
257 - return ctx->cpidx + ctx->argc;
258 -}
259 -
253 static int usage_with_options_internal(const char * const *,
261 - const struct option *, int, int);
254 + const struct option *, int);
255
263 -int parse_options(int argc, const char **argv, const struct option *options,
264 - const char * const usagestr[], int flags)
256 +int parse_options_step(struct parse_opt_ctx_t *ctx,
257 + const struct option *options,
258 + const char * const usagestr[])
259 {
266 - struct parse_opt_ctx_t ctx;
267 -
268 - parse_options_start(&ctx, argc, argv, flags);
269 - for (; ctx.argc; ctx.argc--, ctx.argv++) {
270 - const char *arg = ctx.argv[0];
260 + for (; ctx->argc; ctx->argc--, ctx->argv++) {
261 + const char *arg = ctx->argv[0];
262
263 if (*arg != '-' || !arg[1]) {
273 - if (ctx.flags & PARSE_OPT_STOP_AT_NON_OPTION)
264 + if (ctx->flags & PARSE_OPT_STOP_AT_NON_OPTION)
265 break;
275 - ctx.out[ctx.cpidx++] = ctx.argv[0];
266 + ctx->out[ctx->cpidx++] = ctx->argv[0];
267 continue;
268 }
269
270 if (arg[1] != '-') {
280 - ctx.opt = arg + 1;
281 - if (*ctx.opt == 'h')
282 - usage_with_options(usagestr, options);
283 - if (parse_short_opt(&ctx, options) < 0)
271 + ctx->opt = arg + 1;
272 + if (*ctx->opt == 'h')
273 + return parse_options_usage(usagestr, options);
274 + if (parse_short_opt(ctx, options) < 0)
275 usage_with_options(usagestr, options);
285 - if (ctx.opt)
276 + if (ctx->opt)
277 check_typos(arg + 1, options);
287 - while (ctx.opt) {
288 - if (*ctx.opt == 'h')
289 - usage_with_options(usagestr, options);
290 - if (parse_short_opt(&ctx, options) < 0)
278 + while (ctx->opt) {
279 + if (*ctx->opt == 'h')
280 + return parse_options_usage(usagestr, options);
281 + if (parse_short_opt(ctx, options) < 0)
282 usage_with_options(usagestr, options);
283 }
284 continue;
285 }
286
287 if (!arg[2]) { /* "--" */
297 - if (!(ctx.flags & PARSE_OPT_KEEP_DASHDASH)) {
298 - ctx.argc--;
299 - ctx.argv++;
288 + if (!(ctx->flags & PARSE_OPT_KEEP_DASHDASH)) {
289 + ctx->argc--;
290 + ctx->argv++;
291 }
292 break;
293 }
294
295 if (!strcmp(arg + 2, "help-all"))
305 - usage_with_options_internal(usagestr, options, 1, 1);
296 + return usage_with_options_internal(usagestr, options, 1);
297 if (!strcmp(arg + 2, "help"))
298 + return parse_options_usage(usagestr, options);
299 + if (parse_long_opt(ctx, arg + 2, options))
300 usage_with_options(usagestr, options);
308 - if (parse_long_opt(&ctx, arg + 2, options))
309 - usage_with_options(usagestr, options);
301 + }
302 + return PARSE_OPT_DONE;
303 +}
304 +
305 +int parse_options_end(struct parse_opt_ctx_t *ctx)
306 +{
307 + memmove(ctx->out + ctx->cpidx, ctx->argv, ctx->argc * sizeof(*ctx->out));
308 + ctx->out[ctx->cpidx + ctx->argc] = NULL;
309 + return ctx->cpidx + ctx->argc;
310 +}
311 +
312 +int parse_options(int argc, const char **argv, const struct option *options,
313 + const char * const usagestr[], int flags)
314 +{
315 + struct parse_opt_ctx_t ctx;
316 +
317 + parse_options_start(&ctx, argc, argv, flags);
318 + switch (parse_options_step(&ctx, options, usagestr)) {
319 + case PARSE_OPT_HELP:
320 + exit(129);
321 + case PARSE_OPT_DONE:
322 + break;
323 + default: /* PARSE_OPT_UNKNOWN */
324 + abort(); /* unreached yet */
325 }
326
327 return parse_options_end(&ctx);
@@ -316,7 +331,7 @@ int parse_options(int argc, const char **argv, const struct option *options,
331 #define USAGE_GAP 2
332
333 int usage_with_options_internal(const char * const *usagestr,
319 - const struct option *opts, int full, int do_exit)
334 + const struct option *opts, int full)
335 {
336 fprintf(stderr, "usage: %s\n", *usagestr++);
337 while (*usagestr && **usagestr)
@@ -401,22 +416,20 @@ int usage_with_options_internal(const char * const *usagestr,
416 }
417 fputc('\n', stderr);
418
404 - if (do_exit)
405 - exit(129);
419 return PARSE_OPT_HELP;
420 }
421
422 void usage_with_options(const char * const *usagestr,
410 - const struct option *opts)
423 + const struct option *opts)
424 {
412 - usage_with_options_internal(usagestr, opts, 0, 1);
413 - exit(129); /* make gcc happy */
425 + usage_with_options_internal(usagestr, opts, 0);
426 + exit(129);
427 }
428
429 int parse_options_usage(const char * const *usagestr,
430 const struct option *opts)
431 {
419 - return usage_with_options_internal(usagestr, opts, 0, 0);
432 + return usage_with_options_internal(usagestr, opts, 0);
433 }
434
435
parse-options.h
+4
@@ -133,6 +133,10 @@ extern int parse_options_usage(const char * const *usagestr,
133 extern void parse_options_start(struct parse_opt_ctx_t *ctx,
134 int argc, const char **argv, int flags);
135
136 +extern int parse_options_step(struct parse_opt_ctx_t *ctx,
137 + const struct option *options,
138 + const char * const usagestr[]);
139 +
140 extern int parse_options_end(struct parse_opt_ctx_t *ctx);
141
142