feat: update: #4518, Add example for register,source,save,shuffle and from tsv (#4577)

This commit is contained in:
Justin Ma 2022-02-21 20:25:41 +08:00 committed by GitHub
parent d454fad4dc
commit 968427c4e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 6 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 Register;
@ -50,4 +50,19 @@ impl Command for Register {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head))
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Register `nu_plugin_extra_query` plugin from ~/.cargo/bin/ dir",
example: r#"register -e capnp ~/.cargo/bin/nu_plugin_extra_query"#,
result: None,
},
Example {
description: "Register `nu_plugin_extra_query` plugin from `nu -c`(plugin will be available in that nu session only)",
example: r#"let plugin = ((which nu).path.0 | path dirname | path join 'nu_plugin_extra_query'); nu -c $'register -e capnp ($plugin); version'"#,
result: None,
},
]
}
}

View File

@ -1,7 +1,7 @@
use nu_engine::{eval_block, 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};
/// Source a file for environment variables.
#[derive(Clone)]
@ -40,4 +40,24 @@ impl Command for Source {
let block = engine_state.get_block(block_id as usize).clone();
eval_block(engine_state, stack, &block, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Runs foo.nu in the current context",
example: r#"source foo.nu"#,
result: None,
},
Example {
description: "Runs foo.nu in current context and call the command defined, suppose foo.nu has content: `def say-hi [] { echo 'Hi!' }`",
example: r#"source ./foo.nu; say-hi"#,
result: None,
},
Example {
description: "Runs foo.nu in current context and call the `main` command automatically, suppose foo.nu has content: `def main [] { echo 'Hi!' }`",
example: r#"source ./foo.nu"#,
result: None,
},
]
}
}

View File

@ -1,7 +1,9 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value,
};
use std::io::Write;
use std::path::Path;
@ -21,7 +23,7 @@ impl Command for Save {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("save")
.required("filename", SyntaxShape::Filepath, "the filename to use")
.switch("raw", "open file as raw binary", Some('r'))
.switch("raw", "save file as raw binary", Some('r'))
.category(Category::FileSystem)
}
@ -115,4 +117,19 @@ impl Command for Save {
}
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Save a string to foo.txt in current directory",
example: r#"echo 'save me' | save foo.txt"#,
result: None,
},
Example {
description: "Save a record to foo.json in current directory",
example: r#"echo { a: 1, b: 2 } | save foo.json"#,
result: None,
},
]
}
}

View File

@ -1,6 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
};
use rand::prelude::SliceRandom;
use rand::thread_rng;
@ -32,4 +34,13 @@ impl Command for Shuffle {
let iter = v.into_iter();
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description:
"Shuffle rows randomly(execute it a few more times and see the difference)",
example: r#"echo [[version patch]; [1.0.0 $false] [3.0.1 $true] [2.0.0 $false]] | shuffle"#,
result: None,
}]
}
}

View File

@ -2,7 +2,7 @@ use super::delimited::from_delimited_data;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Config, PipelineData, ShellError, Signature};
use nu_protocol::{Category, Config, Example, PipelineData, ShellError, Signature};
#[derive(Clone)]
pub struct FromTsv;
@ -36,6 +36,21 @@ impl Command for FromTsv {
let config = stack.get_config().unwrap_or_default();
from_tsv(call, input, &config)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Create a tsv file with header columns and open it",
example: r#"echo $'c1(char tab)c2(char tab)c3(char nl)1(char tab)2(char tab)3' | save tsv-data | open tsv-data | from tsv"#,
result: None,
},
Example {
description: "Create a tsv file without header columns and open it",
example: r#"echo $'a1(char tab)b1(char tab)c1(char nl)a2(char tab)b2(char tab)c2' | save tsv-data | open tsv-data | from tsv -n"#,
result: None,
},
]
}
}
fn from_tsv(call: &Call, input: PipelineData, config: &Config) -> Result<PipelineData, ShellError> {