@@ -358,6 +358,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
358
358
min_vals : a. min_vals ,
359
359
max_vals : a. max_vals ,
360
360
help : a. help ,
361
+ empty_vals : a. empty_vals
361
362
} ;
362
363
if pb. min_vals . is_some ( ) && !pb. multiple {
363
364
panic ! ( "Argument \" {}\" does not allow multiple values, yet it is expecting {} \
@@ -415,6 +416,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
415
416
val_names : a. val_names ,
416
417
requires : None ,
417
418
required : a. required ,
419
+ empty_vals : a. empty_vals
418
420
} ;
419
421
if let Some ( ref vec) = ob. val_names {
420
422
ob. num_vals = Some ( vec. len ( ) as u8 ) ;
@@ -456,17 +458,18 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
456
458
}
457
459
self . opts . insert ( a. name , ob) ;
458
460
} else {
459
- if a. short . is_none ( ) && a. long . is_none ( ) {
460
- // Could be a posistional constructed from usage string
461
-
461
+ if !a. empty_vals {
462
+ // Empty vals defaults to true, so if it's false it was manually set
463
+ panic ! ( "The argument '{}' cannot have empty_values() set because it is a flag. \
464
+ Perhaps you mean't to set takes_value(true) as well?", a. name) ;
462
465
}
463
466
if a. required {
464
- panic ! ( "Argument \" {} \" cannot be required(true) because it has no index() or \
467
+ panic ! ( "The argument '{}' cannot be required(true) because it has no index() or \
465
468
takes_value(true)", a. name) ;
466
469
}
467
470
if a. possible_vals . is_some ( ) {
468
- panic ! ( "Argument \" {} \" cannot have a specific value set because it doesn't have \
469
- takes_value(true) set", a. name) ;
471
+ panic ! ( "The argument '{}' cannot have a specific value set because it doesn't \
472
+ have takes_value(true) set", a. name) ;
470
473
}
471
474
// No need to check for index() or takes_value() as that is handled above
472
475
@@ -1316,7 +1319,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1316
1319
if let Some ( ref ma) = matches. args . get ( opt. name ) {
1317
1320
if let Some ( ref vals) = ma. values {
1318
1321
if num == vals. len ( ) as u8 && !opt. multiple {
1319
- self . report_error ( format ! ( "The argument \" {} \" was found, \
1322
+ self . report_error ( format ! ( "The argument '{}' was found, \
1320
1323
but '{}' only expects {} values",
1321
1324
arg,
1322
1325
opt,
@@ -1331,6 +1334,17 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1331
1334
}
1332
1335
}
1333
1336
}
1337
+
1338
+ if !opt. empty_vals &&
1339
+ matches. args . contains_key ( opt. name ) &&
1340
+ arg. is_empty ( ) {
1341
+ self . report_error ( format ! ( "The argument '{}' does not allow empty \
1342
+ values, but one was found.", opt) ,
1343
+ true ,
1344
+ true ,
1345
+ Some ( matches. args . keys ( )
1346
+ . map ( |k| * k) . collect ( ) ) ) ;
1347
+ }
1334
1348
if let Some ( ref mut o) = matches. args . get_mut ( opt. name ) {
1335
1349
// Options have values, so we can unwrap()
1336
1350
if let Some ( ref mut vals) = o. values {
@@ -1457,7 +1471,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1457
1471
if let Some ( ref ma) = matches. args . get ( p. name ) {
1458
1472
if let Some ( ref vals) = ma. values {
1459
1473
if vals. len ( ) as u8 == num {
1460
- self . report_error ( format ! ( "The argument \" {} \" was found, \
1474
+ self . report_error ( format ! ( "The argument '{}' was found, \
1461
1475
but '{}' wasn't expecting any more values", arg, p) ,
1462
1476
true ,
1463
1477
true ,
@@ -1467,6 +1481,14 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1467
1481
}
1468
1482
}
1469
1483
}
1484
+ if !p. empty_vals && matches. args . contains_key ( p. name ) && arg. is_empty ( ) {
1485
+ self . report_error ( format ! ( "The argument '{}' does not allow empty \
1486
+ values, but one was found.", p) ,
1487
+ true ,
1488
+ true ,
1489
+ Some ( matches. args . keys ( )
1490
+ . map ( |k| * k) . collect ( ) ) ) ;
1491
+ }
1470
1492
// Check if it's already existing and update if so...
1471
1493
if let Some ( ref mut pos) = matches. args . get_mut ( p. name ) {
1472
1494
done = true ;
@@ -1483,6 +1505,14 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1483
1505
// Was an update made, or is this the first occurrence?
1484
1506
if !done {
1485
1507
let mut bm = BTreeMap :: new ( ) ;
1508
+ if !p. empty_vals && arg. is_empty ( ) {
1509
+ self . report_error ( format ! ( "The argument '{}' does not allow empty \
1510
+ values, but one was found.", p) ,
1511
+ true ,
1512
+ true ,
1513
+ Some ( matches. args . keys ( )
1514
+ . map ( |k| * k) . collect ( ) ) ) ;
1515
+ }
1486
1516
bm. insert ( 1 , arg. clone ( ) ) ;
1487
1517
matches. args . insert ( p. name , MatchedArg {
1488
1518
occurrences : 1 ,
@@ -1742,6 +1772,14 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1742
1772
}
1743
1773
}
1744
1774
if arg_val. is_some ( ) {
1775
+ if !v. empty_vals && arg. is_empty ( ) && matches. args . contains_key ( v. name ) {
1776
+ self . report_error ( format ! ( "The argument '{}' does not allow empty \
1777
+ values, but one was found.", v) ,
1778
+ true ,
1779
+ true ,
1780
+ Some ( matches. args . keys ( )
1781
+ . map ( |k| * k) . collect ( ) ) ) ;
1782
+ }
1745
1783
if let Some ( ref mut o) = matches. args . get_mut ( v. name ) {
1746
1784
o. occurrences += 1 ;
1747
1785
if let Some ( ref mut vals) = o. values {
@@ -1751,6 +1789,14 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1751
1789
}
1752
1790
}
1753
1791
} else {
1792
+ if !v. empty_vals && arg_val. is_some ( ) && arg_val. clone ( ) . unwrap ( ) . is_empty ( ) {
1793
+ self . report_error ( format ! ( "The argument '{}' does not allow empty \
1794
+ values, but one was found.", v) ,
1795
+ true ,
1796
+ true ,
1797
+ Some ( matches. args . keys ( )
1798
+ . map ( |k| * k) . collect ( ) ) ) ;
1799
+ }
1754
1800
matches. args . insert ( v. name , MatchedArg {
1755
1801
occurrences : if arg_val. is_some ( ) { 1 } else { 0 } ,
1756
1802
values : if arg_val. is_some ( ) {
@@ -1951,7 +1997,6 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
1951
1997
}
1952
1998
} else {
1953
1999
matches. args . insert ( v. name , MatchedArg {
1954
- // name: v.name.to_owned(),
1955
2000
// occurrences will be incremented on getting a value
1956
2001
occurrences : 0 ,
1957
2002
values : Some ( BTreeMap :: new ( ) )
0 commit comments