Add --ignore-error to reject (#10737)

Add `--ignore-errors` flag to reject.

This is a PR in reference to #10215 as select has the flag, but reject
hasn't

user can now add `-i` or `--ignore-errors` flag to turn every cell path
into option.

```nushell
> let arg = [0 5 a c]
> [[a b];[1 2] [3 4] [5 6]] | reject $a | to nuon
error index to large
# ----
> let arg = [0 5 a c]
> [[a b];[1 2] [3 4] [5 6]] | reject $a -i | to nuon
[[a, b]; [1, 2], [3, 4], [5, 6]]
```
This commit is contained in:
Tilen Gimpelj 2023-10-19 00:28:47 +02:00 committed by GitHub
parent d204defb68
commit 9692240b4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -22,6 +22,11 @@ impl Command for Reject {
(Type::Record(vec![]), Type::Record(vec![])),
(Type::Table(vec![]), Type::Table(vec![])),
])
.switch(
"ignore-errors",
"ignore missing data (make all cell path members optional)",
Some('i'),
)
.rest(
"rest",
SyntaxShape::OneOf(vec![
@ -126,6 +131,14 @@ impl Command for Reject {
}
}
let span = call.head;
let ignore_errors = call.has_flag("ignore-errors");
if ignore_errors {
for cell_path in &mut new_columns {
cell_path.make_optional();
}
}
reject(engine_state, span, input, new_columns)
}

View File

@ -185,3 +185,16 @@ fn reject_multiple_rows_descending() {
let actual = nu!("[[a,b];[1 2] [3 4] [5 6]] | reject 2 1 | to nuon");
assert_eq!(actual.out, "[[a, b]; [1, 2]]");
}
#[test]
fn test_ignore_errors_flag() {
let actual = nu!("[[a, b]; [1, 2], [3, 4], [5, 6]] | reject 5 -i | to nuon");
assert_eq!(actual.out, "[[a, b]; [1, 2], [3, 4], [5, 6]]");
}
#[test]
fn test_ignore_errors_flag_var() {
let actual =
nu!("let arg = [5 c]; [[a, b]; [1, 2], [3, 4], [5, 6]] | reject $arg -i | to nuon");
assert_eq!(actual.out, "[[a, b]; [1, 2], [3, 4], [5, 6]]");
}