From b08b59e33fd2891bce67478c815b6e5180a53a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Thu, 13 Jul 2023 20:05:02 +0200 Subject: [PATCH] Add the CLI --- Cargo.toml | 2 ++ src/config.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 13 ++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/config.rs diff --git a/Cargo.toml b/Cargo.toml index 3246af4..fa51688 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,5 @@ license = "MIT OR Apache-2.0" publish = false [dependencies] +anyhow = { version = "1.0.71", default-features = false, features = ["std"] } +clap = { version = "4.3.11", default-features = false, features = ["derive", "std"] } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..0779566 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,38 @@ +use anyhow::Result; +use clap::Parser; +use std::collections::HashSet; +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::path::PathBuf; + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Config { + #[arg(short, long)] + address: Vec, + #[arg(short = 'A', long)] + address_file: Option, + #[arg(short, long, default_value_t = crate::DEFAULT_SEPARATOR)] + separator: char, +} + +impl Config { + pub fn addresses(&self) -> Result> { + let mut addr_set = HashSet::new(); + for addr in &self.address { + addr_set.insert(addr.to_string()); + } + if let Some(path) = &self.address_file { + let f = File::open(path)?; + let f = BufReader::new(f); + for line in f.lines() { + let line = line?; + let addr = line.trim(); + if !addr.is_empty() && !addr.starts_with(crate::COMMENT_CHAR) { + addr_set.insert(addr.to_string()); + } + } + } + Ok(addr_set) + } +} diff --git a/src/main.rs b/src/main.rs index f328e4d..54082b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1 +1,12 @@ -fn main() {} +use clap::Parser; + +mod config; + +const COMMENT_CHAR: char = '#'; +const DEFAULT_SEPARATOR: char = '+'; + +fn main() { + let cfg = config::Config::parse(); + println!("{cfg:?}"); + println!("{:?}", cfg.addresses()); +}