From 219e431fbdebfd6cf3b4d84f7855f0618c62b192 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sun, 4 Jul 2021 18:47:24 +0200 Subject: [PATCH] Don't panic if input file doesn't exist --- src/cli.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index be9eaa0..7bcb0ae 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -111,18 +111,26 @@ pub fn parse() -> Option { 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) - .unwrap_or_else(|| Box::new(stdin())); + { + Some(s) => + Box::new( + File::open(s) + .map_err(|e| eprintln!("{}: {}", s, e)) + .ok()? + ) as Box + , + 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)