diff options
| author | A Farzat <a@farzat.xyz> | 2025-11-21 19:53:35 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2025-11-21 19:53:35 +0300 |
| commit | db713f8e215b2c349e4eaf0fa4791b876125148b (patch) | |
| tree | 9244a2056cf7cdaa7e65315286dd7954a9cf1c8a | |
| parent | 78921f658502442fa72818e829fbd81c2e39e099 (diff) | |
| download | simple-rss-podcast-downloader-db713f8e215b2c349e4eaf0fa4791b876125148b.tar.gz simple-rss-podcast-downloader-db713f8e215b2c349e4eaf0fa4791b876125148b.zip | |
Use clap for argument parsing
| -rw-r--r-- | Cargo.lock | 121 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/lib.rs | 19 | ||||
| -rw-r--r-- | src/main.rs | 19 |
4 files changed, 137 insertions, 23 deletions
@@ -3,6 +3,56 @@ version = 4 [[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + +[[package]] name = "atom_syndication" version = "0.12.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -77,6 +127,52 @@ dependencies = [ ] [[package]] +name = "clap" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] name = "core-foundation" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -344,6 +440,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" [[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] name = "http" version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -598,6 +700,12 @@ dependencies = [ ] [[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] name = "itoa" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -699,6 +807,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] name = "openssl" version = "0.10.75" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1032,6 +1146,7 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" name = "simple-rss-podcast-downloader" version = "0.1.0" dependencies = [ + "clap", "reqwest", "rss", ] @@ -1299,6 +1414,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] +clap = { version = "4", features = ["derive"] } reqwest = { version = "0.12", features = ["blocking"] } rss = "2" @@ -1,26 +1,7 @@ use rss::Channel; -use std::env; use std::fs::File; use std::path::PathBuf; -/// Parse CLI arguments and return (feed_url, output_dir) -pub fn parse_args() -> (String, String) { - let mut args: Vec<String> = env::args().collect(); - if args.len() < 2 { - eprintln!("Usage: {} <RSS_FEED_URL> [OUTPUT_DIR]", args[0]); - std::process::exit(1); - } - - let feed_url = args.remove(1); - let output_dir = if args.len() > 1 { - args.remove(1) - } else { - String::from(".") - }; - - (feed_url, output_dir) -} - /// Fetch RSS feed content from a URL pub fn fetch_feed(url: &str) -> Result<String, reqwest::Error> { reqwest::blocking::get(url)?.text() diff --git a/src/main.rs b/src/main.rs index 7e28a52..a2ac775 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,26 @@ +use clap::Parser; use simple_rss_podcast_downloader::*; +#[derive(Parser, Debug)] +#[command(author, version, about)] +struct Cli { + feed_url: String, + #[arg(default_value = ".")] + output_dir: String, + #[arg(short, long)] + numbered: bool, +} + fn main() -> Result<(), Box<dyn std::error::Error>> { - let (feed_url, output_dir) = parse_args(); + let args = Cli::parse(); - println!("Feed RSS feed from: {}", feed_url); + println!("Feed RSS feed from: {}", args.feed_url); - let xml = fetch_feed(&feed_url)?; + let xml = fetch_feed(&args.feed_url)?; let channel = parse_feed(&xml)?; for url in get_audio_urls(&channel) { println!("Downloading file: {}", url); - download_file(url, &output_dir)?; + download_file(url, &args.output_dir)?; } Ok(()) |
