@@ -15,7 +15,7 @@ use args::{AnyArg, ArgMatcher};
15
15
use args:: settings:: { ArgSettings , ArgFlags } ;
16
16
use errors:: { ErrorKind , Error } ;
17
17
use errors:: Result as ClapResult ;
18
- use utf8 :: INVALID_UTF8 ;
18
+ use INVALID_UTF8 ;
19
19
use suggestions;
20
20
use INTERNAL_ERROR_MSG ;
21
21
use SubCommand ;
@@ -24,6 +24,7 @@ use osstringext::OsStrExt2;
24
24
use app:: meta:: AppMeta ;
25
25
use args:: MatchedArg ;
26
26
27
+ #[ doc( hidden) ]
27
28
pub struct Parser < ' a , ' b > where ' a : ' b {
28
29
required : Vec < & ' b str > ,
29
30
short_list : Vec < char > ,
@@ -114,30 +115,23 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
114
115
115
116
// actually adds the arguments
116
117
pub fn add_arg ( & mut self , a : & Arg < ' a , ' b > ) {
117
- if self . flags . iter ( ) . any ( |f| & f. name == & a. name ) ||
118
- self . opts . iter ( ) . any ( |o| o. name == a. name ) ||
119
- self . positionals . values ( ) . any ( |p| p. name == a. name ) {
120
- panic ! ( "Non-unique argument name: {} is already in use" , a. name) ;
121
- }
118
+ assert ! ( !( self . flags. iter( ) . any( |f| & f. name == & a. name)
119
+ || self . opts. iter( ) . any( |o| o. name == a. name)
120
+ || self . positionals. values( ) . any( |p| p. name == a. name) ) ,
121
+ format!( "Non-unique argument name: {} is already in use" , a. name) ) ;
122
122
if let Some ( grp) = a. group {
123
123
let ag = self . groups . entry ( grp) . or_insert_with ( || ArgGroup :: with_name ( grp) ) ;
124
124
ag. args . push ( a. name ) ;
125
125
}
126
126
if let Some ( s) = a. short {
127
- if self . short_list . contains ( & s) {
128
- panic ! ( "Argument short must be unique\n \n \t -{} is already in use" ,
129
- s) ;
130
- } else {
131
- self . short_list . push ( s) ;
132
- }
127
+ assert ! ( !self . short_list. contains( & s) ,
128
+ format!( "Argument short must be unique\n \n \t -{} is already in use" , s) ) ;
129
+ self . short_list . push ( s) ;
133
130
}
134
131
if let Some ( l) = a. long {
135
- if self . long_list . contains ( & l) {
136
- panic ! ( "Argument long must be unique\n \n \t --{} is already in use" ,
137
- l) ;
138
- } else {
139
- self . long_list . push ( l) ;
140
- }
132
+ assert ! ( !self . long_list. contains( & l) ,
133
+ format!( "Argument long must be unique\n \n \t --{} is already in use" , l) ) ;
134
+ self . long_list . push ( l) ;
141
135
if l == "help" {
142
136
self . set ( AppSettings :: NeedsLongHelp ) ;
143
137
} else if l == "version" {
@@ -153,12 +147,10 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
153
147
} else {
154
148
a. index . unwrap ( ) as usize
155
149
} ;
156
- if self . positionals . contains_key ( & i) {
157
- panic ! ( "Argument \" {}\" has the same index as another positional \
150
+ assert ! ( ! self . positionals. contains_key( & i) ,
151
+ format !( "Argument \" {}\" has the same index as another positional \
158
152
argument\n \n \t Perhaps try .multiple(true) to allow one positional argument \
159
- to take multiple values",
160
- a. name) ;
161
- }
153
+ to take multiple values", a. name) ) ;
162
154
let pb = PosBuilder :: from_arg ( & a, i as u8 , & mut self . required ) ;
163
155
self . positionals . insert ( i, pb) ;
164
156
} else if a. is_set ( ArgSettings :: TakesValue ) {
@@ -169,11 +161,9 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
169
161
self . flags . push ( fb) ;
170
162
}
171
163
if a. is_set ( ArgSettings :: Global ) {
172
- if a. is_set ( ArgSettings :: Required ) {
173
- panic ! ( "Global arguments cannot be required.\n \n \t '{}' is marked as global and \
174
- required",
175
- a. name) ;
176
- }
164
+ assert ! ( !a. is_set( ArgSettings :: Required ) ,
165
+ format!( "Global arguments cannot be required.\n \n \t '{}' is marked as global and \
166
+ required", a. name) ) ;
177
167
self . global_args . push ( a. into ( ) ) ;
178
168
}
179
169
}
@@ -383,7 +373,6 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
383
373
self . settings . set ( s)
384
374
}
385
375
386
-
387
376
pub fn verify_positionals ( & mut self ) {
388
377
// Because you must wait until all arguments have been supplied, this is the first chance
389
378
// to make assertions on positional argument indexes
@@ -392,13 +381,9 @@ impl<'a, 'b> Parser<'a, 'b> where 'a: 'b {
392
381
// positional arguments to verify there are no gaps (i.e. supplying an index of 1 and 3
393
382
// but no 2)
394
383
if let Some ( ( idx, ref p) ) = self . positionals . iter ( ) . rev ( ) . next ( ) {
395
- if idx != self . positionals . len ( ) {
396
- panic ! ( "Found positional argument \" {}\" who's index is {} but there are only {} \
397
- positional arguments defined",
398
- p. name,
399
- idx,
400
- self . positionals. len( ) ) ;
401
- }
384
+ assert ! ( !( idx != self . positionals. len( ) ) ,
385
+ format!( "Found positional argument \" {}\" who's index is {} but there are only {} \
386
+ positional arguments defined", p. name, idx, self . positionals. len( ) ) ) ;
402
387
}
403
388
404
389
// Next we verify that only the highest index has a .multiple(true) (if any)
0 commit comments