5
5
#include "color.h"
6
6
#include "utf8.h"
7
7
8
- static int parse_options_usage (struct parse_opt_ctx_t * ctx ,
9
- const char * const * usagestr ,
10
- const struct option * opts , int err );
11
-
12
8
#define OPT_SHORT 1
13
9
#define OPT_UNSET 2
14
10
@@ -414,7 +410,7 @@ void parse_options_start(struct parse_opt_ctx_t *ctx,
414
410
const struct option * options , int flags )
415
411
{
416
412
memset (ctx , 0 , sizeof (* ctx ));
417
- ctx -> argc = argc - 1 ;
413
+ ctx -> argc = ctx -> total = argc - 1 ;
418
414
ctx -> argv = argv + 1 ;
419
415
ctx -> out = argv ;
420
416
ctx -> prefix = prefix ;
@@ -435,6 +431,7 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
435
431
const char * const usagestr [])
436
432
{
437
433
int internal_help = !(ctx -> flags & PARSE_OPT_NO_INTERNAL_HELP );
434
+ int err = 0 ;
438
435
439
436
/* we must reset ->opt, unknown short option leave it dangling */
440
437
ctx -> opt = NULL ;
@@ -451,27 +448,32 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
451
448
continue ;
452
449
}
453
450
451
+ /* lone -h asks for help */
452
+ if (internal_help && ctx -> total == 1 && !strcmp (arg + 1 , "h" ))
453
+ goto show_usage ;
454
+
454
455
if (arg [1 ] != '-' ) {
455
456
ctx -> opt = arg + 1 ;
456
- if (internal_help && * ctx -> opt == 'h' )
457
- return parse_options_usage (ctx , usagestr , options , 0 );
458
457
switch (parse_short_opt (ctx , options )) {
459
458
case -1 :
460
- return parse_options_usage ( ctx , usagestr , options , 1 ) ;
459
+ goto show_usage_error ;
461
460
case -2 :
462
461
if (ctx -> opt )
463
462
check_typos (arg + 1 , options );
463
+ if (internal_help && * ctx -> opt == 'h' )
464
+ goto show_usage ;
464
465
goto unknown ;
465
466
}
466
467
if (ctx -> opt )
467
468
check_typos (arg + 1 , options );
468
469
while (ctx -> opt ) {
469
- if (internal_help && * ctx -> opt == 'h' )
470
- return parse_options_usage (ctx , usagestr , options , 0 );
471
470
switch (parse_short_opt (ctx , options )) {
472
471
case -1 :
473
- return parse_options_usage ( ctx , usagestr , options , 1 ) ;
472
+ goto show_usage_error ;
474
473
case -2 :
474
+ if (internal_help && * ctx -> opt == 'h' )
475
+ goto show_usage ;
476
+
475
477
/* fake a short option thing to hide the fact that we may have
476
478
* started to parse aggregated stuff
477
479
*
@@ -496,10 +498,10 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
496
498
if (internal_help && !strcmp (arg + 2 , "help-all" ))
497
499
return usage_with_options_internal (ctx , usagestr , options , 1 , 0 );
498
500
if (internal_help && !strcmp (arg + 2 , "help" ))
499
- return parse_options_usage ( ctx , usagestr , options , 0 ) ;
501
+ goto show_usage ;
500
502
switch (parse_long_opt (ctx , arg + 2 , options )) {
501
503
case -1 :
502
- return parse_options_usage ( ctx , usagestr , options , 1 ) ;
504
+ goto show_usage_error ;
503
505
case -2 :
504
506
goto unknown ;
505
507
}
@@ -511,6 +513,11 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
511
513
ctx -> opt = NULL ;
512
514
}
513
515
return PARSE_OPT_DONE ;
516
+
517
+ show_usage_error :
518
+ err = 1 ;
519
+ show_usage :
520
+ return usage_with_options_internal (ctx , usagestr , options , 0 , err );
514
521
}
515
522
516
523
int parse_options_end (struct parse_opt_ctx_t * ctx )
@@ -656,13 +663,6 @@ void NORETURN usage_msg_opt(const char *msg,
656
663
usage_with_options (usagestr , options );
657
664
}
658
665
659
- static int parse_options_usage (struct parse_opt_ctx_t * ctx ,
660
- const char * const * usagestr ,
661
- const struct option * opts , int err )
662
- {
663
- return usage_with_options_internal (ctx , usagestr , opts , 0 , err );
664
- }
665
-
666
666
#undef opterror
667
667
int opterror (const struct option * opt , const char * reason , int flags )
668
668
{
0 commit comments