Skip to content

Add '--version' flag and allow version and help flags when called as 'cargo-clippy' #1328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from Nov 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
use rustc::session::{config, Session};
use rustc::session::config::{Input, ErrorOutputType};
use std::path::PathBuf;
use std::process::Command;
use std::process::{self, Command};
use syntax::ast;
use std::io::{self, Write};

use clippy_lints::utils::cargo;

Expand Down Expand Up @@ -118,6 +119,7 @@ Usage:
Common options:
-h, --help Print this message
--features Features to compile for the package
-V, --version Print version info and exit

Other options are the same as `cargo rustc`.

Expand All @@ -140,26 +142,41 @@ fn show_help() {
println!("{}", CARGO_CLIPPY_HELP);
}

#[allow(print_stdout)]
fn show_version() {
println!("{}", env!("CARGO_PKG_VERSION"));
}

pub fn main() {
use std::env;

if env::var("CLIPPY_DOGFOOD").map(|_| true).unwrap_or(false) {
panic!("yummy");
}

// Check for version and help flags even when invoked as 'cargo-clippy'
if std::env::args().any(|a| a == "--help" || a == "-h") {
show_help();
return;
}
if std::env::args().any(|a| a == "--version" || a == "-V") {
show_version();
return;
}

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

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

if std::env::args().any(|a| a == "--help" || a == "-h") {
show_help();
return;
}

let manifest_path_arg = std::env::args().skip(2).find(|val| val.starts_with("--manifest-path="));

let mut metadata = cargo::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)).expect("could not obtain cargo metadata");
let mut metadata = if let Ok(metadata) = cargo::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
metadata
} else {
let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata."));
process::exit(101);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a pretty random policy with panics vs process::exit, so I guess this is fine. What's the reason for this change though?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, actually this was just because I dislike seeing stack traces when running tools. And I'd just done something similar for 'cargo-check' so I thought "might as well".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a million other cases where cargo clippy can panic. I think we should do a clean refactor of the main function to use Result based error handling and then have a single location that calls process::exit but that's for a different PR :)

};

assert_eq!(metadata.version, 1);

Expand Down