forked from extern/nushell
Add -i
flag back to get
and select
(#8488)
https://github.com/nushell/nushell/pull/8379 removed the `-i` flag from `get` and `select` because the new `?` functionality covers most of the same use cases. However, https://github.com/nushell/nushell/issues/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.
This commit is contained in:
parent
d74a260883
commit
1b2916988e
@ -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",
|
||||
@ -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)
|
||||
|
@ -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,
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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]]");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user