@@ -178,8 +178,7 @@ impl Options {
178
178
hasarg : HasArg ,
179
179
occur : Occur ,
180
180
) -> & mut Options {
181
- validate_names ( short_name, long_name) ;
182
- self . grps . push ( OptGroup {
181
+ self . push_group ( OptGroup {
183
182
short_name : short_name. to_string ( ) ,
184
183
long_name : long_name. to_string ( ) ,
185
184
hint : hint. to_string ( ) ,
@@ -207,8 +206,7 @@ impl Options {
207
206
/// assert!(matches.opt_present("h"));
208
207
/// ```
209
208
pub fn optflag ( & mut self , short_name : & str , long_name : & str , desc : & str ) -> & mut Options {
210
- validate_names ( short_name, long_name) ;
211
- self . grps . push ( OptGroup {
209
+ self . push_group ( OptGroup {
212
210
short_name : short_name. to_string ( ) ,
213
211
long_name : long_name. to_string ( ) ,
214
212
hint : "" . to_string ( ) ,
@@ -237,8 +235,7 @@ impl Options {
237
235
/// assert_eq!(2, matches.opt_count("v"));
238
236
/// ```
239
237
pub fn optflagmulti ( & mut self , short_name : & str , long_name : & str , desc : & str ) -> & mut Options {
240
- validate_names ( short_name, long_name) ;
241
- self . grps . push ( OptGroup {
238
+ self . push_group ( OptGroup {
242
239
short_name : short_name. to_string ( ) ,
243
240
long_name : long_name. to_string ( ) ,
244
241
hint : "" . to_string ( ) ,
@@ -277,8 +274,7 @@ impl Options {
277
274
desc : & str ,
278
275
hint : & str ,
279
276
) -> & mut Options {
280
- validate_names ( short_name, long_name) ;
281
- self . grps . push ( OptGroup {
277
+ self . push_group ( OptGroup {
282
278
short_name : short_name. to_string ( ) ,
283
279
long_name : long_name. to_string ( ) ,
284
280
hint : hint. to_string ( ) ,
@@ -319,8 +315,7 @@ impl Options {
319
315
desc : & str ,
320
316
hint : & str ,
321
317
) -> & mut Options {
322
- validate_names ( short_name, long_name) ;
323
- self . grps . push ( OptGroup {
318
+ self . push_group ( OptGroup {
324
319
short_name : short_name. to_string ( ) ,
325
320
long_name : long_name. to_string ( ) ,
326
321
hint : hint. to_string ( ) ,
@@ -360,8 +355,7 @@ impl Options {
360
355
desc : & str ,
361
356
hint : & str ,
362
357
) -> & mut Options {
363
- validate_names ( short_name, long_name) ;
364
- self . grps . push ( OptGroup {
358
+ self . push_group ( OptGroup {
365
359
short_name : short_name. to_string ( ) ,
366
360
long_name : long_name. to_string ( ) ,
367
361
hint : hint. to_string ( ) ,
@@ -403,8 +397,7 @@ impl Options {
403
397
desc : & str ,
404
398
hint : & str ,
405
399
) -> & mut Options {
406
- validate_names ( short_name, long_name) ;
407
- self . grps . push ( OptGroup {
400
+ self . push_group ( OptGroup {
408
401
short_name : short_name. to_string ( ) ,
409
402
long_name : long_name. to_string ( ) ,
410
403
hint : hint. to_string ( ) ,
@@ -680,6 +673,23 @@ impl Options {
680
673
681
674
Box :: new ( rows)
682
675
}
676
+
677
+ fn push_group ( & mut self , group : OptGroup ) {
678
+ validate_names ( & group. short_name , & group. long_name ) ;
679
+ #[ cfg( debug_assertions) ]
680
+ self . check_conflict ( & group) ;
681
+ self . grps . push ( group) ;
682
+ }
683
+
684
+ #[ cfg( debug_assertions) ]
685
+ fn check_conflict ( & self , group : & OptGroup ) {
686
+ if !group. short_name . is_empty ( ) && self . grps . iter ( ) . any ( |g| g. short_name == group. short_name ) {
687
+ panic ! ( "the short option name -{} caused conflict among multiple options" , group. short_name) ;
688
+ }
689
+ if !group. long_name . is_empty ( ) && self . grps . iter ( ) . any ( |g| g. long_name == group. long_name ) {
690
+ panic ! ( "the long option name --{} caused conflict among multiple options" , group. long_name) ;
691
+ }
692
+ }
683
693
}
684
694
685
695
fn validate_names ( short_name : & str , long_name : & str ) {
0 commit comments