mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Add fuzzy/ignore flag to get (#641)
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::{Call, CellPath};
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, IntoPipelineData, PipelineData, Signature, SyntaxShape};
|
||||
use nu_protocol::{Category, IntoPipelineData, PipelineData, Signature, SyntaxShape, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Get;
|
||||
@ -22,6 +22,11 @@ impl Command for Get {
|
||||
SyntaxShape::CellPath,
|
||||
"the cell path to the data",
|
||||
)
|
||||
.switch(
|
||||
"ignore-errors",
|
||||
"return nothing if path can't be found",
|
||||
Some('i'),
|
||||
)
|
||||
.category(Category::Filters)
|
||||
}
|
||||
|
||||
@ -33,9 +38,19 @@ impl Command for Get {
|
||||
input: PipelineData,
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let cell_path: CellPath = call.req(engine_state, stack, 0)?;
|
||||
let ignore_errors = call.has_flag("ignore-errors");
|
||||
|
||||
input
|
||||
let output = input
|
||||
.follow_cell_path(&cell_path.members, call.head)
|
||||
.map(|x| x.into_pipeline_data())
|
||||
.map(|x| x.into_pipeline_data());
|
||||
|
||||
if ignore_errors {
|
||||
match output {
|
||||
Ok(output) => Ok(output),
|
||||
Err(_) => Ok(Value::Nothing { span: call.head }.into_pipeline_data()),
|
||||
}
|
||||
} else {
|
||||
output
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user