Skip to content

Commit 2cfe1e7

Browse files
authored
Merge pull request #1328 from Machtan/master
Add '--version' flag and allow version and help flags when called as 'cargo-clippy'
2 parents a260e65 + 2d9386f commit 2cfe1e7

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/main.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
1717
use rustc::session::{config, Session};
1818
use rustc::session::config::{Input, ErrorOutputType};
1919
use std::path::PathBuf;
20-
use std::process::Command;
20+
use std::process::{self, Command};
2121
use syntax::ast;
22+
use std::io::{self, Write};
2223

2324
use clippy_lints::utils::cargo;
2425

@@ -118,6 +119,7 @@ Usage:
118119
Common options:
119120
-h, --help Print this message
120121
--features Features to compile for the package
122+
-V, --version Print version info and exit
121123
122124
Other options are the same as `cargo rustc`.
123125
@@ -140,26 +142,41 @@ fn show_help() {
140142
println!("{}", CARGO_CLIPPY_HELP);
141143
}
142144

145+
#[allow(print_stdout)]
146+
fn show_version() {
147+
println!("{}", env!("CARGO_PKG_VERSION"));
148+
}
149+
143150
pub fn main() {
144151
use std::env;
145152

146153
if env::var("CLIPPY_DOGFOOD").map(|_| true).unwrap_or(false) {
147154
panic!("yummy");
148155
}
156+
157+
// Check for version and help flags even when invoked as 'cargo-clippy'
158+
if std::env::args().any(|a| a == "--help" || a == "-h") {
159+
show_help();
160+
return;
161+
}
162+
if std::env::args().any(|a| a == "--version" || a == "-V") {
163+
show_version();
164+
return;
165+
}
149166

150167
let dep_path = env::current_dir().expect("current dir is not readable").join("target").join("debug").join("deps");
151168

152169
if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
153170
// this arm is executed on the initial call to `cargo clippy`
154171

155-
if std::env::args().any(|a| a == "--help" || a == "-h") {
156-
show_help();
157-
return;
158-
}
159-
160172
let manifest_path_arg = std::env::args().skip(2).find(|val| val.starts_with("--manifest-path="));
161173

162-
let mut metadata = cargo::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)).expect("could not obtain cargo metadata");
174+
let mut metadata = if let Ok(metadata) = cargo::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
175+
metadata
176+
} else {
177+
let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata."));
178+
process::exit(101);
179+
};
163180

164181
assert_eq!(metadata.version, 1);
165182

0 commit comments

Comments
 (0)