Skip to content

Commit ee68b87

Browse files
MadCodergitster
authored andcommitted
parse-opt: Export a non NORETURN usage dumper.
Signed-off-by: Pierre Habouzit <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7e7bbcb commit ee68b87

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

parse-options.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
257257
return ctx->cpidx + ctx->argc;
258258
}
259259

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);
262262

263263
int parse_options(int argc, const char **argv, const struct option *options,
264264
const char * const usagestr[], int flags)
@@ -302,7 +302,7 @@ int parse_options(int argc, const char **argv, const struct option *options,
302302
}
303303

304304
if (!strcmp(arg + 2, "help-all"))
305-
usage_with_options_internal(usagestr, options, 1);
305+
usage_with_options_internal(usagestr, options, 1, 1);
306306
if (!strcmp(arg + 2, "help"))
307307
usage_with_options(usagestr, options);
308308
if (parse_long_opt(&ctx, arg + 2, options))
@@ -315,8 +315,8 @@ int parse_options(int argc, const char **argv, const struct option *options,
315315
#define USAGE_OPTS_WIDTH 24
316316
#define USAGE_GAP 2
317317

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)
320320
{
321321
fprintf(stderr, "usage: %s\n", *usagestr++);
322322
while (*usagestr && **usagestr)
@@ -401,15 +401,25 @@ void usage_with_options_internal(const char * const *usagestr,
401401
}
402402
fputc('\n', stderr);
403403

404-
exit(129);
404+
if (do_exit)
405+
exit(129);
406+
return PARSE_OPT_HELP;
405407
}
406408

407409
void usage_with_options(const char * const *usagestr,
408410
const struct option *opts)
409411
{
410-
usage_with_options_internal(usagestr, opts, 0);
412+
usage_with_options_internal(usagestr, opts, 0, 1);
413+
exit(129); /* make gcc happy */
411414
}
412415

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+
413423
/*----- some often used options -----*/
414424
#include "cache.h"
415425

parse-options.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
113113

114114
/*----- incremantal advanced APIs -----*/
115115

116+
enum {
117+
PARSE_OPT_HELP = -1,
118+
PARSE_OPT_DONE,
119+
PARSE_OPT_UNKNOWN,
120+
};
121+
116122
struct parse_opt_ctx_t {
117123
const char **argv;
118124
const char **out;
@@ -121,6 +127,9 @@ struct parse_opt_ctx_t {
121127
int flags;
122128
};
123129

130+
extern int parse_options_usage(const char * const *usagestr,
131+
const struct option *opts);
132+
124133
extern void parse_options_start(struct parse_opt_ctx_t *ctx,
125134
int argc, const char **argv, int flags);
126135

0 commit comments

Comments
 (0)