use clap::Parser; use serde::Deserialize; #[derive(Parser)] #[command(name = env!("CARGO_PKG_NAME"))] #[command(version)] #[command(about = "Generate random messages with customizable word lists.")] pub struct Args { /// Specify configuration directory #[arg(short = 'c', long)] pub configdir: Option, /// Write output to file #[arg(short = 'o', long)] pub output: Option, /// Add an adjective #[arg(short = 'a', long)] pub adjective: Option>, /// Add a noun #[arg(short = 'n', long)] pub noun: Option>, /// Add a verb #[arg(short = 'v', long)] pub verb: Option>, /// Add an adverb #[arg(short = 'd', long)] pub adverb: Option>, /// Add a location #[arg(short = 'l', long)] pub location: Option>, /// Custom override template: "{(n)oun} {(a)djective} {(v)erb} {a(d)verb} {(l)ocation}" #[arg(short = 't', long)] pub template: Option, /// Replace default lists instead of extending #[arg(short = 'r', long)] pub replace: bool, /// Lowercase output #[arg(short = 'L', long)] pub lowercase: bool, /// Capitalize output #[arg(short = 'U', long)] pub uppercase: bool, /// String to strip #[arg(short = 's', long)] pub strip: Option>, /// Delimiter #[arg(short = 'D', long)] pub delimiter: Option, /// Cut-off #[arg(short = 'C', long)] pub cut_off: Option, /// Dump default lists #[arg(short = 'p', long, value_name = "WORD_TYPE")] pub print: Option>, /// Outputs shell completion for the given shell #[arg(long, value_name = "SHELL")] pub completion: Option, } #[derive(Deserialize, Default, Debug)] pub struct RcArgs { pub output: Option, pub adjective: Option>, pub noun: Option>, pub verb: Option>, pub adverb: Option>, pub location: Option>, pub template: Option, pub replace: Option, pub lowercase: Option, pub uppercase: Option, pub strip: Option>, pub delimiter: Option, pub cut_off: Option, } impl Args { pub fn merge_with_rc(mut self, rc_args: RcArgs) -> Self { // CLI args take precedence over RC file args // Only use RC file values if CLI values are None/false/default if self.output.is_none() { self.output = rc_args.output; } if self.adjective.is_none() { self.adjective = rc_args.adjective; } if self.noun.is_none() { self.noun = rc_args.noun; } if self.verb.is_none() { self.verb = rc_args.verb; } if self.adverb.is_none() { self.adverb = rc_args.adverb; } if self.location.is_none() { self.location = rc_args.location; } if self.template.is_none() { self.template = rc_args.template; } if !self.replace { self.replace = rc_args.replace.unwrap_or(false); } if !self.lowercase { self.lowercase = rc_args.lowercase.unwrap_or(false); } if !self.uppercase { self.uppercase = rc_args.uppercase.unwrap_or(false); } if self.strip.is_none() { self.strip = rc_args.strip; } if self.delimiter.is_none() { self.delimiter = rc_args.delimiter; } if self.cut_off.is_none() { self.cut_off = rc_args.cut_off; } self } }