Skip to content

Commit fc7a31a

Browse files
committed
fix: fixed confusing error message, also added test for it
1 parent af6b58f commit fc7a31a

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/args/arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<'n, 'l, 'h, 'g, 'p, 'r> Arg<'n, 'l, 'h, 'g, 'p, 'r> {
280280
}
281281

282282
Arg {
283-
name: name.unwrap(),
283+
name: name.unwrap_or_else(|| panic!("Missing flag name in \"{}\", check from_usage call", u)),
284284
short: short,
285285
long: long,
286286
help: help,

src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1242,4 +1242,17 @@ mod tests {
12421242
assert!(m.is_present("debug"));
12431243
}
12441244

1245+
#[test]
1246+
#[should_panic]
1247+
fn short_flag_misspel() {
1248+
App::new("short_flag")
1249+
.arg(Arg::from_usage("-f1, --flag 'some flag'"));
1250+
}
1251+
1252+
#[test]
1253+
#[should_panic]
1254+
fn short_flag_name_missing() {
1255+
App::new("short_flag")
1256+
.arg(Arg::from_usage("-f 'some flag'"));
1257+
}
12451258
}

src/usageparser.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ impl<'u> Iterator for UsageParser<'u> {
124124
self.e += 1;
125125
continue
126126
},
127-
_ => {
127+
None => {
128128
return None
129+
},
130+
Some(c) => {
131+
panic!("Usage parser error, unexpected \"{}\" at \"{}\", check from_usage call", c, self.usage);
129132
}
130133
}
131134
}

0 commit comments

Comments
 (0)