8
8
9
9
static int disallow_abbreviated_options ;
10
10
11
- #define OPT_SHORT 1
12
- #define OPT_UNSET 2
11
+ enum opt_parsed {
12
+ OPT_LONG = 0 ,
13
+ OPT_SHORT = 1 <<0 ,
14
+ OPT_UNSET = 1 <<1 ,
15
+ };
13
16
14
- int optbug (const struct option * opt , const char * reason )
17
+ static int optbug (const struct option * opt , const char * reason )
15
18
{
16
19
if (opt -> long_name ) {
17
20
if (opt -> short_name )
@@ -22,9 +25,26 @@ int optbug(const struct option *opt, const char *reason)
22
25
return error ("BUG: switch '%c' %s" , opt -> short_name , reason );
23
26
}
24
27
28
+ static const char * optname (const struct option * opt , enum opt_parsed flags )
29
+ {
30
+ static struct strbuf sb = STRBUF_INIT ;
31
+
32
+ strbuf_reset (& sb );
33
+ if (flags & OPT_SHORT )
34
+ strbuf_addf (& sb , "switch `%c'" , opt -> short_name );
35
+ else if (flags & OPT_UNSET )
36
+ strbuf_addf (& sb , "option `no-%s'" , opt -> long_name );
37
+ else if (flags == OPT_LONG )
38
+ strbuf_addf (& sb , "option `%s'" , opt -> long_name );
39
+ else
40
+ BUG ("optname() got unknown flags %d" , flags );
41
+
42
+ return sb .buf ;
43
+ }
44
+
25
45
static enum parse_opt_result get_arg (struct parse_opt_ctx_t * p ,
26
46
const struct option * opt ,
27
- int flags , const char * * arg )
47
+ enum opt_parsed flags , const char * * arg )
28
48
{
29
49
if (p -> opt ) {
30
50
* arg = p -> opt ;
@@ -50,7 +70,7 @@ static void fix_filename(const char *prefix, const char **file)
50
70
static enum parse_opt_result opt_command_mode_error (
51
71
const struct option * opt ,
52
72
const struct option * all_opts ,
53
- int flags )
73
+ enum opt_parsed flags )
54
74
{
55
75
const struct option * that ;
56
76
struct strbuf that_name = STRBUF_INIT ;
@@ -82,7 +102,7 @@ static enum parse_opt_result opt_command_mode_error(
82
102
static enum parse_opt_result get_value (struct parse_opt_ctx_t * p ,
83
103
const struct option * opt ,
84
104
const struct option * all_opts ,
85
- int flags )
105
+ enum opt_parsed flags )
86
106
{
87
107
const char * s , * arg ;
88
108
const int unset = flags & OPT_UNSET ;
@@ -298,11 +318,11 @@ static enum parse_opt_result parse_long_opt(
298
318
const struct option * all_opts = options ;
299
319
const char * arg_end = strchrnul (arg , '=' );
300
320
const struct option * abbrev_option = NULL , * ambiguous_option = NULL ;
301
- int abbrev_flags = 0 , ambiguous_flags = 0 ;
321
+ enum opt_parsed abbrev_flags = OPT_LONG , ambiguous_flags = OPT_LONG ;
302
322
303
323
for (; options -> type != OPTION_END ; options ++ ) {
304
324
const char * rest , * long_name = options -> long_name ;
305
- int flags = 0 , opt_flags = 0 ;
325
+ enum opt_parsed flags = OPT_LONG , opt_flags = OPT_LONG ;
306
326
307
327
if (!long_name )
308
328
continue ;
@@ -481,7 +501,8 @@ static void parse_options_check(const struct option *opts)
481
501
482
502
static void parse_options_start_1 (struct parse_opt_ctx_t * ctx ,
483
503
int argc , const char * * argv , const char * prefix ,
484
- const struct option * options , int flags )
504
+ const struct option * options ,
505
+ enum parse_opt_flags flags )
485
506
{
486
507
ctx -> argc = argc ;
487
508
ctx -> argv = argv ;
@@ -506,7 +527,8 @@ static void parse_options_start_1(struct parse_opt_ctx_t *ctx,
506
527
507
528
void parse_options_start (struct parse_opt_ctx_t * ctx ,
508
529
int argc , const char * * argv , const char * prefix ,
509
- const struct option * options , int flags )
530
+ const struct option * options ,
531
+ enum parse_opt_flags flags )
510
532
{
511
533
memset (ctx , 0 , sizeof (* ctx ));
512
534
parse_options_start_1 (ctx , argc , argv , prefix , options , flags );
@@ -697,13 +719,14 @@ static void free_preprocessed_options(struct option *options)
697
719
free (options );
698
720
}
699
721
700
- static int usage_with_options_internal (struct parse_opt_ctx_t * ,
701
- const char * const * ,
702
- const struct option * , int , int );
722
+ static enum parse_opt_result usage_with_options_internal (struct parse_opt_ctx_t * ,
723
+ const char * const * ,
724
+ const struct option * ,
725
+ int , int );
703
726
704
- int parse_options_step (struct parse_opt_ctx_t * ctx ,
705
- const struct option * options ,
706
- const char * const usagestr [])
727
+ enum parse_opt_result parse_options_step (struct parse_opt_ctx_t * ctx ,
728
+ const struct option * options ,
729
+ const char * const usagestr [])
707
730
{
708
731
int internal_help = !(ctx -> flags & PARSE_OPT_NO_INTERNAL_HELP );
709
732
@@ -837,9 +860,11 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
837
860
return ctx -> cpidx + ctx -> argc ;
838
861
}
839
862
840
- int parse_options (int argc , const char * * argv , const char * prefix ,
841
- const struct option * options , const char * const usagestr [],
842
- int flags )
863
+ enum parse_opt_result parse_options (int argc , const char * * argv ,
864
+ const char * prefix ,
865
+ const struct option * options ,
866
+ const char * const usagestr [],
867
+ enum parse_opt_flags flags )
843
868
{
844
869
struct parse_opt_ctx_t ctx ;
845
870
struct option * real_options ;
@@ -861,7 +886,7 @@ int parse_options(int argc, const char **argv, const char *prefix,
861
886
case PARSE_OPT_NON_OPTION :
862
887
case PARSE_OPT_DONE :
863
888
break ;
864
- default : /* PARSE_OPT_UNKNOWN */
889
+ case PARSE_OPT_UNKNOWN :
865
890
if (ctx .argv [0 ][1 ] == '-' ) {
866
891
error (_ ("unknown option `%s'" ), ctx .argv [0 ] + 2 );
867
892
} else if (isascii (* ctx .opt )) {
@@ -897,9 +922,10 @@ static int usage_argh(const struct option *opts, FILE *outfile)
897
922
#define USAGE_OPTS_WIDTH 24
898
923
#define USAGE_GAP 2
899
924
900
- static int usage_with_options_internal (struct parse_opt_ctx_t * ctx ,
901
- const char * const * usagestr ,
902
- const struct option * opts , int full , int err )
925
+ static enum parse_opt_result usage_with_options_internal (struct parse_opt_ctx_t * ctx ,
926
+ const char * const * usagestr ,
927
+ const struct option * opts ,
928
+ int full , int err )
903
929
{
904
930
FILE * outfile = err ? stderr : stdout ;
905
931
int need_newline ;
@@ -1052,18 +1078,3 @@ void NORETURN usage_msg_opt(const char *msg,
1052
1078
fprintf (stderr , "fatal: %s\n\n" , msg );
1053
1079
usage_with_options (usagestr , options );
1054
1080
}
1055
-
1056
- const char * optname (const struct option * opt , int flags )
1057
- {
1058
- static struct strbuf sb = STRBUF_INIT ;
1059
-
1060
- strbuf_reset (& sb );
1061
- if (flags & OPT_SHORT )
1062
- strbuf_addf (& sb , "switch `%c'" , opt -> short_name );
1063
- else if (flags & OPT_UNSET )
1064
- strbuf_addf (& sb , "option `no-%s'" , opt -> long_name );
1065
- else
1066
- strbuf_addf (& sb , "option `%s'" , opt -> long_name );
1067
-
1068
- return sb .buf ;
1069
- }
0 commit comments