Add or update examples for some commands (#4521)

* chore: add or update examples for some commands

* chore: code formatting
This commit is contained in:
Justin Ma 2022-02-18 21:06:52 +08:00 committed by GitHub
parent 1377693f0f
commit a5f9ad2a43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 89 additions and 9 deletions

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape};
use nu_protocol::{Category, Example, PipelineData, Signature, SyntaxShape};
#[derive(Clone)]
pub struct Alias;
@ -34,4 +34,12 @@ impl Command for Alias {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Alias ll to ls -l",
example: "alias ll = ls -l",
result: None,
}]
}
}

View File

@ -61,6 +61,11 @@ impl Command for Let {
example: "let x = 10 + 100",
result: None,
},
Example {
description: "Set a variable based on the condition",
example: "let x = if $false { -1 } else { 1 }",
result: None,
},
]
}
}

View File

@ -1,7 +1,7 @@
use chrono::Local;
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 SubCommand;
@ -33,4 +33,12 @@ impl Command for SubCommand {
}
.into_pipeline_data())
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Get the current date and display it in a given format string.",
example: r#"date now | date format "%Y-%m-%d %H:%M:%S""#,
result: None,
}]
}
}

View File

@ -48,6 +48,10 @@ 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: "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 ',' }",

View File

@ -1,7 +1,7 @@
use nu_engine::{eval_block_with_redirect, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape};
use nu_protocol::{Category, Example, PipelineData, Signature, SyntaxShape};
#[derive(Clone)]
pub struct Where;
@ -63,4 +63,29 @@ impl Command for Where {
)
.map(|x| x.set_metadata(metadata))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "List all files in the current directory with sizes greater than 2kb",
example: "ls | where size > 2kb",
result: None,
},
Example {
description: "List only the files in the current directory",
example: "ls | where type == file",
result: None,
},
Example {
description: "List all files with names that contain \"Car\"",
example: "ls | where name =~ \"Car\"",
result: None,
},
Example {
description: "List all files that were modified in the last two weeks",
example: "ls | where modified <= 2wk",
result: None,
},
]
}
}

View File

@ -2,8 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Signature,
SyntaxShape, Value,
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Signature,
Span, SyntaxShape, Value,
};
#[derive(Clone)]
@ -64,4 +64,19 @@ impl Command for Wrap {
.into_pipeline_data()),
}
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Wrap a list into a table with a given column name",
example: "echo [1 2 3] | wrap num",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
span: Span::test_data(),
}],
span: Span::test_data(),
}),
}]
}
}

View File

@ -1,7 +1,7 @@
use nu_engine::{current_dir, CallExt};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
/// Source a file for environment variables.
#[derive(Clone)]
@ -97,4 +97,19 @@ impl Command for Exit {
Ok(PipelineData::new(call.head))
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Exit the current shell",
example: "exit",
result: None,
},
Example {
description: "Exit all shells (exiting Nu)",
example: "exit --now",
result: None,
},
]
}
}

View File

@ -76,16 +76,16 @@ fn test_no_color_flag() {
}
#[test]
fn test_html_color_where_flag_dark_false() {
fn test_html_color_cd_flag_dark_false() {
let actual = nu!(
cwd: ".", pipeline(
r#"
where --help | to html --html-color
cd --help | to html --html-color
"#
)
);
assert_eq!(
actual.out,
r"<html><style>body { background-color:white;color:black; }</style><body>Filter values based on a condition.<br><br>Usage:<br> &gt; where &lt;cond&gt; <br><br>Flags:<br> -h, --help<br> Display this help message<br><br>Parameters:<br> cond: condition<br><br></body></html>"
r"<html><style>body { background-color:white;color:black; }</style><body>Change directory.<br><br>Usage:<br> &gt; cd (path) <br><br>Flags:<br> -h, --help<br> Display this help message<br><br>Parameters:<br> (optional) path: the path to change to<br><br></body></html>"
);
}