Use typed arguments for default values

Signed-off-by: hr567 <hr567@hr567.me>
This commit is contained in:
hr567 2024-06-04 14:45:46 +08:00
parent 72574323ba
commit 7e8f4f8a00
Signed by: hr567
SSH Key Fingerprint: SHA256:AUrHz/3TKmc6tf4wqaaEPV/BGQ4aULSp7g76FpqujYw
2 changed files with 8 additions and 10 deletions

View File

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = { version = "4.5.4", features = ["derive", "string"] } clap = { version = "4.5.4", features = ["derive"] }
dashmap = { version = "5.5.3", features = ["serde"] } dashmap = { version = "5.5.3", features = ["serde"] }
futures = "0.3.30" futures = "0.3.30"
indicatif = "0.17.8" indicatif = "0.17.8"

View File

@ -2,6 +2,7 @@ use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap, VecDeque}; use std::collections::{BTreeMap, HashMap, VecDeque};
use std::env::current_dir; use std::env::current_dir;
use std::fmt::Write; use std::fmt::Write;
use std::fs::read_to_string;
use std::io; use std::io;
use std::path::PathBuf; use std::path::PathBuf;
use std::rc::{Rc, Weak}; use std::rc::{Rc, Weak};
@ -21,13 +22,11 @@ use tracing::Instrument;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
struct Args { struct Args {
#[arg(short, long, default_value = #[arg(short, long, default_value_os_t = current_dir().unwrap())]
current_dir().unwrap().into_os_string())]
directory: PathBuf, directory: PathBuf,
#[arg(short, long, default_value = "16")] #[arg(short, long, default_value_t = 16)]
jobs: usize, jobs: usize,
#[arg(short = 'f', long, default_value = #[arg(short = 'f', long, default_value_os_t = current_dir().unwrap().join("keywords.txt"))]
current_dir().unwrap().join("keywords.txt").into_os_string())]
keywords_file: PathBuf, keywords_file: PathBuf,
#[arg(last = true)] #[arg(last = true)]
keywords: Vec<String>, keywords: Vec<String>,
@ -35,10 +34,9 @@ struct Args {
static ARGS: Lazy<Args> = Lazy::new(|| { static ARGS: Lazy<Args> = Lazy::new(|| {
let mut args = Args::parse(); let mut args = Args::parse();
if args.keywords_file.exists() { if args.keywords.is_empty() && args.keywords_file.exists() {
let content = let content = read_to_string(&args.keywords_file).expect("failed to read keywords file");
std::fs::read_to_string(&args.keywords_file).expect("failed to read keywords file"); args.keywords = content.split_whitespace().map(|s| s.to_string()).collect();
args.keywords.extend(content.lines().map(|s| s.to_string()));
} }
args args
}); });