nu_cmd_lang/core_commands/
module.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::engine::CommandType;
3
4#[derive(Clone)]
5pub struct Module;
6
7impl Command for Module {
8    fn name(&self) -> &str {
9        "module"
10    }
11
12    fn description(&self) -> &str {
13        "Define a custom module."
14    }
15
16    fn signature(&self) -> nu_protocol::Signature {
17        Signature::build("module")
18            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
19            .allow_variants_without_examples(true)
20            .required("module", SyntaxShape::String, "Module name or module path.")
21            .optional(
22                "block",
23                SyntaxShape::Block,
24                "Body of the module if 'module' parameter is not a module path.",
25            )
26            .category(Category::Core)
27    }
28
29    fn extra_description(&self) -> &str {
30        r#"This command is a parser keyword. For details, check:
31  https://www.nushell.sh/book/thinking_in_nu.html"#
32    }
33
34    fn command_type(&self) -> CommandType {
35        CommandType::Keyword
36    }
37
38    fn run(
39        &self,
40        _engine_state: &EngineState,
41        _stack: &mut Stack,
42        _call: &Call,
43        _input: PipelineData,
44    ) -> Result<PipelineData, ShellError> {
45        Ok(PipelineData::empty())
46    }
47
48    fn examples(&self) -> Vec<Example<'_>> {
49        vec![
50            Example {
51                description: "Define a custom command in a module and call it",
52                example: r#"module spam { export def foo [] { "foo" } }; use spam foo; foo"#,
53                result: Some(Value::test_string("foo")),
54            },
55            Example {
56                description: "Define an environment variable in a module",
57                example: r#"module foo { export-env { $env.FOO = "BAZ" } }; use foo; $env.FOO"#,
58                result: Some(Value::test_string("BAZ")),
59            },
60            Example {
61                description: "Define a custom command that participates in the environment in a module and call it",
62                example: r#"module foo { export def --env bar [] { $env.FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
63                result: Some(Value::test_string("BAZ")),
64            },
65        ]
66    }
67}