From 691b671560993081d2ba25f0cc7e5b6a370e1b4c Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 27 Mar 2021 09:35:59 -0600 Subject: optionally read in URLs from a file --- src/main.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index da1ae1f..21b4d82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::fs; +use std::io::{self, BufRead}; use std::mem; use std::path::PathBuf; @@ -10,6 +12,10 @@ use download::download; #[derive(StructOpt, Debug)] struct Args { + /// Read URLs from a file + #[structopt(short, long)] + input_file: Option, + /// Specify output filename (will cause an error if multiple URLs are given) #[structopt(short = "O", long)] output_document: Option, @@ -22,7 +28,16 @@ struct Args { async fn main() -> Result<(), Error> { let (args, urls) = { let mut args: Args = Args::from_args(); - let urls = mem::take(&mut args.urls); + let mut urls = mem::take(&mut args.urls); + if let Some(input_file) = &args.input_file { + if let Ok(input_file) = fs::File::open(input_file) { + let input_file = io::BufReader::new(input_file); + let input_file_urls = input_file + .lines() + .filter_map(|line| line.ok().and_then(|line| line.parse().ok())); + urls.extend(input_file_urls); + } + } (args, urls) }; if urls.len() != 1 && args.output_document.is_some() { -- cgit v1.2.3