Skip to main content

nu_command/shells/
exit.rs

1use nu_engine::{command_prelude::*, exit::cleanup};
2
3#[derive(Clone)]
4pub struct Exit;
5
6impl Command for Exit {
7    fn name(&self) -> &str {
8        "exit"
9    }
10
11    fn signature(&self) -> Signature {
12        Signature::build("exit")
13            .input_output_types(vec![(Type::Nothing, Type::Nothing)])
14            .optional(
15                "exit_code",
16                SyntaxShape::Int,
17                "Exit code to return immediately with.",
18            )
19            .switch("abort", "Exit by abort.", None)
20            .category(Category::Shells)
21    }
22
23    fn description(&self) -> &str {
24        "Exit Nu."
25    }
26
27    fn search_terms(&self) -> Vec<&str> {
28        vec!["quit", "close", "exit_code", "error_code", "logout"]
29    }
30
31    fn run(
32        &self,
33        engine_state: &EngineState,
34        stack: &mut Stack,
35        call: &Call,
36        _input: PipelineData,
37    ) -> Result<PipelineData, ShellError> {
38        let exit_code: Option<i64> = call.opt(engine_state, stack, 0)?;
39
40        let abort = call.has_flag(engine_state, stack, "abort")?;
41        let exit_code = exit_code.map_or(0, |it| it as i32);
42
43        if abort {
44            if cleanup((), engine_state).is_some() {
45                Ok(Value::nothing(call.head).into_pipeline_data())
46            } else {
47                Err(ShellError::Exit {
48                    code: exit_code,
49                    abort: true,
50                })
51            }
52        } else if cleanup((), engine_state).is_some() {
53            Ok(Value::nothing(call.head).into_pipeline_data())
54        } else {
55            Err(ShellError::Exit {
56                code: exit_code,
57                abort: false,
58            })
59        }
60    }
61
62    fn examples(&self) -> Vec<Example<'_>> {
63        vec![Example {
64            description: "Exit the current shell",
65            example: "exit",
66            result: None,
67        }]
68    }
69}