Skip to content

Commit

Permalink
Merge pull request #1631 from jqnatividad/1630-enum_start_option
Browse files Browse the repository at this point in the history
`enum`: add `--start` option
  • Loading branch information
jqnatividad authored Feb 29, 2024
2 parents cc74cdb + 337beaa commit 7007ee3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/cmd/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Usage:
enum options:
-c, --new-column <name> Name of the column to create.
Will default to "index".
--start <value> The value to start the enumeration from.
(default: 0)
--constant <value> Fill a new column with the given value.
Changes the default column name to "constant".
To specify a null value, pass the literal "<NULL>".
Expand Down Expand Up @@ -61,6 +63,7 @@ const NULL_VALUE: &str = "<NULL>";
struct Args {
arg_input: Option<String>,
flag_new_column: Option<String>,
flag_start: u64,
flag_constant: Option<String>,
flag_copy: Option<SelectColumns>,
flag_uuid: bool,
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions tests/test_enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>> = 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");
Expand Down

0 comments on commit 7007ee3

Please sign in to comment.