@@ -119,8 +119,8 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
119119 const struct option * options )
120120{
121121 const char * arg_end = strchr (arg , '=' );
122- const struct option * abbrev_option = NULL ;
123- int abbrev_flags = 0 ;
122+ const struct option * abbrev_option = NULL , * ambiguous_option = NULL ;
123+ int abbrev_flags = 0 , ambiguous_flags = 0 ;
124124
125125 if (!arg_end )
126126 arg_end = arg + strlen (arg );
@@ -137,16 +137,16 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
137137 /* abbreviated? */
138138 if (!strncmp (options -> long_name , arg , arg_end - arg )) {
139139is_abbreviated :
140- if (abbrev_option )
141- return error ( "Ambiguous option: %s "
142- "(could be --%s%s or --%s%s)" ,
143- arg ,
144- ( flags & OPT_UNSET ) ?
145- "no-" : "" ,
146- options -> long_name ,
147- ( abbrev_flags & OPT_UNSET ) ?
148- "no-" : "" ,
149- abbrev_option -> long_name );
140+ if (abbrev_option ) {
141+ /*
142+ * If this is abbreviated, it is
143+ * ambiguous. So when there is no
144+ * exact match later, we need to
145+ * error out.
146+ */
147+ ambiguous_option = abbrev_option ;
148+ ambiguous_flags = abbrev_flags ;
149+ }
150150 if (!(flags & OPT_UNSET ) && * arg_end )
151151 p -> opt = arg_end + 1 ;
152152 abbrev_option = options ;
@@ -176,6 +176,15 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
176176 }
177177 return get_value (p , options , flags );
178178 }
179+
180+ if (ambiguous_option )
181+ return error ("Ambiguous option: %s "
182+ "(could be --%s%s or --%s%s)" ,
183+ arg ,
184+ (ambiguous_flags & OPT_UNSET ) ? "no-" : "" ,
185+ ambiguous_option -> long_name ,
186+ (abbrev_flags & OPT_UNSET ) ? "no-" : "" ,
187+ abbrev_option -> long_name );
179188 if (abbrev_option )
180189 return get_value (p , abbrev_option , abbrev_flags );
181190 return error ("unknown option `%s'" , arg );
0 commit comments