From 1235d516a5b50e4872b172b7dd94c9bc92cc8920 Mon Sep 17 00:00:00 2001 From: Justin Ma Date: Sat, 19 Feb 2022 00:19:37 +0800 Subject: [PATCH] Add examples for env,let-env,rm,touch and date list-timezone (#4531) * feat: update #4518, add examples for env,let-env,rm,touch and date list-timezone * fix typo * update example for `date list-timezone` command --- crates/nu-command/src/date/list_timezone.rs | 19 +++++++++++++- crates/nu-command/src/env/env_command.rs | 22 +++++++++++++++- crates/nu-command/src/env/let_env.rs | 10 ++++++- crates/nu-command/src/filesystem/rm.rs | 29 +++++++++++++++++++-- crates/nu-command/src/filesystem/touch.rs | 17 +++++++++++- 5 files changed, 91 insertions(+), 6 deletions(-) diff --git a/crates/nu-command/src/date/list_timezone.rs b/crates/nu-command/src/date/list_timezone.rs index 1114398174..2d9b0373e8 100644 --- a/crates/nu-command/src/date/list_timezone.rs +++ b/crates/nu-command/src/date/list_timezone.rs @@ -1,7 +1,9 @@ use chrono_tz::TZ_VARIANTS; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Category, IntoInterruptiblePipelineData, PipelineData, Signature, Value}; +use nu_protocol::{ + Category, Example, IntoInterruptiblePipelineData, PipelineData, Signature, Span, Value, +}; #[derive(Clone)] pub struct SubCommand; @@ -41,4 +43,19 @@ impl Command for SubCommand { .into_iter() .into_pipeline_data(engine_state.ctrlc.clone())) } + + fn examples(&self) -> Vec { + vec![Example { + example: "date list-timezone | where timezone =~ Shanghai", + description: "Show timezone(s) that contains 'Shanghai'", + result: Some(Value::List { + vals: vec![Value::Record { + cols: vec!["timezone".into()], + vals: vec![Value::test_string("Asia/Shanghai")], + span: Span::test_data(), + }], + span: Span::test_data(), + }), + }] + } } diff --git a/crates/nu-command/src/env/env_command.rs b/crates/nu-command/src/env/env_command.rs index 5d1f34fa8f..4dab284b24 100644 --- a/crates/nu-command/src/env/env_command.rs +++ b/crates/nu-command/src/env/env_command.rs @@ -1,7 +1,7 @@ use nu_engine::env_to_string; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Category, IntoPipelineData, PipelineData, Signature, Value}; +use nu_protocol::{Category, Example, IntoPipelineData, PipelineData, Signature, Value}; #[derive(Clone)] pub struct Env; @@ -59,4 +59,24 @@ impl Command for Env { Ok(Value::List { vals: values, span }.into_pipeline_data()) } + + fn examples(&self) -> Vec { + vec![ + Example { + description: "Display current path environment variable", + example: "env | where name == PATH", + result: None, + }, + Example { + description: "Check whether the env variable `MY_ENV_ABC` exists", + example: r#"env | any? name == MY_ENV_ABC"#, + result: Some(Value::test_bool(false)), + }, + Example { + description: "Another way to check whether the env variable `PATH` exists", + example: r#"'PATH' in (env).name"#, + result: Some(Value::test_bool(true)), + }, + ] + } } diff --git a/crates/nu-command/src/env/let_env.rs b/crates/nu-command/src/env/let_env.rs index a11ae0cc43..1bab91b8c9 100644 --- a/crates/nu-command/src/env/let_env.rs +++ b/crates/nu-command/src/env/let_env.rs @@ -1,7 +1,7 @@ use nu_engine::{current_dir, eval_expression_with_input, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Category, PipelineData, Signature, SyntaxShape, Value}; +use nu_protocol::{Category, Example, PipelineData, Signature, SyntaxShape, Value}; #[derive(Clone)] pub struct LetEnv; @@ -58,4 +58,12 @@ impl Command for LetEnv { } Ok(PipelineData::new(call.head)) } + + fn examples(&self) -> Vec { + vec![Example { + description: "Create an environment variable and display it", + example: "let-env MY_ENV_VAR = 1; $env.MY_ENV_VAR", + result: Some(Value::test_int(1)), + }] + } } diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index dc6f1c0520..7caeb1329c 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -12,8 +12,8 @@ use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - Category, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span, Spanned, - SyntaxShape, Type, Value, + Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span, + Spanned, SyntaxShape, Type, Value, }; const GLOB_PARAMS: glob::MatchOptions = glob::MatchOptions { @@ -67,6 +67,31 @@ impl Command for Rm { ) -> Result { rm(engine_state, stack, call) } + + fn examples(&self) -> Vec { + vec![ + Example { + description: "Delete or move a file to the system trash (depending on 'rm_always_trash' config option)", + example: "rm file.txt", + result: None, + }, + Example { + description: "Move a file to the system trash", + example: "rm --trash file.txt", + result: None, + }, + Example { + description: "Delete a file permanently", + example: "rm --permanent file.txt", + result: None, + }, + Example { + description: "Delete a file, and suppress errors if no file is found", + example: "rm --force file.txt", + result: None, + } + ] + } } fn rm( diff --git a/crates/nu-command/src/filesystem/touch.rs b/crates/nu-command/src/filesystem/touch.rs index ece811c976..061b0f5860 100644 --- a/crates/nu-command/src/filesystem/touch.rs +++ b/crates/nu-command/src/filesystem/touch.rs @@ -3,7 +3,7 @@ use std::fs::OpenOptions; use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape}; +use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape}; #[derive(Clone)] pub struct Touch; @@ -52,4 +52,19 @@ impl Command for Touch { Ok(PipelineData::new(call.head)) } + + fn examples(&self) -> Vec { + vec![ + Example { + description: "Creates \"fixture.json\"", + example: "touch fixture.json", + result: None, + }, + Example { + description: "Creates files a, b and c", + example: "touch a b c", + result: None, + }, + ] + } }