nu_cmd_lang/core_commands/
def.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::engine::CommandType;
3
4#[derive(Clone)]
5pub struct Def;
6
7impl Command for Def {
8    fn name(&self) -> &str {
9        "def"
10    }
11
12    fn description(&self) -> &str {
13        "Define a custom command."
14    }
15
16    fn signature(&self) -> nu_protocol::Signature {
17        Signature::build("def")
18            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
19            .required("def_name", SyntaxShape::String, "Command name.")
20            .required("params", SyntaxShape::Signature, "Parameters.")
21            .required("block", SyntaxShape::Closure(None), "Body of the definition.")
22            .switch("env", "keep the environment defined inside the command", None)
23            .switch("wrapped", "treat unknown flags and arguments as strings (requires ...rest-like parameter in signature)", None)
24            .category(Category::Core)
25    }
26
27    fn extra_description(&self) -> &str {
28        r#"This command is a parser keyword. For details, check:
29  https://www.nushell.sh/book/thinking_in_nu.html"#
30    }
31
32    fn command_type(&self) -> CommandType {
33        CommandType::Keyword
34    }
35
36    fn run(
37        &self,
38        _engine_state: &EngineState,
39        _stack: &mut Stack,
40        _call: &Call,
41        _input: PipelineData,
42    ) -> Result<PipelineData, ShellError> {
43        Ok(PipelineData::empty())
44    }
45
46    fn examples(&self) -> Vec<Example<'_>> {
47        vec![
48            Example {
49                description: "Define a command and run it",
50                example: r#"def say-hi [] { echo 'hi' }; say-hi"#,
51                result: Some(Value::test_string("hi")),
52            },
53            Example {
54                description: "Define a command and run it with parameter(s)",
55                example: r#"def say-sth [sth: string] { echo $sth }; say-sth hi"#,
56                result: Some(Value::test_string("hi")),
57            },
58            Example {
59                description: "Set environment variable by call a custom command",
60                example: r#"def --env foo [] { $env.BAR = "BAZ" }; foo; $env.BAR"#,
61                result: Some(Value::test_string("BAZ")),
62            },
63            Example {
64                description: "cd affects the environment, so '--env' is required to change directory from within a command",
65                example: r#"def --env gohome [] { cd ~ }; gohome; $env.PWD == ('~' | path expand)"#,
66                result: Some(Value::test_string("true")),
67            },
68            Example {
69                description: "Define a custom wrapper for an external command",
70                example: r#"def --wrapped my-echo [...rest] { ^echo ...$rest }; my-echo -e 'spam\tspam'"#,
71                result: Some(Value::test_string("spam\tspam")),
72            },
73            Example {
74                description: "Define a custom command with a type signature. Passing a non-int value will result in an error",
75                example: r#"def only_int []: int -> int { $in }; 42 | only_int"#,
76                result: Some(Value::test_int(42)),
77            },
78        ]
79    }
80}