44#include "commit.h"
55#include "color.h"
66
7- static int parse_options_usage (const char * const * usagestr ,
7+ static int parse_options_usage (struct parse_opt_ctx_t * ctx ,
8+ const char * const * usagestr ,
89 const struct option * opts , int err );
910
1011#define OPT_SHORT 1
@@ -351,7 +352,8 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
351352 die ("STOP_AT_NON_OPTION and KEEP_UNKNOWN don't go together" );
352353}
353354
354- static int usage_with_options_internal (const char * const * ,
355+ static int usage_with_options_internal (struct parse_opt_ctx_t * ,
356+ const char * const * ,
355357 const struct option * , int , int );
356358
357359int parse_options_step (struct parse_opt_ctx_t * ctx ,
@@ -380,21 +382,21 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
380382 if (arg [1 ] != '-' ) {
381383 ctx -> opt = arg + 1 ;
382384 if (internal_help && * ctx -> opt == 'h' )
383- return parse_options_usage (usagestr , options , 0 );
385+ return parse_options_usage (ctx , usagestr , options , 0 );
384386 switch (parse_short_opt (ctx , options )) {
385387 case -1 :
386- return parse_options_usage (usagestr , options , 1 );
388+ return parse_options_usage (ctx , usagestr , options , 1 );
387389 case -2 :
388390 goto unknown ;
389391 }
390392 if (ctx -> opt )
391393 check_typos (arg + 1 , options );
392394 while (ctx -> opt ) {
393395 if (internal_help && * ctx -> opt == 'h' )
394- return parse_options_usage (usagestr , options , 0 );
396+ return parse_options_usage (ctx , usagestr , options , 0 );
395397 switch (parse_short_opt (ctx , options )) {
396398 case -1 :
397- return parse_options_usage (usagestr , options , 1 );
399+ return parse_options_usage (ctx , usagestr , options , 1 );
398400 case -2 :
399401 /* fake a short option thing to hide the fact that we may have
400402 * started to parse aggregated stuff
@@ -418,12 +420,12 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
418420 }
419421
420422 if (internal_help && !strcmp (arg + 2 , "help-all" ))
421- return usage_with_options_internal (usagestr , options , 1 , 0 );
423+ return usage_with_options_internal (ctx , usagestr , options , 1 , 0 );
422424 if (internal_help && !strcmp (arg + 2 , "help" ))
423- return parse_options_usage (usagestr , options , 0 );
425+ return parse_options_usage (ctx , usagestr , options , 0 );
424426 switch (parse_long_opt (ctx , arg + 2 , options )) {
425427 case -1 :
426- return parse_options_usage (usagestr , options , 1 );
428+ return parse_options_usage (ctx , usagestr , options , 1 );
427429 case -2 :
428430 goto unknown ;
429431 }
@@ -485,14 +487,18 @@ static int usage_argh(const struct option *opts, FILE *outfile)
485487#define USAGE_OPTS_WIDTH 24
486488#define USAGE_GAP 2
487489
488- static int usage_with_options_internal (const char * const * usagestr ,
489- const struct option * opts , int full , int err )
490+ static int usage_with_options_internal (struct parse_opt_ctx_t * ctx ,
491+ const char * const * usagestr ,
492+ const struct option * opts , int full , int err )
490493{
491494 FILE * outfile = err ? stderr : stdout ;
492495
493496 if (!usagestr )
494497 return PARSE_OPT_HELP ;
495498
499+ if (!err && ctx && ctx -> flags & PARSE_OPT_SHELL_EVAL )
500+ fprintf (outfile , "cat <<\\EOF\n" );
501+
496502 fprintf (outfile , "usage: %s\n" , * usagestr ++ );
497503 while (* usagestr && * * usagestr )
498504 fprintf (outfile , " or: %s\n" , * usagestr ++ );
@@ -548,13 +554,16 @@ static int usage_with_options_internal(const char * const *usagestr,
548554 }
549555 fputc ('\n' , outfile );
550556
557+ if (!err && ctx && ctx -> flags & PARSE_OPT_SHELL_EVAL )
558+ fputs ("EOF\n" , outfile );
559+
551560 return PARSE_OPT_HELP ;
552561}
553562
554563void usage_with_options (const char * const * usagestr ,
555564 const struct option * opts )
556565{
557- usage_with_options_internal (usagestr , opts , 0 , 1 );
566+ usage_with_options_internal (NULL , usagestr , opts , 0 , 1 );
558567 exit (129 );
559568}
560569
@@ -566,10 +575,11 @@ void usage_msg_opt(const char *msg,
566575 usage_with_options (usagestr , options );
567576}
568577
569- static int parse_options_usage (const char * const * usagestr ,
578+ static int parse_options_usage (struct parse_opt_ctx_t * ctx ,
579+ const char * const * usagestr ,
570580 const struct option * opts , int err )
571581{
572- return usage_with_options_internal (usagestr , opts , 0 , err );
582+ return usage_with_options_internal (ctx , usagestr , opts , 0 , err );
573583}
574584
575585
0 commit comments