feat: update: #4518, Add examples for command: use,module,export def,export env and export def-env (#4584)

This commit is contained in:
Justin Ma 2022-02-21 23:32:31 +08:00 committed by GitHub
parent 07cd8f483e
commit 53b5012f1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 5 deletions

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape}; use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)] #[derive(Clone)]
pub struct ExportDef; pub struct ExportDef;
@ -35,4 +35,15 @@ impl Command for ExportDef {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head)) Ok(PipelineData::new(call.head))
} }
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Define a custom command in a module and call it",
example: r#"module spam { export def foo [] { "foo" } }; use spam foo; foo"#,
result: Some(Value::String {
val: "foo".to_string(),
span: Span::test_data(),
}),
}]
}
} }

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape}; use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)] #[derive(Clone)]
pub struct ExportDefEnv; pub struct ExportDefEnv;
@ -35,4 +35,15 @@ impl Command for ExportDefEnv {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head)) Ok(PipelineData::new(call.head))
} }
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Define a custom command that participates in the environment in a module and call it",
example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
}),
}]
}
} }

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape}; use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)] #[derive(Clone)]
pub struct ExportEnv; pub struct ExportEnv;
@ -39,4 +39,15 @@ impl Command for ExportEnv {
//TODO: Add the env to stack //TODO: Add the env to stack
Ok(PipelineData::new(call.head)) Ok(PipelineData::new(call.head))
} }
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Import and evaluate environment variable from a module",
example: r#"module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
}),
}]
}
} }

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call; use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape}; use nu_protocol::{Category, Example, PipelineData, Signature, Span, SyntaxShape, Value};
#[derive(Clone)] #[derive(Clone)]
pub struct Module; pub struct Module;
@ -34,4 +34,33 @@ impl Command for Module {
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
Ok(PipelineData::new(call.head)) Ok(PipelineData::new(call.head))
} }
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Define a custom command in a module and call it",
example: r#"module spam { export def foo [] { "foo" } }; use spam foo; foo"#,
result: Some(Value::String {
val: "foo".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Define an environment variable in a module and evaluate it",
example: r#"module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Define a custom command that participates in the environment in a module and call it",
example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
}),
},
]
}
} }

View File

@ -1,7 +1,9 @@
use nu_engine::eval_block; use nu_engine::eval_block;
use nu_protocol::ast::{Call, Expr, Expression, ImportPatternMember}; use nu_protocol::ast::{Call, Expr, Expression, ImportPatternMember};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape}; use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct Use; pub struct Use;
@ -114,4 +116,33 @@ impl Command for Use {
Ok(PipelineData::new(call.head)) Ok(PipelineData::new(call.head))
} }
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Define a custom command in a module and call it",
example: r#"module spam { export def foo [] { "foo" } }; use spam foo; foo"#,
result: Some(Value::String {
val: "foo".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Define an environment variable in a module and evaluate it",
example: r#"module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Define a custom command that participates in the environment in a module and call it",
example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
}),
},
]
}
} }