@@ -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,21 +673,39 @@ impl Options {
680
673
681
674
Box :: new ( rows)
682
675
}
683
- }
684
676
685
- fn validate_names ( short_name : & str , long_name : & str ) {
686
- let len = short_name. len ( ) ;
687
- assert ! (
688
- len == 1 || len == 0 ,
689
- "the short_name (first argument) should be a single character, \
690
- or an empty string for none"
691
- ) ;
692
- let len = long_name. len ( ) ;
693
- assert ! (
694
- len == 0 || len > 1 ,
695
- "the long_name (second argument) should be longer than a single \
696
- character, or an empty string for none"
697
- ) ;
677
+ fn push_group ( & mut self , group : OptGroup ) {
678
+ self . validate_names ( & group) ;
679
+ self . grps . push ( group) ;
680
+ }
681
+
682
+ fn validate_names ( & self , group : & OptGroup ) {
683
+ let len = group. short_name . len ( ) ;
684
+ assert ! (
685
+ len == 1 || len == 0 ,
686
+ "the short_name (first argument) should be a single character, \
687
+ or an empty string for none"
688
+ ) ;
689
+ let len = group. long_name . len ( ) ;
690
+ assert ! (
691
+ len == 0 || len > 1 ,
692
+ "the long_name (second argument) should be longer than a single \
693
+ character, or an empty string for none"
694
+ ) ;
695
+
696
+ debug_assert ! (
697
+ group. short_name. is_empty( )
698
+ || self . grps. iter( ) . all( |g| g. short_name != group. short_name) ,
699
+ "the short option name -{} caused conflict among multiple options" ,
700
+ group. short_name,
701
+ ) ;
702
+ debug_assert ! (
703
+ group. long_name. is_empty( )
704
+ || self . grps. iter( ) . all( |g| g. long_name != group. long_name) ,
705
+ "the long option name --{} caused conflict among multiple options" ,
706
+ group. long_name,
707
+ ) ;
708
+ }
698
709
}
699
710
700
711
/// What parsing style to use when parsing arguments.
0 commit comments