passing numeric-start should imply using numeric suffixes

This commit is contained in:
Mike Cugini 2021-03-16 11:39:22 -04:00
parent 107e692081
commit 5caa52811c

View File

@ -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<usize>,
/// 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<dyn Error>> {
let opts: Options = argh::from_env();
// open source file
let input: Box<dyn Read> = match opts.filename.as_str() {
"-" => Box::new(io::stdin()),
path => match File::open(path) {
@ -101,22 +102,30 @@ fn try_main() -> Result<(), Box<dyn Error>> {
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<dyn Iterator<Item = String>> = 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<dyn Iterator<Item = String>> = 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();