From 87b3a68038eb4d98a8e22e717afbf17b61b4b5bc Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Tue, 3 Sep 2024 07:39:19 -0400 Subject: [PATCH] `fixlengths`: added `--quote` and `--escape` options resolves #2103 --- src/cmd/fixlengths.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cmd/fixlengths.rs b/src/cmd/fixlengths.rs index a119d255d..6cdefa91b 100644 --- a/src/cmd/fixlengths.rs +++ b/src/cmd/fixlengths.rs @@ -24,6 +24,9 @@ fixlengths options: is inserted from the END of each record going backwards. If is positive, it is inserted from the BEGINNING of each record going forward. [default: 0] + --quote The quote character to use. [default: "] + --escape The escape character to use. When not specified, + quotes are escaped by doubling them. Common options: -h, --help Display this message @@ -46,22 +49,32 @@ struct Args { arg_input: Option, flag_length: Option, flag_insert: i16, + flag_quote: Delimiter, + flag_escape: Option, flag_output: Option, flag_delimiter: Option, } pub fn run(argv: &[&str]) -> CliResult<()> { let args: Args = util::get_args(USAGE, argv)?; - let config = Config::new(&args.arg_input) + let mut config = Config::new(&args.arg_input) .delimiter(args.flag_delimiter) + .quote(args.flag_quote.as_byte()) .no_headers(true) .flexible(true); + + if let Some(escape) = args.flag_escape { + config = config.escape(Some(escape.as_byte())).double_quote(false); + } + let length = if let Some(length) = args.flag_length { if length == 0 { return fail_incorrectusage_clierror!("Length must be greater than 0."); } length } else { + // --length not set, so we need to determine the length of the longest record + // by scanning the entire file if config.is_stdin() { return fail_incorrectusage_clierror!( " cannot be used in this command. Please specify a file path." @@ -94,7 +107,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> { } else { args.flag_insert }; - log::debug!("length: {length} insert_pos: {insert_pos}"); + // log::debug!("length: {length} insert_pos: {insert_pos}"); while rdr.read_byte_record(&mut record)? { if length >= record.len() { @@ -122,7 +135,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> { record_work.push_field(b""); } } - record = record_work.clone(); + record.clone_from(&record_work); } } else { record.truncate(length);