Skip to content

Commit c78ce12

Browse files
committed
fix: fixes crash on invalid arg error
1 parent 180fcc0 commit c78ce12

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/app/app.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -1559,21 +1559,25 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
15591559
-> ClapError {
15601560
let msg = match error_type {
15611561
ClapErrorType::ArgumentError => {
1562+
assert_eq!(data.len(), 1);
15621563
format!("{}", data[0].as_ref())
15631564
},
15641565
ClapErrorType::InvalidValue => {
1566+
assert_eq!(data.len(), 4);
15651567
format!("'{}' isn't a valid value for '{}'{}{}",
15661568
Format::Warning(data[0].as_ref()),
15671569
Format::Warning(data[1].as_ref()),
15681570
format!("\n\t[valid values:{}]\n", data[2].as_ref()),
15691571
data[3].as_ref())
15701572
},
15711573
ClapErrorType::InvalidArgument => {
1574+
assert_eq!(data.len(), 2);
15721575
format!("The argument '{}' isn't valid{}",
15731576
Format::Warning(data[0].as_ref()),
15741577
data[1].as_ref())
15751578
},
15761579
ClapErrorType::InvalidSubcommand => {
1580+
assert_eq!(data.len(), 2);
15771581
format!("The subcommand '{}' isn't valid\n\tDid you mean '{}' ?\n\n\
15781582
If you received this message in error, try \
15791583
re-running with '{} {} {}'",
@@ -1584,37 +1588,46 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
15841588
data[0].as_ref())
15851589
},
15861590
ClapErrorType::EmptyValue => {
1591+
assert_eq!(data.len(), 1);
15871592
format!("The argument '{}' requires a value but none was supplied",
15881593
Format::Warning(data[0].as_ref()))
15891594
},
1590-
ClapErrorType::ValueValidationError => data[0].as_ref().to_owned(),
1595+
ClapErrorType::ValueValidationError => {
1596+
assert_eq!(data.len(), 1);
1597+
data[0].as_ref().to_owned()
1598+
},
15911599
ClapErrorType::TooManyArgs => {
1600+
assert_eq!(data.len(), 2);
15921601
format!("The argument '{}' was found, but '{}' wasn't expecting any more values",
15931602
Format::Warning(data[0].as_ref()),
15941603
Format::Warning(data[1].as_ref()))
15951604
},
15961605
ClapErrorType::TooFewValues => {
1606+
assert_eq!(data.len(), 4);
15971607
format!("The argument '{}' requires at least {} values, but {} w{} provided",
15981608
Format::Warning(data[0].as_ref()),
15991609
Format::Good(data[1].as_ref()),
16001610
Format::Error(data[2].as_ref()),
16011611
data[3].as_ref())
16021612
},
16031613
ClapErrorType::TooManyValues => {
1614+
assert_eq!(data.len(), 4);
16041615
format!("The argument '{}' only requires {} values, but {} w{} provided",
16051616
Format::Warning(data[0].as_ref()),
16061617
Format::Good(data[1].as_ref()),
16071618
Format::Error(data[2].as_ref()),
16081619
data[3].as_ref())
16091620
},
16101621
ClapErrorType::WrongNumValues => {
1622+
assert_eq!(data.len(), 4);
16111623
format!("The argument '{}' requires {} values, but {} w{} provided",
16121624
Format::Warning(data[0].as_ref()),
16131625
Format::Good(data[1].as_ref()),
16141626
Format::Error(data[2].as_ref()),
16151627
data[3].as_ref())
16161628
},
16171629
ClapErrorType::ArgumentConflict => {
1630+
assert_eq!(data.len(), 2);
16181631
format!("The argument '{}' cannot be used with {}",
16191632
Format::Warning(data[0].as_ref()),
16201633
match self.blacklisted_from(data[1].as_ref(), &matches) {
@@ -1624,6 +1637,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
16241637
})
16251638
},
16261639
ClapErrorType::MissingRequiredArgument => {
1640+
// Callers still use &[""]
1641+
assert_eq!(data.len(), 1);
16271642
format!("The following required arguments were not supplied:{}",
16281643
self.get_required_from(self.required.iter()
16291644
.map(|s| *s)
@@ -1634,21 +1649,26 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
16341649
Format::Error(s))[..]))
16351650
},
16361651
ClapErrorType::MissingSubcommand => {
1652+
assert_eq!(data.len(), 1);
16371653
format!("'{}' requires a subcommand but none was provided",
16381654
Format::Warning(data[0].as_ref()))
16391655
},
16401656
ClapErrorType::MissingArgumentOrSubcommand => "".to_owned(),
16411657
ClapErrorType::UnexpectedArgument => {
1658+
assert_eq!(data.len(), 1);
16421659
format!("Found argument '{}', but {} wasn't expecting any",
16431660
Format::Warning(data[0].as_ref()),
16441661
self.bin_name.as_ref().unwrap_or(&self.name))
16451662
},
16461663
ClapErrorType::UnexpectedMultipleUsage => {
1664+
assert_eq!(data.len(), 1);
16471665
format!("The argument '{}' was supplied more \
16481666
than once, but does not support multiple values",
16491667
Format::Warning(data[0].as_ref()))
16501668
},
16511669
ClapErrorType::InvalidUnicode => {
1670+
// Callers still use &[""]
1671+
assert_eq!(data.len(), 1);
16521672
"Invalid unicode character in one or more arguments".to_owned()
16531673
}
16541674
};
@@ -3173,7 +3193,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
31733193
if !b {
31743194
return Err(
31753195
self.create_error(
3176-
&[&*format!("-{}", c)],
3196+
&[&*format!("-{}", c), ""],
31773197
ClapErrorType::InvalidArgument,
31783198
matches));
31793199
}

0 commit comments

Comments
 (0)