From 337beaa8d002401d3295ce40673bfd1a58bc77cf Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Thu, 29 Feb 2024 09:02:10 -0500 Subject: [PATCH] `enum`: add `--start` option resolves #1630 --- src/cmd/enumerate.rs | 5 ++++- tests/test_enumerate.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/cmd/enumerate.rs b/src/cmd/enumerate.rs index 3cb620def..4448c493c 100644 --- a/src/cmd/enumerate.rs +++ b/src/cmd/enumerate.rs @@ -28,6 +28,8 @@ Usage: enum options: -c, --new-column Name of the column to create. Will default to "index". + --start The value to start the enumeration from. + (default: 0) --constant Fill a new column with the given value. Changes the default column name to "constant". To specify a null value, pass the literal "". @@ -61,6 +63,7 @@ const NULL_VALUE: &str = ""; struct Args { arg_input: Option, flag_new_column: Option, + flag_start: u64, flag_constant: Option, flag_copy: Option, flag_uuid: bool, @@ -111,7 +114,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> { } let mut record = csv::ByteRecord::new(); - let mut counter: u64 = 0; + let mut counter: u64 = args.flag_start; while rdr.read_byte_record(&mut record)? { if let Some(constant_value) = &args.flag_constant { diff --git a/tests/test_enumerate.rs b/tests/test_enumerate.rs index 6ddde40e6..966152de4 100644 --- a/tests/test_enumerate.rs +++ b/tests/test_enumerate.rs @@ -27,6 +27,33 @@ fn enumerate() { assert_eq!(got, expected); } +#[test] +fn enumerate_counter() { + let wrk = Workdir::new("enumerate_counter"); + wrk.create( + "data.csv", + vec![ + svec!["letter", "number"], + svec!["a", "13"], + svec!["b", "24"], + svec!["c", "72"], + svec!["d", "7"], + ], + ); + let mut cmd = wrk.command("enum"); + cmd.args(&["--start", "10"]).arg("data.csv"); + + let got: Vec> = wrk.read_stdout(&mut cmd); + let expected = vec![ + svec!["letter", "number", "index"], + svec!["a", "13", "10"], + svec!["b", "24", "11"], + svec!["c", "72", "12"], + svec!["d", "7", "13"], + ]; + assert_eq!(got, expected); +} + #[test] fn enumerate_column_name() { let wrk = Workdir::new("enum");