@@ -20,8 +20,9 @@ int optbug(const struct option *opt, const char *reason)
20
20
return error ("BUG: switch '%c' %s" , opt -> short_name , reason );
21
21
}
22
22
23
- static int get_arg (struct parse_opt_ctx_t * p , const struct option * opt ,
24
- int flags , const char * * arg )
23
+ static enum parse_opt_result get_arg (struct parse_opt_ctx_t * p ,
24
+ const struct option * opt ,
25
+ int flags , const char * * arg )
25
26
{
26
27
if (p -> opt ) {
27
28
* arg = p -> opt ;
@@ -44,9 +45,10 @@ static void fix_filename(const char *prefix, const char **file)
44
45
* file = prefix_filename (prefix , * file );
45
46
}
46
47
47
- static int opt_command_mode_error (const struct option * opt ,
48
- const struct option * all_opts ,
49
- int flags )
48
+ static enum parse_opt_result opt_command_mode_error (
49
+ const struct option * opt ,
50
+ const struct option * all_opts ,
51
+ int flags )
50
52
{
51
53
const struct option * that ;
52
54
struct strbuf that_name = STRBUF_INIT ;
@@ -69,16 +71,16 @@ static int opt_command_mode_error(const struct option *opt,
69
71
error (_ ("%s is incompatible with %s" ),
70
72
optname (opt , flags ), that_name .buf );
71
73
strbuf_release (& that_name );
72
- return -1 ;
74
+ return PARSE_OPT_ERROR ;
73
75
}
74
76
return error (_ ("%s : incompatible with something else" ),
75
77
optname (opt , flags ));
76
78
}
77
79
78
- static int get_value (struct parse_opt_ctx_t * p ,
79
- const struct option * opt ,
80
- const struct option * all_opts ,
81
- int flags )
80
+ static enum parse_opt_result get_value (struct parse_opt_ctx_t * p ,
81
+ const struct option * opt ,
82
+ const struct option * all_opts ,
83
+ int flags )
82
84
{
83
85
const char * s , * arg ;
84
86
const int unset = flags & OPT_UNSET ;
@@ -208,7 +210,8 @@ static int get_value(struct parse_opt_ctx_t *p,
208
210
}
209
211
}
210
212
211
- static int parse_short_opt (struct parse_opt_ctx_t * p , const struct option * options )
213
+ static enum parse_opt_result parse_short_opt (struct parse_opt_ctx_t * p ,
214
+ const struct option * options )
212
215
{
213
216
const struct option * all_opts = options ;
214
217
const struct option * numopt = NULL ;
@@ -239,11 +242,12 @@ static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *optio
239
242
free (arg );
240
243
return rc ;
241
244
}
242
- return -2 ;
245
+ return PARSE_OPT_UNKNOWN ;
243
246
}
244
247
245
- static int parse_long_opt (struct parse_opt_ctx_t * p , const char * arg ,
246
- const struct option * options )
248
+ static enum parse_opt_result parse_long_opt (
249
+ struct parse_opt_ctx_t * p , const char * arg ,
250
+ const struct option * options )
247
251
{
248
252
const struct option * all_opts = options ;
249
253
const char * arg_end = strchrnul (arg , '=' );
@@ -269,7 +273,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
269
273
if (* rest )
270
274
continue ;
271
275
p -> out [p -> cpidx ++ ] = arg - 2 ;
272
- return 0 ;
276
+ return PARSE_OPT_DONE ;
273
277
}
274
278
if (!rest ) {
275
279
/* abbreviated? */
@@ -334,11 +338,11 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
334
338
ambiguous_option -> long_name ,
335
339
(abbrev_flags & OPT_UNSET ) ? "no-" : "" ,
336
340
abbrev_option -> long_name );
337
- return -3 ;
341
+ return PARSE_OPT_HELP ;
338
342
}
339
343
if (abbrev_option )
340
344
return get_value (p , abbrev_option , all_opts , abbrev_flags );
341
- return -2 ;
345
+ return PARSE_OPT_UNKNOWN ;
342
346
}
343
347
344
348
static int parse_nodash_opt (struct parse_opt_ctx_t * p , const char * arg ,
@@ -590,22 +594,28 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
590
594
if (arg [1 ] != '-' ) {
591
595
ctx -> opt = arg + 1 ;
592
596
switch (parse_short_opt (ctx , options )) {
593
- case -1 :
597
+ case PARSE_OPT_ERROR :
594
598
return PARSE_OPT_ERROR ;
595
- case -2 :
599
+ case PARSE_OPT_UNKNOWN :
596
600
if (ctx -> opt )
597
601
check_typos (arg + 1 , options );
598
602
if (internal_help && * ctx -> opt == 'h' )
599
603
goto show_usage ;
600
604
goto unknown ;
605
+ case PARSE_OPT_NON_OPTION :
606
+ case PARSE_OPT_HELP :
607
+ case PARSE_OPT_COMPLETE :
608
+ BUG ("parse_short_opt() cannot return these" );
609
+ case PARSE_OPT_DONE :
610
+ break ;
601
611
}
602
612
if (ctx -> opt )
603
613
check_typos (arg + 1 , options );
604
614
while (ctx -> opt ) {
605
615
switch (parse_short_opt (ctx , options )) {
606
- case -1 :
616
+ case PARSE_OPT_ERROR :
607
617
return PARSE_OPT_ERROR ;
608
- case -2 :
618
+ case PARSE_OPT_UNKNOWN :
609
619
if (internal_help && * ctx -> opt == 'h' )
610
620
goto show_usage ;
611
621
@@ -617,6 +627,12 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
617
627
ctx -> argv [0 ] = xstrdup (ctx -> opt - 1 );
618
628
* (char * )ctx -> argv [0 ] = '-' ;
619
629
goto unknown ;
630
+ case PARSE_OPT_NON_OPTION :
631
+ case PARSE_OPT_COMPLETE :
632
+ case PARSE_OPT_HELP :
633
+ BUG ("parse_short_opt() cannot return these" );
634
+ case PARSE_OPT_DONE :
635
+ break ;
620
636
}
621
637
}
622
638
continue ;
@@ -635,12 +651,17 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
635
651
if (internal_help && !strcmp (arg + 2 , "help" ))
636
652
goto show_usage ;
637
653
switch (parse_long_opt (ctx , arg + 2 , options )) {
638
- case -1 :
654
+ case PARSE_OPT_ERROR :
639
655
return PARSE_OPT_ERROR ;
640
- case -2 :
656
+ case PARSE_OPT_UNKNOWN :
641
657
goto unknown ;
642
- case -3 :
658
+ case PARSE_OPT_HELP :
643
659
goto show_usage ;
660
+ case PARSE_OPT_NON_OPTION :
661
+ case PARSE_OPT_COMPLETE :
662
+ BUG ("parse_long_opt() cannot return these" );
663
+ case PARSE_OPT_DONE :
664
+ break ;
644
665
}
645
666
continue ;
646
667
unknown :
0 commit comments