Skip to content

Commit 73229bd

Browse files
committed
Merge pull request #111 from kbknapp/less-color
2 parents 05a34c8 + a5e25a5 commit 73229bd

File tree

2 files changed

+79
-47
lines changed

2 files changed

+79
-47
lines changed

clap-tests/run_tests.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,38 @@
4545
4646
USAGE:
4747
claptests [POSITIONAL] [FLAGS] [OPTIONS] [SUBCOMMANDS]
48+
4849
For more information try --help'''
4950

5051
_arg_dym_usage = '''The argument --optio isn't valid
51-
\tDid you mean --option ?
52+
Did you mean --option ?
53+
5254
USAGE:
53-
\tclaptests
55+
claptests --option <opt>...
56+
5457
For more information try --help'''
5558

5659
_pv_dym_usage = '''"slo" isn't a valid value for '--Option <option3>'
5760
[valid values: fast slow]
5861
Did you mean 'slow' ?
62+
5963
USAGE:
6064
claptests --Option <option3>
65+
6166
For more information try --help'''
6267

6368
_excluded = '''The argument '--flag' cannot be used with '-F'
69+
6470
USAGE:
6571
\tclaptests [positional2] -F --long-option-2 <option2>
72+
6673
For more information try --help'''
6774

6875
_excluded_l = '''The argument -f cannot be used '-F'
76+
6977
USAGE:
7078
claptests [positional2] -F --long-option-2 <option2>
79+
7180
For more information try --help'''
7281

7382
_required = '''The following required arguments were not supplied:
@@ -76,6 +85,7 @@
7685
7786
USAGE:
7887
\tclaptests [positional2] -F --long-option-2 <option2>
88+
7989
For more information try --help'''
8090

8191
_fop = '''flag present 1 times
@@ -173,8 +183,10 @@
173183
scpositional present with value: value'''
174184

175185
_min_vals_few = '''The argument '--minvals2 <minvals>...' requires at least 2 values, but 1 was provided
186+
176187
USAGE:
177188
\tclaptests --minvals2 <minvals>...
189+
178190
For more information try --help'''
179191

180192
_exact = '''flag NOT present
@@ -201,19 +213,25 @@
201213
positional present with value: too
202214
subcmd NOT present'''
203215

204-
_mult_vals_more = '''Argument --multvals was supplied more than once, but does not support multiple values
216+
_mult_vals_more = '''The argument --multvals was supplied more than once, but does not support multiple values
217+
205218
USAGE:
206219
\tclaptests --multvals <one> <two>
220+
207221
For more information try --help'''
208222

