Don't panic if input file doesn't exist

This commit is contained in:
Matéo Duparc 2021-07-04 18:47:24 +02:00
parent c3f24b2f4a
commit 219e431fbd
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
1 changed files with 12 additions and 4 deletions

View File

@ -111,18 +111,26 @@ pub fn parse() -> Option<CliArgs> {
let block_size = number(app.value_of("blocksize").unwrap())?;
let input = app
let input = match app
.value_of("INPUT")
.and_then(|s| if s == "-" { None } else { Some(s) })
.map(|s| Box::new(File::open(s).unwrap()) as Box<dyn Read>)
.unwrap_or_else(|| Box::new(stdin()));
{
Some(s) =>
Box::new(
File::open(s)
.map_err(|e| eprintln!("{}: {}", s, e))
.ok()?
) as Box<dyn Read>
,
None => Box::new(stdin())
};
let output = app
.value_of("OUTPUT")
.and_then(|s| if s == "-" { None } else { Some(s) })
.map(|s| {
if Path::new(s).exists() {
eprintln!("{} already exists", s);
eprintln!("WARNING: {} already exists", s);
None
} else {
Some(Box::new(File::create(s).unwrap()) as Box<dyn Write>)