diff --git a/crates/nu-command/src/core_commands/export_def.rs b/crates/nu-command/src/core_commands/export_def.rs index 79d7c2bff7..1444cab98d 100644 --- a/crates/nu-command/src/core_commands/export_def.rs +++ b/crates/nu-command/src/core_commands/export_def.rs @@ -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, Span, SyntaxShape, Value}; #[derive(Clone)] pub struct ExportDef; @@ -35,4 +35,15 @@ impl Command for ExportDef { ) -> Result { Ok(PipelineData::new(call.head)) } + + fn examples(&self) -> Vec { + 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(), + }), + }] + } } diff --git a/crates/nu-command/src/core_commands/export_def_env.rs b/crates/nu-command/src/core_commands/export_def_env.rs index c5bc0b3a03..d724f44a37 100644 --- a/crates/nu-command/src/core_commands/export_def_env.rs +++ b/crates/nu-command/src/core_commands/export_def_env.rs @@ -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, Span, SyntaxShape, Value}; #[derive(Clone)] pub struct ExportDefEnv; @@ -35,4 +35,15 @@ impl Command for ExportDefEnv { ) -> Result { Ok(PipelineData::new(call.head)) } + + fn examples(&self) -> Vec { + 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(), + }), + }] + } } diff --git a/crates/nu-command/src/core_commands/export_env.rs b/crates/nu-command/src/core_commands/export_env.rs index 261a97b3ab..883519c33b 100644 --- a/crates/nu-command/src/core_commands/export_env.rs +++ b/crates/nu-command/src/core_commands/export_env.rs @@ -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, Span, SyntaxShape, Value}; #[derive(Clone)] pub struct ExportEnv; @@ -39,4 +39,15 @@ impl Command for ExportEnv { //TODO: Add the env to stack Ok(PipelineData::new(call.head)) } + + fn examples(&self) -> Vec { + 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(), + }), + }] + } } diff --git a/crates/nu-command/src/core_commands/module.rs b/crates/nu-command/src/core_commands/module.rs index fbdc067295..f96519796d 100644 --- a/crates/nu-command/src/core_commands/module.rs +++ b/crates/nu-command/src/core_commands/module.rs @@ -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, Span, SyntaxShape, Value}; #[derive(Clone)] pub struct Module; @@ -34,4 +34,33 @@ impl Command for Module { ) -> Result { Ok(PipelineData::new(call.head)) } + + fn examples(&self) -> Vec { + 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(), + }), + }, + ] + } } diff --git a/crates/nu-command/src/core_commands/use_.rs b/crates/nu-command/src/core_commands/use_.rs index b9bfcc2bbe..db1ccb8c20 100644 --- a/crates/nu-command/src/core_commands/use_.rs +++ b/crates/nu-command/src/core_commands/use_.rs @@ -1,7 +1,9 @@ use nu_engine::eval_block; use nu_protocol::ast::{Call, Expr, Expression, ImportPatternMember}; 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)] pub struct Use; @@ -114,4 +116,33 @@ impl Command for Use { Ok(PipelineData::new(call.head)) } + + fn examples(&self) -> Vec { + 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(), + }), + }, + ] + } }