nu_command/system/
run_internal.rs1use nu_engine::{CallExt, command_prelude::*, find_builtin_decl};
2use nu_protocol::ir;
3
4#[derive(Clone)]
11pub struct RunInternal;
12
13impl Command for RunInternal {
14 fn name(&self) -> &str {
15 "run-internal"
16 }
17
18 fn description(&self) -> &str {
19 "Run a built-in command by name. Used internally by `%($cmd)` dynamic dispatch."
20 }
21
22 fn signature(&self) -> Signature {
23 Signature::build("run-internal")
24 .input_output_types(vec![(Type::Any, Type::Any)])
25 .required(
26 "name",
27 SyntaxShape::String,
28 "The name of the built-in command to run.",
29 )
30 .rest(
31 "args",
32 SyntaxShape::Any,
33 "Arguments to pass to the built-in command.",
34 )
35 }
36
37 fn run(
38 &self,
39 engine_state: &EngineState,
40 stack: &mut Stack,
41 call: &Call,
42 input: PipelineData,
43 ) -> Result<PipelineData, ShellError> {
44 let head = call.head;
45 let name: String = call.req(engine_state, stack, 0)?;
46
47 let decl_id = find_builtin_decl(engine_state, &name)
48 .ok_or(ShellError::CommandNotFound { span: head })?;
49
50 let decl = engine_state.get_decl(decl_id);
51 let rest_args: Vec<(Value, bool)> = call.rest_preserving_spreads(engine_state, stack, 1)?;
54
55 let mut builder = ir::Call::build(decl_id, head);
57 for (val, is_spread) in rest_args {
58 if is_spread {
59 match val {
63 Value::List { ref vals, .. } if vals.is_empty() => continue,
64 Value::Nothing { .. } => continue,
65 _ => {
66 builder.add_spread(stack, head, val);
67 }
68 }
69 } else {
70 builder.add_positional(stack, head, val);
71 }
72 }
73
74 builder.with(stack, |stack, engine_call| {
77 decl.run(engine_state, stack, engine_call, input)
78 })
79 }
80}