diff options
author | Melody Horn <melody@boringcactus.com> | 2021-03-27 09:35:59 -0600 |
---|---|---|
committer | Melody Horn <melody@boringcactus.com> | 2021-03-27 09:35:59 -0600 |
commit | 691b671560993081d2ba25f0cc7e5b6a370e1b4c (patch) | |
tree | aa784b7f0963381129ce0951cbc678304d3d1185 /src | |
parent | 4e79ab051917fece21fcec0bd31cc2942462fda0 (diff) | |
download | webget-691b671560993081d2ba25f0cc7e5b6a370e1b4c.tar.gz webget-691b671560993081d2ba25f0cc7e5b6a370e1b4c.zip |
optionally read in URLs from a file
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 17 |
1 files changed, 16 insertions, 1 deletions
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<PathBuf>, + /// Specify output filename (will cause an error if multiple URLs are given) #[structopt(short = "O", long)] output_document: Option<PathBuf>, @@ -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() { |