Skip to content

Commit

Permalink
Add -i flag back to get and select (#8488)
Browse files Browse the repository at this point in the history
#8379 removed the `-i` flag from
`get` and `select` because the new `?` functionality covers most of the
same use cases. However, #8480
made me realize that `-i` is still useful when dealing with cell paths
in variables.

This PR re-adds the `-i` flag to `get` and `select`. It works by just
marking every member in the cell path as optional, which will behave
_slightly_ differently than `-i` used to (previously it would suppress
any errors, even type errors) but IMO that's OK.
  • Loading branch information
rgwood authored Mar 16, 2023
1 parent d74a260 commit 1b29169
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
12 changes: 11 additions & 1 deletion crates/nu-command/src/filters/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ If multiple cell paths are given, this will produce a list of values."#
"the cell path to the data",
)
.rest("rest", SyntaxShape::CellPath, "additional cell paths")
.switch(
"ignore-errors",
"ignore missing data (make all cell path members optional)",
Some('i'),
)
.switch(
"sensitive",
"get path in a case sensitive manner",
Expand All @@ -57,12 +62,17 @@ If multiple cell paths are given, this will produce a list of values."#
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let span = call.head;
let cell_path: CellPath = call.req(engine_state, stack, 0)?;
let mut cell_path: CellPath = call.req(engine_state, stack, 0)?;
let rest: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let ignore_errors = call.has_flag("ignore-errors");
let sensitive = call.has_flag("sensitive");
let ctrlc = engine_state.ctrlc.clone();
let metadata = input.metadata();

if ignore_errors {
cell_path.make_optional();
}

if rest.is_empty() {
input
.follow_cell_path(&cell_path.members, call.head, !sensitive)
Expand Down
14 changes: 13 additions & 1 deletion crates/nu-command/src/filters/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ impl Command for Select {
(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::CellPath,
Expand Down Expand Up @@ -51,9 +56,16 @@ produce a table, a list will produce a list, and a record will produce a record.
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let columns: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let mut columns: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let ignore_errors = call.has_flag("ignore-errors");
let span = call.head;

if ignore_errors {
for cell_path in &mut columns {
cell_path.make_optional();
}
}

select(engine_state, span, columns, input)
}

Expand Down
13 changes: 13 additions & 0 deletions crates/nu-command/tests/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,16 @@ fn get_does_not_delve_too_deep_in_nested_lists() {

assert!(actual.err.contains("cannot find column"));
}

#[test]
fn ignore_errors_works() {
let actual = nu!(
cwd: ".",
r#"
let path = "foo";
{} | get -i $path | to nuon
"#
);

assert_eq!(actual.out, "null");
}
13 changes: 13 additions & 0 deletions crates/nu-command/tests/commands/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,16 @@ fn select_failed4() {

assert!(actual.err.contains("Select can't get the same row twice"));
}

#[test]
fn ignore_errors_works() {
let actual = nu!(
cwd: ".",
r#"
let path = "foo";
[{}] | select -i $path | to nuon
"#
);

assert_eq!(actual.out, "[[foo]; [null]]");
}

0 comments on commit 1b29169

Please sign in to comment.