Add -f flag for adding links from a file instead of needing to use cat

This commit is contained in:
Kenneth Gitere 2021-02-01 11:28:07 +03:00
parent b98c0a69a6
commit 3d56023592
3 changed files with 35 additions and 3 deletions

View file

@ -12,7 +12,13 @@ Paperoni is a web article downloader written in Rust. The downloaded articles ar
paperoni https://en.wikipedia.org/wiki/Pepperoni
```
Paperoni also supports passing multiple links as arguments. If you are on a Unix-like OS, you can simply do something like this:
Paperoni also supports passing multiple links as arguments. These can be read from a file using the `-f` flag.
```sh
paperoni -f links.txt
```
Alternatively, if you are on a Unix-like OS, you can simply do something like this:
```sh
cat links.txt | xargs paperoni

View file

@ -18,4 +18,11 @@ It takes a url and downloads the article content from it and saves it to an epub
.help("Urls of web articles")
.multiple(true),
)
.arg(
Arg::with_name("file")
.short("f")
.long("file")
.help("Input file containing links")
.takes_value(true),
)
}

View file

@ -1,7 +1,7 @@
#[macro_use]
extern crate lazy_static;
use std::fs::File;
use std::{fs::File, io::Read};
use async_std::task;
use epub_builder::{EpubBuilder, EpubContent, ZipLibrary};
@ -15,8 +15,27 @@ use extractor::Extractor;
fn main() {
let app = cli::cli_init();
let arg_matches = app.get_matches();
let mut urls: Vec<String> = match arg_matches.value_of("file") {
Some(file_name) => {
if let Ok(mut file) = File::open(file_name) {
let mut content = String::new();
match file.read_to_string(&mut content) {
Ok(_) => content.lines().map(|line| line.to_owned()).collect(),
Err(_) => vec![],
}
} else {
println!("Unable to open file: {}", file_name);
vec![]
}
}
None => vec![],
};
if let Some(vals) = arg_matches.values_of("urls") {
let urls = vals.map(|val| val.to_string()).collect::<Vec<_>>();
urls.extend(vals.map(|val| val.to_string()));
}
if !urls.is_empty() {
download(urls);
}
}