parse-opt: Export a non NORETURN usage dumper.
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:28 UTC
ee68b87a62a245fca46374fe5132aec58d802baa
2 files changed
+26
-7
parse-options.c
+17
-7
@@ -257,8 +257,8 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
257
return ctx->cpidx + ctx->argc;
258
}
259
260
-static NORETURN void usage_with_options_internal(const char * const *,
261
- const struct option *, int);
260
+static int usage_with_options_internal(const char * const *,
261
+ const struct option *, int, int);
262
263
int parse_options(int argc, const char **argv, const struct option *options,
264
const char * const usagestr[], int flags)
@@ -302,7 +302,7 @@ int parse_options(int argc, const char **argv, const struct option *options,
302
}
303
304
if (!strcmp(arg + 2, "help-all"))
305
- usage_with_options_internal(usagestr, options, 1);
305
+ usage_with_options_internal(usagestr, options, 1, 1);
306
if (!strcmp(arg + 2, "help"))
307
usage_with_options(usagestr, options);
308
if (parse_long_opt(&ctx, arg + 2, options))
@@ -315,8 +315,8 @@ int parse_options(int argc, const char **argv, const struct option *options,
315
#define USAGE_OPTS_WIDTH 24
316
#define USAGE_GAP 2
317
318
-void usage_with_options_internal(const char * const *usagestr,
319
- const struct option *opts, int full)
318
+int usage_with_options_internal(const char * const *usagestr,
319
+ const struct option *opts, int full, int do_exit)
320
{
321
fprintf(stderr, "usage: %s\n", *usagestr++);
322
while (*usagestr && **usagestr)
@@ -401,15 +401,25 @@ void usage_with_options_internal(const char * const *usagestr,
401
}
402
fputc('\n', stderr);
403
404
- exit(129);
404
+ if (do_exit)
405
+ exit(129);
406
+ return PARSE_OPT_HELP;
407
}
408
409
void usage_with_options(const char * const *usagestr,
410
const struct option *opts)
411
{
410
- usage_with_options_internal(usagestr, opts, 0);
412
+ usage_with_options_internal(usagestr, opts, 0, 1);
413
+ exit(129); /* make gcc happy */
414
}
415
416
+int parse_options_usage(const char * const *usagestr,
417
+ const struct option *opts)
418
+{
419
+ return usage_with_options_internal(usagestr, opts, 0, 0);
420
+}
421
+
422
+
423
/*----- some often used options -----*/
424
#include "cache.h"
425
parse-options.h
+9
@@ -113,6 +113,12 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
113
114
/*----- incremantal advanced APIs -----*/
115
116
+enum {
117
+ PARSE_OPT_HELP = -1,
118
+ PARSE_OPT_DONE,
119
+ PARSE_OPT_UNKNOWN,
120
+};
121
+
122
struct parse_opt_ctx_t {
123
const char **argv;
124
const char **out;
@@ -121,6 +127,9 @@ struct parse_opt_ctx_t {
127
int flags;
128
};
129
130
+extern int parse_options_usage(const char * const *usagestr,
131
+ const struct option *opts);
132
+
133
extern void parse_options_start(struct parse_opt_ctx_t *ctx,
134
int argc, const char **argv, int flags);
135