Skip to content

Commit 6ed217f

Browse files
committed
Add colored help
1 parent 8c48b93 commit 6ed217f

File tree

3 files changed

+74
-54
lines changed

3 files changed

+74
-54
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ clippy_lints = { path = "clippy_lints" }
2525
rustc_tools_util = "0.3.0"
2626
tempfile = { version = "3.2", optional = true }
2727
termize = "0.1"
28+
color-print = "0.3.4" # Sync version with Cargo
29+
anstream = "0.5.0"
2830

2931
[dev-dependencies]
3032
ui_test = "0.20"

src/driver.rs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![warn(rust_2018_idioms, unused_lifetimes)]
88
// warn on rustc internal lints
99
#![warn(rustc::internal)]
10+
#![allow(clippy::ignored_unit_patterns)]
1011

1112
// FIXME: switch to something more ergonomic here, once available.
1213
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
@@ -26,6 +27,8 @@ use std::ops::Deref;
2627
use std::path::Path;
2728
use std::process::exit;
2829

30+
use anstream::println;
31+
2932
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
3033
/// true, then return it. The parameter is assumed to be either `--arg=value` or `--arg value`.
3134
fn arg_value<'a, T: Deref<Target = str>>(
@@ -163,33 +166,7 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
163166
}
164167

165168
fn display_help() {
166-
println!(
167-
"\
168-
Checks a package to catch common mistakes and improve your Rust code.
169-
170-
Usage:
171-
cargo clippy [options] [--] [<opts>...]
172-
173-
Common options:
174-
-h, --help Print this message
175-
--rustc Pass all args to rustc
176-
-V, --version Print version info and exit
177-
178-
For the other options see `cargo check --help`.
179-
180-
To allow or deny a lint from the command line you can use `cargo clippy --`
181-
with:
182-
183-
-W --warn OPT Set lint warnings
184-
-A --allow OPT Set lint allowed
185-
-D --deny OPT Set lint denied
186-
-F --forbid OPT Set lint forbidden
187-
188-
You can use tool lints to allow or deny lints from your code, eg.:
189-
190-
#[allow(clippy::needless_lifetimes)]
191-
"
192-
);
169+
println!("{}", help_message());
193170
}
194171

195172
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/new?template=ice.yml";
@@ -292,3 +269,36 @@ pub fn main() {
292269
}
293270
}))
294271
}
272+
273+
#[must_use]
274+
fn help_message() -> &'static str {
275+
color_print::cstr!(
276+
"Checks a package to catch common mistakes and improve your Rust code.
277+
278+
<green,bold>Usage</>:
279+
<cyan,bold>clippy-driver</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</>
280+
281+
<green,bold>Common options:</>
282+
<cyan,bold>--no-deps</> Run Clippy only on the given crate, without linting the dependencies
283+
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</>
284+
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
285+
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
286+
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint
287+
288+
See all options with <cyan,bold>cargo check --help</>.
289+
290+
<green,bold>Allowing / Denying lints</>
291+
292+
To allow or deny a lint from the command line you can use <cyan,bold>cargo clippy --</> with:
293+
294+
<cyan,bold>-W</> / <cyan,bold>--warn</> <cyan>[LINT]</> Set lint warnings
295+
<cyan,bold>-A</> / <cyan,bold>--allow</> <cyan>[LINT]</> Set lint allowed
296+
<cyan,bold>-D</> / <cyan,bold>--deny</> <cyan>[LINT]</> Set lint denied
297+
<cyan,bold>-F</> / <cyan,bold>--forbid</> <cyan>[LINT]</> Set lint forbidden
298+
299+
You can use tool lints to allow or deny lints from your code, e.g.:
300+
301+
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
302+
"
303+
)
304+
}

src/main.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
22
// warn on lints, that are included in `rust-lang/rust`s bootstrap
33
#![warn(rust_2018_idioms, unused_lifetimes)]
4+
#![allow(clippy::ignored_unit_patterns)]
45

56
use std::env;
67
use std::path::PathBuf;
78
use std::process::{self, Command};
89

9-
const CARGO_CLIPPY_HELP: &str = "Checks a package to catch common mistakes and improve your Rust code.
10-
11-
Usage:
12-
cargo clippy [options] [--] [<opts>...]
13-
14-
Common options:
15-
--no-deps Run Clippy only on the given crate, without linting the dependencies
16-
--fix Automatically apply lint suggestions. This flag implies `--no-deps` and `--all-targets`
17-
-h, --help Print this message
18-
-V, --version Print version info and exit
19-
--explain LINT Print the documentation for a given lint
20-
21-
For the other options see `cargo check --help`.
22-
23-
To allow or deny a lint from the command line you can use `cargo clippy --`
24-
with:
25-
26-
-W --warn OPT Set lint warnings
27-
-A --allow OPT Set lint allowed
28-
-D --deny OPT Set lint denied
29-
-F --forbid OPT Set lint forbidden
30-
31-
You can use tool lints to allow or deny lints from your code, e.g.:
32-
33-
#[allow(clippy::needless_lifetimes)]
34-
";
10+
use anstream::println;
3511

3612
fn show_help() {
37-
println!("{CARGO_CLIPPY_HELP}");
13+
println!("{}", help_message());
3814
}
3915

4016
fn show_version() {
@@ -168,6 +144,38 @@ where
168144
}
169145
}
170146

147+
#[must_use]
148+
pub fn help_message() -> &'static str {
149+
color_print::cstr!(
150+
"Checks a package to catch common mistakes and improve your Rust code.
151+
152+
<green,bold>Usage</>:
153+
<cyan,bold>cargo clippy</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</>
154+
155+
<green,bold>Common options:</>
156+
<cyan,bold>--no-deps</> Run Clippy only on the given crate, without linting the dependencies
157+
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</>
158+
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
159+
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
160+
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint
161+
162+
See all options with <cyan,bold>cargo check --help</>.
163+
164+
<green,bold>Allowing / Denying lints</>
165+
166+
To allow or deny a lint from the command line you can use <cyan,bold>cargo clippy --</> with:
167+
168+
<cyan,bold>-W</> / <cyan,bold>--warn</> <cyan>[LINT]</> Set lint warnings
169+
<cyan,bold>-A</> / <cyan,bold>--allow</> <cyan>[LINT]</> Set lint allowed
170+
<cyan,bold>-D</> / <cyan,bold>--deny</> <cyan>[LINT]</> Set lint denied
171+
<cyan,bold>-F</> / <cyan,bold>--forbid</> <cyan>[LINT]</> Set lint forbidden
172+
173+
You can use tool lints to allow or deny lints from your code, e.g.:
174+
175+
<yellow,bold>#[allow(clippy::needless_lifetimes)]</>
176+
"
177+
)
178+
}
171179
#[cfg(test)]
172180
mod tests {
173181
use super::ClippyCmd;

0 commit comments

Comments
 (0)