@@ -2990,9 +2990,50 @@ static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *va
29902990
29912991static int diff_scoreopt_parse (const char * opt );
29922992
2993+ static inline int short_opt (char opt , const char * * argv ,
2994+ const char * * optarg )
2995+ {
2996+ const char * arg = argv [0 ];
2997+ if (arg [0 ] != '-' || arg [1 ] != opt )
2998+ return 0 ;
2999+ if (arg [2 ] != '\0' ) {
3000+ * optarg = arg + 2 ;
3001+ return 1 ;
3002+ }
3003+ if (!argv [1 ])
3004+ die ("Option '%c' requires a value" , opt );
3005+ * optarg = argv [1 ];
3006+ return 2 ;
3007+ }
3008+
3009+ int parse_long_opt (const char * opt , const char * * argv ,
3010+ const char * * optarg )
3011+ {
3012+ const char * arg = argv [0 ];
3013+ if (arg [0 ] != '-' || arg [1 ] != '-' )
3014+ return 0 ;
3015+ arg += strlen ("--" );
3016+ if (prefixcmp (arg , opt ))
3017+ return 0 ;
3018+ arg += strlen (opt );
3019+ if (* arg == '=' ) { /* sticked form: --option=value */
3020+ * optarg = arg + 1 ;
3021+ return 1 ;
3022+ }
3023+ if (* arg != '\0' )
3024+ return 0 ;
3025+ /* separate form: --option value */
3026+ if (!argv [1 ])
3027+ die ("Option '--%s' requires a value" , opt );
3028+ * optarg = argv [1 ];
3029+ return 2 ;
3030+ }
3031+
29933032int diff_opt_parse (struct diff_options * options , const char * * av , int ac )
29943033{
29953034 const char * arg = av [0 ];
3035+ const char * optarg ;
3036+ int argcount ;
29963037
29973038 /* Output format options */
29983039 if (!strcmp (arg , "-p" ) || !strcmp (arg , "-u" ) || !strcmp (arg , "--patch" ))
@@ -3149,10 +3190,11 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
31493190 else
31503191 die ("bad --word-diff argument: %s" , type );
31513192 }
3152- else if (! prefixcmp ( arg , "-- word-diff-regex=" )) {
3193+ else if (( argcount = parse_long_opt ( " word-diff-regex" , av , & optarg ) )) {
31533194 if (options -> word_diff == DIFF_WORDS_NONE )
31543195 options -> word_diff = DIFF_WORDS_PLAIN ;
3155- options -> word_regex = arg + 18 ;
3196+ options -> word_regex = optarg ;
3197+ return argcount ;
31563198 }
31573199 else if (!strcmp (arg , "--exit-code" ))
31583200 DIFF_OPT_SET (options , EXIT_WITH_STATUS );
@@ -3180,18 +3222,26 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
31803222 /* misc options */
31813223 else if (!strcmp (arg , "-z" ))
31823224 options -> line_termination = 0 ;
3183- else if (!prefixcmp (arg , "-l" ))
3184- options -> rename_limit = strtoul (arg + 2 , NULL , 10 );
3185- else if (!prefixcmp (arg , "-S" ))
3186- options -> pickaxe = arg + 2 ;
3225+ else if ((argcount = short_opt ('l' , av , & optarg ))) {
3226+ options -> rename_limit = strtoul (optarg , NULL , 10 );
3227+ return argcount ;
3228+ }
3229+ else if ((argcount = short_opt ('S' , av , & optarg ))) {
3230+ options -> pickaxe = optarg ;
3231+ return argcount ;
3232+ }
31873233 else if (!strcmp (arg , "--pickaxe-all" ))
31883234 options -> pickaxe_opts = DIFF_PICKAXE_ALL ;
31893235 else if (!strcmp (arg , "--pickaxe-regex" ))
31903236 options -> pickaxe_opts = DIFF_PICKAXE_REGEX ;
3191- else if (!prefixcmp (arg , "-O" ))
3192- options -> orderfile = arg + 2 ;
3193- else if (!prefixcmp (arg , "--diff-filter=" ))
3194- options -> filter = arg + 14 ;
3237+ else if ((argcount = short_opt ('O' , av , & optarg ))) {
3238+ options -> orderfile = optarg ;
3239+ return argcount ;
3240+ }
3241+ else if ((argcount = parse_long_opt ("diff-filter" , av , & optarg ))) {
3242+ options -> filter = optarg ;
3243+ return argcount ;
3244+ }
31953245 else if (!strcmp (arg , "--abbrev" ))
31963246 options -> abbrev = DEFAULT_ABBREV ;
31973247 else if (!prefixcmp (arg , "--abbrev=" )) {
@@ -3201,20 +3251,25 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
32013251 else if (40 < options -> abbrev )
32023252 options -> abbrev = 40 ;
32033253 }
3204- else if (!prefixcmp (arg , "--src-prefix=" ))
3205- options -> a_prefix = arg + 13 ;
3206- else if (!prefixcmp (arg , "--dst-prefix=" ))
3207- options -> b_prefix = arg + 13 ;
3254+ else if ((argcount = parse_long_opt ("src-prefix" , av , & optarg ))) {
3255+ options -> a_prefix = optarg ;
3256+ return argcount ;
3257+ }
3258+ else if ((argcount = parse_long_opt ("dst-prefix" , av , & optarg ))) {
3259+ options -> b_prefix = optarg ;
3260+ return argcount ;
3261+ }
32083262 else if (!strcmp (arg , "--no-prefix" ))
32093263 options -> a_prefix = options -> b_prefix = "" ;
32103264 else if (opt_arg (arg , '\0' , "inter-hunk-context" ,
32113265 & options -> interhunkcontext ))
32123266 ;
3213- else if (! prefixcmp ( arg , "-- output=" )) {
3214- options -> file = fopen (arg + strlen ( "--output=" ) , "w" );
3267+ else if (( argcount = parse_long_opt ( " output" , av , & optarg ) )) {
3268+ options -> file = fopen (optarg , "w" );
32153269 if (!options -> file )
32163270 die_errno ("Could not open '%s'" , arg + strlen ("--output=" ));
32173271 options -> close_file = 1 ;
3272+ return argcount ;
32183273 } else
32193274 return 0 ;
32203275 return 1 ;
0 commit comments