209-
_mult_vals_few = '''Argument '--multvals <one> <two>' requires a value but none was supplied
223+
_mult_vals_few = '''The argument '--multvals <one> <two>' requires a value but none was supplied
224+
210225
USAGE:
211226
\tclaptests --multvals <one> <two>
227+
212228
For more information try --help'''
213229

214230
_mult_vals_2m1 = '''The argument '--multvalsmo <one> <two>' requires 2 values, but 1 was provided
231+
215232
USAGE:
216233
claptests --multvalsmo <one> <two>
234+
217235
For more information try --help'''
218236

219237
_bin = './target/release/claptests'

src/app.rs

+57-43
Original file line numberDiff line numberDiff line change
@@ -1152,17 +1152,17 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
11521152
// Reports and error to the users screen along with an optional usage statement and quits
11531153
#[cfg(not(feature = "color"))]
11541154
fn report_error(&self, msg: String, usage: bool, quit: bool, matches: Option<Vec<&str>>) {
1155-
println!("{}", msg);
1155+
println!("{}\n", msg);
11561156
if usage { self.print_usage(true, matches); }
11571157
if quit { self.exit(1); }
11581158
}
11591159

11601160
#[cfg(feature = "color")]
11611161
fn report_error(&self, msg: String, usage: bool, quit: bool, matches: Option<Vec<&str>>) {
1162-
println!("{}", Red.paint(&msg[..]));
1162+
println!("{}\n", Red.paint(&msg[..]));
11631163
if usage {
1164-
print!("{}",Red.paint(&self.create_usage(matches)[..]));
1165-
println!("{}",Red.paint("\nFor more information try --help"));
1164+
print!("{}",&self.create_usage(matches)[..]);
1165+
println!("{}","\n\nFor more information try --help");
11661166
}
11671167
if quit { self.exit(1); }
11681168
}
@@ -1233,7 +1233,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
12331233

12341234
/// Returns a suffix that can be empty, or is the standard 'did you mean phrase
12351235
fn did_you_mean_suffix<'z, T, I>(arg: &str, values: I, style: DidYouMeanMessageStyle)
1236-
-> String
1236+
-> (String, Option<&'z str>)
12371237
where T: AsRef<str> + 'z,
12381238
I: IntoIterator<Item=&'z T> {
12391239
match did_you_mean(arg, values) {
@@ -1248,9 +1248,9 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
12481248
suffix.push('\'');
12491249
}
12501250
suffix.push_str(" ?");
1251-
suffix
1251+
(suffix, Some(candidate))
12521252
},
1253-
None => String::new(),
1253+
None => (String::new(), None),
12541254
}
12551255
}
12561256

@@ -1267,7 +1267,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
12671267
.fold(String::new(), |acc, name| {
12681268
acc + &format!(" {}",name)[..]
12691269
})),
1270-
suffix),
1270+
suffix.0),
12711271
true,
12721272
true,
12731273
Some(matches.args.keys().map(|k| *k).collect()));
@@ -1283,7 +1283,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
12831283
while let Some(arg) = it.next() {
12841284
let arg_slice = &arg[..];
12851285
let mut skip = false;
1286-
if !pos_only && !arg_slice.starts_with("-") {
1286+
if !pos_only && !arg_slice.starts_with("-") && !self.subcommands.contains_key(arg_slice) {
12871287
if let Some(nvo) = needs_val_of {
12881288
if let Some(ref opt) = self.opts.get(nvo) {
12891289
if let Some(ref p_vals) = opt.possible_vals {
@@ -1349,7 +1349,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
13491349
if let Some(ref o) = self.opts.get(name) {
13501350
if !o.multiple {
13511351
self.report_error(
1352-
format!("Argument '{}' requires a value but none was supplied", o),
1352+
format!("The argument '{}' requires a value but none was supplied", o),
13531353
true,
13541354
true,
13551355
Some(matches.args.keys().map(|k| *k).collect() ) );
@@ -1384,7 +1384,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
13841384
self.report_error(
13851385
format!("The subcommand '{}' isn't valid\n\tDid you mean '{}' ?\n\n\
13861386
If you received this message in error, try \
1387-
re-running with '{} -- {}'\n",
1387+
re-running with '{} -- {}'",
13881388
arg,
13891389
candidate_subcommand,
13901390
self.bin_name.clone().unwrap_or(self.name.clone()),
@@ -1512,34 +1512,35 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
15121512
};
15131513
if should_err {
15141514
self.report_error(
1515-
format!("Argument '{}' requires a value but none was supplied", o),
1515+
format!("The argument '{}' requires a value but none was \
1516+
supplied", o),
15161517
true,
15171518
true,
15181519
Some(matches.args.keys().map(|k| *k).collect() ) );
15191520
}
15201521
}
15211522
else if !o.multiple {
15221523
self.report_error(
1523-
format!("Argument '{}' requires a value but none was supplied", o),
1524+
format!("The argument '{}' requires a value but none was supplied", o),
15241525
true,
15251526
true,
15261527
Some(matches.args.keys().map(|k| *k).collect() ) );
15271528
}
15281529
else {
15291530
self.report_error(format!("The following required arguments were not \
1530-
supplied:\n{}",
1531+
supplied:{}",
15311532
self.get_required_from(self.required.iter()
15321533
.map(|s| *s)
15331534
.collect::<HashSet<_>>())
15341535
.iter()
1535-
.fold(String::new(), |acc, s| acc + &format!("\t'{}'\n",s)[..])),
1536+
.fold(String::new(), |acc, s| acc + &format!("\n\t'{}'",s)[..])),
15361537
true,
15371538
true,
15381539
Some(matches.args.keys().map(|k| *k).collect()));
15391540
}
15401541
} else {
15411542
self.report_error(
1542-
format!("Argument '{}' requires a value but none was supplied",
1543+
format!("The argument '{}' requires a value but none was supplied",
15431544
format!("{}", self.positionals_idx.get(
15441545
self.positionals_name.get(a).unwrap()).unwrap())),
15451546
true,
@@ -1556,12 +1557,12 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
15561557
if !self.required.is_empty() {
15571558
if self.validate_required(&matches) {
15581559
self.report_error(format!("The following required arguments were not \
1559-
supplied:\n{}",
1560+
supplied:{}",
15601561
self.get_required_from(self.required.iter()
15611562
.map(|s| *s)
15621563
.collect::<HashSet<_>>())
15631564
.iter()
1564-
.fold(String::new(), |acc, s| acc + &format!("\t'{}'\n",s)[..])),
1565+
.fold(String::new(), |acc, s| acc + &format!("\n\t'{}'",s)[..])),
15651566
true,
15661567
true,
15671568
Some(matches.args.keys().map(|k| *k).collect()));
@@ -1683,8 +1684,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
16831684
arg = arg_vec[0];
16841685
// prevents "--config= value" typo
16851686
if arg_vec[1].len() == 0 {
1686-
self.report_error(format!("Argument --{} requires a value, but none was supplied",
1687-
arg),
1687+
self.report_error(format!("The argument --{} requires a value, but none was \
1688+
supplied", arg),
16881689
true,
16891690
true,
16901691
Some(matches.args.keys().map(|k| *k).collect()));
@@ -1705,7 +1706,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
17051706

17061707
if matches.args.contains_key(v.name) {
17071708
if !v.multiple {
1708-
self.report_error(format!("Argument --{} was supplied more than once, but \
1709+
self.report_error(format!("The argument --{} was supplied more than once, but \
17091710
does not support multiple values", arg),
17101711
true,
17111712
true,
@@ -1788,7 +1789,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
17881789

17891790
// Make sure this isn't one being added multiple times if it doesn't suppor it
17901791
if matches.args.contains_key(v.name) && !v.multiple {
1791-
self.report_error(format!("Argument '{}' was supplied more than once, but does \
1792+
self.report_error(format!("The argument '{}' was supplied more than once, but does \
17921793
not support multiple values", v),
17931794
true,
17941795
true,
@@ -1835,27 +1836,40 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{
18351836
return None;
18361837
}
18371838

1838-
let mut suffix = App::did_you_mean_suffix(arg, self.opts.values()
1839-
.filter_map(|v|
1840-
if let Some(ref l) = v.long {
1841-
Some(l)
1842-
} else {
1843-
None
1844-
}
1845-
), DidYouMeanMessageStyle::LongFlag);
1846-
1847-
// If it didn't find a good match for opts, try flags
1848-
if suffix.is_empty() {
1849-
suffix = App::did_you_mean_suffix(arg, self.flags.values()
1850-
.filter_map(|v|
1851-
if let Some(ref l) = v.long {
1852-
Some(l)
1853-
} else {
1854-
None
1855-
}
1856-
), DidYouMeanMessageStyle::LongFlag);
1857-
}
1858-
self.report_error(format!("The argument --{} isn't valid{}", arg, suffix),
1839+
let suffix = App::did_you_mean_suffix(arg,
1840+
self.long_list.iter(),
1841+
DidYouMeanMessageStyle::LongFlag);
1842+
if let Some(name) = suffix.1 {
1843+
if let Some(ref opt) = self.opts.values()
1844+
.filter_map(|ref o| {
1845+
if o.long.is_some() && o.long.unwrap() == name {
1846+
Some(o.name)
1847+
} else {
1848+
None
1849+
}
1850+
})
1851+
.next() {
1852+
matches.args.insert(opt, MatchedArg {
1853+
occurrences: 0,
1854+
values: None
1855+
});
1856+
} else if let Some(ref flg) = self.flags.values()
1857+
.filter_map(|ref f| {
1858+
if f.long.is_some() && f.long.unwrap() == name {
1859+
Some(f.name)
1860+
} else {
1861+
None
1862+
}
1863+
})
1864+
.next() {
1865+
matches.args.insert(flg, MatchedArg {
1866+
occurrences: 0,
1867+
values: None
1868+
});
1869+
}
1870+
}
1871+
1872+
self.report_error(format!("The argument --{} isn't valid{}", arg, suffix.0),
18591873
true,
18601874
true,
18611875
Some(matches.args.keys().map(|k| *k).collect()));

0 commit comments

Comments
 (0)