diff --git a/crates/nu-command/src/filters/update.rs b/crates/nu-command/src/filters/update.rs index a70a17e50b..f7c6a85a35 100644 --- a/crates/nu-command/src/filters/update.rs +++ b/crates/nu-command/src/filters/update.rs @@ -19,7 +19,7 @@ impl Command for Update { .required( "field", SyntaxShape::CellPath, - "the name of the column to update", + "the name of the column to update or create", ) .required( "replacement value", @@ -30,7 +30,7 @@ impl Command for Update { } fn usage(&self) -> &str { - "Update an existing column to have a new value." + "Update an existing column to have a new value, or create a new column." } fn run( @@ -48,13 +48,17 @@ impl Command for Update { description: "Update a column value", example: "echo {'name': 'nu', 'stars': 5} | update name 'Nushell'", result: Some(Value::Record { cols: vec!["name".into(), "stars".into()], vals: vec![Value::test_string("Nushell"), Value::test_int(5)], span: Span::test_data()}), + }, Example { + description: "Add a new column", + example: "echo {'name': 'nu', 'stars': 5} | update language 'Rust'", + result: Some(Value::Record { cols: vec!["name".into(), "stars".into(), "language".into()], vals: vec![Value::test_string("nu"), Value::test_int(5), Value::test_string("Rust")], span: Span::test_data()}), }, Example { description: "Use in block form for more involved updating logic", example: "echo [[count fruit]; [1 'apple']] | update count {|f| $f.count + 1}", result: Some(Value::List { vals: vec![Value::Record { cols: vec!["count".into(), "fruit".into()], vals: vec![Value::test_int(2), Value::test_string("apple")], span: Span::test_data()}], span: Span::test_data()}), }, Example { description: "Use in block form for more involved updating logic", - example: "echo [[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors { get authors | str collect ',' }", + example: "echo [[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|a| $a.authors | str collect ','}", result: Some(Value::List { vals: vec![Value::Record { cols: vec!["project".into(), "authors".into()], vals: vec![Value::test_string("nu"), Value::test_string("Andrés,JT,Yehuda")], span: Span::test_data()}], span: Span::test_data()}), }] } @@ -135,3 +139,15 @@ fn update( ) } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_examples() { + use crate::test_examples; + + test_examples(Update {}) + } +}