Skip to content

Commit

Permalink
Add --check flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed May 11, 2018
1 parent 798bffb commit 4d9de48
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ fn make_opts() -> Options {
let mut opts = Options::new();

// Sorted in alphabetical order.
opts.optflag(
"",
"check",
"Run in 'check' mode. Exits with 0 if input if formatted correctly. Exits \
with 1 and prints a diff if formatting is required.",
);
opts.optopt(
"",
"color",
Expand Down Expand Up @@ -308,7 +314,8 @@ fn print_usage_to_stdout(opts: &Options, reason: &str) {
}

fn print_help_file_lines() {
println!("If you want to restrict reformatting to specific sets of lines, you can
println!(
"If you want to restrict reformatting to specific sets of lines, you can
use the `--file-lines` option. Its argument is a JSON array of objects
with `file` and `range` properties, where `file` is a file name, and
`range` is an array representing a range of lines like `[7,13]`. Ranges
Expand All @@ -325,7 +332,8 @@ rustfmt --file-lines '[
would format lines `7-13` and `21-29` of `src/lib.rs`, and lines `10-11`,
and `15` of `src/foo.rs`. No other files would be formatted, even if they
are included as out of line modules from `src/lib.rs`.");
are included as out of line modules from `src/lib.rs`."
);
}

fn print_version() {
Expand Down
9 changes: 8 additions & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ pub struct CliOptions {
verbose: bool,
pub(super) config_path: Option<PathBuf>,
write_mode: Option<WriteMode>,
check: bool,
color: Option<Color>,
file_lines: FileLines, // Default is all lines in all files.
unstable_features: bool,
Expand Down Expand Up @@ -361,7 +362,11 @@ impl CliOptions {

options.config_path = matches.opt_str("config-path").map(PathBuf::from);

options.check = matches.opt_present("check");
if let Some(ref write_mode) = matches.opt_str("write-mode") {
if options.check {
return Err(format_err!("Invalid to set write-mode and `--check`"));
}
if let Ok(write_mode) = WriteMode::from_str(write_mode) {
options.write_mode = Some(write_mode);
} else {
Expand Down Expand Up @@ -410,7 +415,9 @@ impl CliOptions {
if let Some(error_on_unformatted) = self.error_on_unformatted {
config.set().error_on_unformatted(error_on_unformatted);
}
if let Some(write_mode) = self.write_mode {
if self.check {
config.set().write_mode(WriteMode::Check);
} else if let Some(write_mode) = self.write_mode {
config.set().write_mode(write_mode);
}
if let Some(color) = self.color {
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ pub use config::{file_lines, load_config, Config, Verbosity, WriteMode};

pub type FmtResult<T> = std::result::Result<T, failure::Error>;

pub const WRITE_MODE_LIST: &str =
"[replace|overwrite|display|plain|diff|coverage|checkstyle|check]";
pub const WRITE_MODE_LIST: &str = "[replace|overwrite|display|plain|diff|coverage|checkstyle]";

#[macro_use]
mod utils;
Expand Down

0 comments on commit 4d9de48

Please sign in to comment.