Add the CLI

This commit is contained in:
Rodolphe Bréard 2023-07-13 20:05:02 +02:00
parent 51b079cc94
commit b08b59e33f
3 changed files with 52 additions and 1 deletions

View file

@ -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"] }

38
src/config.rs Normal file
View file

@ -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<String>,
#[arg(short = 'A', long)]
address_file: Option<PathBuf>,
#[arg(short, long, default_value_t = crate::DEFAULT_SEPARATOR)]
separator: char,
}
impl Config {
pub fn addresses(&self) -> Result<HashSet<String>> {
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)
}
}

View file

@ -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());
}