From 2f1016d44f9ff3094d12686310bd8d61f95ca7ae Mon Sep 17 00:00:00 2001 From: Chris Gillespie <6572184+gillespiecd@users.noreply.github.com> Date: Thu, 1 Oct 2020 18:13:42 -0700 Subject: [PATCH] Add examples to update cmd (#2628) --- crates/nu-cli/src/commands/rename.rs | 19 ++++++++++++++----- crates/nu-cli/src/commands/update.rs | 13 +++++++++++++ docs/commands/update.md | 9 +++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/crates/nu-cli/src/commands/rename.rs b/crates/nu-cli/src/commands/rename.rs index 9539870b3..51d95715a 100644 --- a/crates/nu-cli/src/commands/rename.rs +++ b/crates/nu-cli/src/commands/rename.rs @@ -1,6 +1,6 @@ use crate::commands::WholeStreamCommand; use crate::prelude::*; -use indexmap::IndexMap; +use indexmap::indexmap; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; @@ -45,13 +45,22 @@ impl WholeStreamCommand for Rename { vec![ Example { description: "Rename a column", - example: r#"echo "{a: 1, b: 2, c: 3}" | from json | rename my_column"#, - result: None, + example: "echo [[a, b]; [1, 2]] | rename my_column", + result: Some(vec![UntaggedValue::row(indexmap! { + "my_column".to_string() => UntaggedValue::int(1).into(), + "b".to_string() => UntaggedValue::int(2).into(), + }) + .into()]), }, Example { description: "Rename many columns", - example: r#"echo "{a: 1, b: 2, c: 3}" | from json | rename spam eggs cars"#, - result: None, + example: "echo [[a, b, c]; [1, 2, 3]] | rename eggs ham bacon", + result: Some(vec![UntaggedValue::row(indexmap! { + "eggs".to_string() => UntaggedValue::int(1).into(), + "ham".to_string() => UntaggedValue::int(2).into(), + "bacon".to_string() => UntaggedValue::int(3).into(), + }) + .into()]), }, ] } diff --git a/crates/nu-cli/src/commands/update.rs b/crates/nu-cli/src/commands/update.rs index 3983d976e..6539b677e 100644 --- a/crates/nu-cli/src/commands/update.rs +++ b/crates/nu-cli/src/commands/update.rs @@ -2,6 +2,7 @@ use crate::command_registry::CommandRegistry; use crate::commands::classified::block::run_block; use crate::commands::WholeStreamCommand; use crate::prelude::*; +use indexmap::indexmap; use nu_errors::ShellError; use nu_protocol::{ ColumnPath, Primitive, ReturnSuccess, Scope, Signature, SyntaxShape, UntaggedValue, Value, @@ -49,6 +50,18 @@ impl WholeStreamCommand for Update { ) -> Result { update(args, registry).await } + + fn examples(&self) -> Vec { + vec![Example { + description: "Update a column value", + example: "echo [[a, b]; [1, 2]] | update a 'nu'", + result: Some(vec![UntaggedValue::row(indexmap! { + "a".to_string() => Value::from("nu"), + "b".to_string() => UntaggedValue::int(2).into(), + }) + .into()]), + }] + } } async fn process_row( diff --git a/docs/commands/update.md b/docs/commands/update.md index 8d9e07207..1847b3a3b 100644 --- a/docs/commands/update.md +++ b/docs/commands/update.md @@ -49,3 +49,12 @@ Updates an existing column on a table. First parameter is the column to update a 1 │ X │ filesystem │ / ━━━┷━━━┷━━━━━━━━━━━━┷━━━━━━ ``` + +Collect all the values of a nested column and join them together +```shell +> version | update features {get features | str collect ', '} +───┬─────────┬──────────────────────────────────────────┬─────────────────────────── + # │ version │ commit_hash │ features +───┼─────────┼──────────────────────────────────────────┼─────────────────────────── + 0 │ 0.20.0 │ fdab3368094e938c390f1e5a7892a42da45add3e │ default, clipboard, trash +───┴─────────┴──────────────────────────────────────────┴───────────────────────────