--file_lines argument now supports "stdin"

This commit is contained in:
Jonathan Behrens 2017-03-30 19:16:06 -04:00
parent 36f0402bac
commit 56da223dc2
2 changed files with 12 additions and 11 deletions

View file

@ -217,17 +217,16 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
// write_mode is always Plain for Stdin.
config.write_mode = WriteMode::Plain;
// parse file_lines
if let Some(ref file_lines) = matches.opt_str("file-lines") {
config.file_lines = try!(file_lines.parse());
}
Ok(run(Input::Text(input), &config))
}
Operation::Format {
mut files,
config_path,
} => {
Operation::Format { files, config_path } => {
let options = try!(CliOptions::from_matches(&matches));
// Add any additional files that were specified via `--file-lines`.
files.extend(options.file_lines.files().cloned().map(PathBuf::from));
let mut config = Config::default();
let mut path = None;
// Load the config path file if provided
@ -345,9 +344,8 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
Some(dir)
});
// if no file argument is supplied and `--file-lines` is not specified, read from stdin
if matches.free.is_empty() && !matches.opt_present("file-lines") {
// if no file argument is supplied, read from stdin
if matches.free.is_empty() {
let mut buffer = String::new();
try!(io::stdin().read_to_string(&mut buffer));
@ -357,7 +355,6 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
});
}
// We append files from `--file-lines` later in `execute()`.
let files: Vec<_> = matches.free.iter().map(PathBuf::from).collect();
Ok(Operation::Format {

View file

@ -162,6 +162,10 @@ impl<'a> iter::Iterator for Files<'a> {
}
fn canonicalize_path_string(s: &str) -> Result<String, ()> {
if s == "stdin" {
return Ok(s.to_string());
}
match path::PathBuf::from(s).canonicalize() {
Ok(canonicalized) => canonicalized.to_str().map(|s| s.to_string()).ok_or(()),
_ => Err(()),