From 5a7590547662a60861c5b00e80be79ef4215ff0b Mon Sep 17 00:00:00 2001 From: Jens Humrich Date: Wed, 16 Sep 2020 17:59:39 +0200 Subject: [PATCH] Add additional-suffix option to split --- src/uu/split/src/split.rs | 23 ++++++++++++++++++++--- tests/by-util/test_split.rs | 12 ++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index bba5cea39..727bc3f04 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -43,6 +43,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 { "numeric-suffixes", "use numeric suffixes instead of alphabetic", ); + opts.optopt( + "", + "additional-suffix", + "additional suffix to append to output file names", + "SUFFIX", + ); opts.optopt("l", "lines", "put NUMBER lines per output file", "NUMBER"); opts.optflag( "", @@ -86,6 +92,7 @@ size is 1000, and default PREFIX is 'x'. With no INPUT, or when INPUT is prefix: "".to_owned(), numeric_suffix: false, suffix_length: 0, + additional_suffix: "".to_owned(), input: "".to_owned(), strategy: "".to_owned(), strategy_param: "".to_owned(), @@ -102,6 +109,12 @@ size is 1000, and default PREFIX is 'x'. With no INPUT, or when INPUT is None => 2, }; + settings.additional_suffix = if matches.opt_present("additional-suffix") { + matches.opt_str("additional-suffix").unwrap() + } else { + "".to_owned() + }; + settings.verbose = matches.opt_present("verbose"); settings.strategy = "l".to_owned(); @@ -134,6 +147,7 @@ struct Settings { prefix: String, numeric_suffix: bool, suffix_length: usize, + additional_suffix: String, input: String, strategy: String, strategy_param: String, @@ -316,11 +330,14 @@ fn split(settings: &Settings) -> i32 { let mut filename = settings.prefix.clone(); filename.push_str( if settings.numeric_suffix { - num_prefix(fileno, settings.suffix_length) + num_prefix(fileno, settings.suffix_length) } else { str_prefix(fileno, settings.suffix_length) - } - .as_ref(), + }.as_ref(), + ); + filename.push_str( + settings.additional_suffix + .as_ref() ); if fileno != 0 { diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index 69a15d807..4dce1dd28 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -145,3 +145,15 @@ fn test_split_str_prefixed_chunks_by_lines() { assert_eq!(glob.count(), 10); assert_eq!(glob.collate(), at.read(name).into_bytes()); } + +#[test] +fn test_split_additional_suffix() { + let (at, mut ucmd) = at_and_ucmd!(); + let name = "split_additional_suffix"; + let glob = Glob::new(&at, ".", r"x[[:alpha:]][[:alpha:]].txt$"); + RandomFile::new(&at, name).add_lines(2000); + ucmd.args(&["--additional-suffix", ".txt", name]).succeeds(); + assert_eq!(glob.count(), 2); + assert_eq!(glob.collate(), at.read(name).into_bytes()); +} +