@@ -20,8 +20,9 @@ int optbug(const struct option *opt, const char *reason)
2020 return error ("BUG: switch '%c' %s" , opt -> short_name , reason );
2121}
2222
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 )
2526{
2627 if (p -> opt ) {
2728 * arg = p -> opt ;
@@ -44,9 +45,10 @@ static void fix_filename(const char *prefix, const char **file)
4445 * file = prefix_filename (prefix , * file );
4546}
4647
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 )
5052{
5153 const struct option * that ;
5254 struct strbuf that_name = STRBUF_INIT ;
@@ -69,16 +71,16 @@ static int opt_command_mode_error(const struct option *opt,
6971 error (_ ("%s is incompatible with %s" ),
7072 optname (opt , flags ), that_name .buf );
7173 strbuf_release (& that_name );
72- return -1 ;
74+ return PARSE_OPT_ERROR ;
7375 }
7476 return error (_ ("%s : incompatible with something else" ),
7577 optname (opt , flags ));
7678}
7779
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 )
8284{
8385 const char * s , * arg ;
8486 const int unset = flags & OPT_UNSET ;
@@ -208,7 +210,8 @@ static int get_value(struct parse_opt_ctx_t *p,
208210 }
209211}
210212
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 )
212215{
213216 const struct option * all_opts = options ;
214217 const struct option * numopt = NULL ;
@@ -239,11 +242,12 @@ static int parse_short_opt(struct parse_opt_ctx_t *p, const struct option *optio
239242 free (arg );
240243 return rc ;
241244 }
242- return -2 ;
245+ return PARSE_OPT_UNKNOWN ;
243246}
244247
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 )
247251{
248252 const struct option * all_opts = options ;
249253 const char * arg_end = strchrnul (arg , '=' );
@@ -269,7 +273,7 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
269273 if (* rest )
270274 continue ;
271275 p -> out [p -> cpidx ++ ] = arg - 2 ;
272- return 0 ;
276+ return PARSE_OPT_DONE ;
273277 }
274278 if (!rest ) {
275279 /* abbreviated? */
@@ -334,11 +338,11 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const char *arg,
334338 ambiguous_option -> long_name ,
335339 (abbrev_flags & OPT_UNSET ) ? "no-" : "" ,
336340 abbrev_option -> long_name );
337- return -3 ;
341+ return PARSE_OPT_HELP ;
338342 }
339343 if (abbrev_option )
340344 return get_value (p , abbrev_option , all_opts , abbrev_flags );
341- return -2 ;
345+ return PARSE_OPT_UNKNOWN ;
342346}
343347
344348static 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,
590594 if (arg [1 ] != '-' ) {
591595 ctx -> opt = arg + 1 ;
592596 switch (parse_short_opt (ctx , options )) {
593- case -1 :
597+ case PARSE_OPT_ERROR :
594598 return PARSE_OPT_ERROR ;
595- case -2 :
599+ case PARSE_OPT_UNKNOWN :
596600 if (ctx -> opt )
597601 check_typos (arg + 1 , options );
598602 if (internal_help && * ctx -> opt == 'h' )
599603 goto show_usage ;
600604 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 ;
601611 }
602612 if (ctx -> opt )
603613 check_typos (arg + 1 , options );
604614 while (ctx -> opt ) {
605615 switch (parse_short_opt (ctx , options )) {
606- case -1 :
616+ case PARSE_OPT_ERROR :
607617 return PARSE_OPT_ERROR ;
608- case -2 :
618+ case PARSE_OPT_UNKNOWN :
609619 if (internal_help && * ctx -> opt == 'h' )
610620 goto show_usage ;
611621
@@ -617,6 +627,12 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
617627 ctx -> argv [0 ] = xstrdup (ctx -> opt - 1 );
618628 * (char * )ctx -> argv [0 ] = '-' ;
619629 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 ;
620636 }
621637 }
622638 continue ;
@@ -635,12 +651,17 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
635651 if (internal_help && !strcmp (arg + 2 , "help" ))
636652 goto show_usage ;
637653 switch (parse_long_opt (ctx , arg + 2 , options )) {
638- case -1 :
654+ case PARSE_OPT_ERROR :
639655 return PARSE_OPT_ERROR ;
640- case -2 :
656+ case PARSE_OPT_UNKNOWN :
641657 goto unknown ;
642- case -3 :
658+ case PARSE_OPT_HELP :
643659 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 ;
644665 }
645666 continue ;
646667unknown :
0 commit comments