From 5caa52811cf095df2562bad7b5185323ffadf8d3 Mon Sep 17 00:00:00 2001 From: Mike Cugini Date: Tue, 16 Mar 2021 11:39:22 -0400 Subject: [PATCH] passing numeric-start should imply using numeric suffixes --- src/main.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 135035f..98c006f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,9 +42,9 @@ struct Options { #[argh(switch, short = 'd')] numeric: bool, - /// use numeric suffixes starting with 0, not alphabetic - #[argh(option, long = "numeric-start", default = "0")] - numeric_start: usize, + /// use numeric suffixes starting with this value + #[argh(option, long = "numeric-start")] + numeric_start: Option, /// filename to read from, or "-" for stdin (default "-") #[argh(positional, default = "String::from(\"-\")")] @@ -94,6 +94,7 @@ impl Iterator for AlphabeticSuffixGenerator { fn try_main() -> Result<(), Box> { let opts: Options = argh::from_env(); + // open source file let input: Box = match opts.filename.as_str() { "-" => Box::new(io::stdin()), path => match File::open(path) { @@ -101,22 +102,30 @@ fn try_main() -> Result<(), Box> { Ok(file) => Box::new(file), }, }; + let mut reader = BufReader::new(input); + // build prefix format let prefix = match opts.prefix.as_str() { "" => String::from(""), _ => opts.prefix + "_", }; - let suffix_gen: Box> = if opts.numeric { + // use numeric suffixes if --numeric or --numeric-start are passed + let use_numeric = opts.numeric || opts.numeric_start.is_some(); + + let suffix_gen: Box> = if use_numeric { + let numeric_start = match opts.numeric_start { + Some(num) => num, + None => 0, + }; Box::new( - (opts.numeric_start..(opts.numeric_start + opts.count)).map(|num| format!("{}", num)), + (numeric_start..(numeric_start + opts.count)).map(|num| format!("{}", num)), ) } else { Box::new(AlphabeticSuffixGenerator::new(opts.count)) }; let mut eof = false; - let mut reader = BufReader::new(input); for suffix in suffix_gen { let mut out_file = File::create(format!("{}{}", prefix, suffix))?; let mut line = String::new();