From 37cb4e1fd212d99f7c7dc7402f9a846b1d12e082 Mon Sep 17 00:00:00 2001 From: Kenneth Gitere Date: Tue, 24 Nov 2020 09:58:50 +0300 Subject: [PATCH] Change from structopt to clap This allows printing the help message if no args are passed --- Cargo.lock | 65 +---------------------------------------------------- Cargo.toml | 2 +- src/cli.rs | 30 ++++++++++++++++--------- src/main.rs | 10 ++++----- 4 files changed, 26 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c067c51..2d88011 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -822,15 +822,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "heck" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "hermit-abi" version = "0.1.17" @@ -1254,13 +1245,13 @@ name = "paperoni" version = "0.1.0-alpha1" dependencies = [ "async-std", + "clap", "epub-builder", "html5ever", "kuchiki", "lazy_static", "md5", "regex", - "structopt", "surf", "url", ] @@ -1424,30 +1415,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro-hack" version = "0.5.19" @@ -1900,30 +1867,6 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" -[[package]] -name = "structopt" -version = "0.3.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "126d630294ec449fae0b16f964e35bf3c74f940da9dca17ee9b905f7b3112eb8" -dependencies = [ - "clap", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65e51c492f9e23a220534971ff5afc14037289de430e3c83f9daf6a1b6ae91e8" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "subtle" version = "2.3.0" @@ -2168,12 +2111,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db8716a166f290ff49dabc18b44aa407cb7c6dbe1aa0971b44b8a24b0ca35aae" - [[package]] name = "unicode-width" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index 1d8fcd6..ebdee93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ readme = "README.md" [dependencies] async-std = "1.7.0" +clap = "2.33.3" epub-builder = "0.4.8" html5ever = "0.25.1" kuchiki = "0.8.1" @@ -20,5 +21,4 @@ lazy_static = "1.4.0" md5 = "0.7.0" regex = "1.4.2" surf = "2.1.0" -structopt = { version = "0.3" } url = "2.2.0" \ No newline at end of file diff --git a/src/cli.rs b/src/cli.rs index e0e12db..f62b0c5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,13 +1,21 @@ -use structopt::StructOpt; +use clap::{App, AppSettings, Arg}; -#[derive(Debug, StructOpt)] -#[structopt(name = "paperoni")] -/// Paperoni is an article downloader. -/// -/// It takes a url and downloads the article content from it and -/// saves it to an epub. -pub struct Opts { - // #[structopt(conflicts_with("links"))] - /// Url of a web article - pub urls: Vec, +pub fn cli_init() -> App<'static, 'static> { + App::new("paperoni") + .settings(&[ + AppSettings::ArgRequiredElseHelp, + AppSettings::UnifiedHelpMessage, + ]) + .version("0.1.0-alpha1") + .about( + " +Paperoni is an article downloader. +It takes a url and downloads the article content from it and saves it to an epub. + ", + ) + .arg( + Arg::with_name("urls") + .help("Urls of web articles") + .multiple(true), + ) } diff --git a/src/main.rs b/src/main.rs index e1b6e30..ae81f8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use std::fs::File; use async_std::task; use epub_builder::{EpubBuilder, EpubContent, ZipLibrary}; -use structopt::StructOpt; use url::Url; mod cli; @@ -14,10 +13,11 @@ mod moz_readability; use extractor::Extractor; fn main() { - let opt = cli::Opts::from_args(); - if !opt.urls.is_empty() { - println!("Downloading single article"); - download(opt.urls); + let app = cli::cli_init(); + let arg_matches = app.get_matches(); + if let Some(vals) = arg_matches.values_of("urls") { + let urls = vals.map(|val| val.to_string()).collect::>(); + download(urls); } }