passing numeric-start should imply using numeric suffixes
This commit is contained in:
parent
107e692081
commit
5caa52811c
21
src/main.rs
21
src/main.rs
@ -42,9 +42,9 @@ struct Options {
|
|||||||
#[argh(switch, short = 'd')]
|
#[argh(switch, short = 'd')]
|
||||||
numeric: bool,
|
numeric: bool,
|
||||||
|
|
||||||
/// use numeric suffixes starting with 0, not alphabetic
|
/// use numeric suffixes starting with this value
|
||||||
#[argh(option, long = "numeric-start", default = "0")]
|
#[argh(option, long = "numeric-start")]
|
||||||
numeric_start: usize,
|
numeric_start: Option<usize>,
|
||||||
|
|
||||||
/// filename to read from, or "-" for stdin (default "-")
|
/// filename to read from, or "-" for stdin (default "-")
|
||||||
#[argh(positional, default = "String::from(\"-\")")]
|
#[argh(positional, default = "String::from(\"-\")")]
|
||||||
@ -94,6 +94,7 @@ impl Iterator for AlphabeticSuffixGenerator {
|
|||||||
fn try_main() -> Result<(), Box<dyn Error>> {
|
fn try_main() -> Result<(), Box<dyn Error>> {
|
||||||
let opts: Options = argh::from_env();
|
let opts: Options = argh::from_env();
|
||||||
|
|
||||||
|
// open source file
|
||||||
let input: Box<dyn Read> = match opts.filename.as_str() {
|
let input: Box<dyn Read> = match opts.filename.as_str() {
|
||||||
"-" => Box::new(io::stdin()),
|
"-" => Box::new(io::stdin()),
|
||||||
path => match File::open(path) {
|
path => match File::open(path) {
|
||||||
@ -101,22 +102,30 @@ fn try_main() -> Result<(), Box<dyn Error>> {
|
|||||||
Ok(file) => Box::new(file),
|
Ok(file) => Box::new(file),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
let mut reader = BufReader::new(input);
|
||||||
|
|
||||||
|
// build prefix format
|
||||||
let prefix = match opts.prefix.as_str() {
|
let prefix = match opts.prefix.as_str() {
|
||||||
"" => String::from(""),
|
"" => String::from(""),
|
||||||
_ => opts.prefix + "_",
|
_ => 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(
|
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 {
|
} else {
|
||||||
Box::new(AlphabeticSuffixGenerator::new(opts.count))
|
Box::new(AlphabeticSuffixGenerator::new(opts.count))
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut eof = false;
|
let mut eof = false;
|
||||||
let mut reader = BufReader::new(input);
|
|
||||||
for suffix in suffix_gen {
|
for suffix in suffix_gen {
|
||||||
let mut out_file = File::create(format!("{}{}", prefix, suffix))?;
|
let mut out_file = File::create(format!("{}{}", prefix, suffix))?;
|
||||||
let mut line = String::new();
|
let mut line = String::new();
|
||||||
|
Loading…
Reference in New Issue
Block a user