nushell/crates/nu-cli/src/commands/exit.rs

74 lines
1.7 KiB
Rust
Raw Normal View History

use crate::command_registry::CommandRegistry;
use crate::commands::command::WholeStreamCommand;
2019-06-13 23:47:25 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{CommandAction, ReturnSuccess, Signature};
2019-06-13 23:47:25 +02:00
2019-08-07 19:49:11 +02:00
pub struct Exit;
2020-05-29 10:22:52 +02:00
#[async_trait]
2019-08-15 07:02:02 +02:00
impl WholeStreamCommand for Exit {
fn name(&self) -> &str {
"exit"
}
fn signature(&self) -> Signature {
Signature::build("exit").switch("now", "exit out of the shell immediately", Some('n'))
}
fn usage(&self) -> &str {
"Exit the current shell (or all shells)"
}
2020-05-29 10:22:52 +02:00
async fn run(
2019-08-09 06:51:21 +02:00
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
exit(args, registry).await
2019-08-07 19:49:11 +02:00
}
2020-05-12 07:17:17 +02:00
fn examples(&self) -> Vec<Example> {
vec![
2020-05-12 07:17:17 +02:00
Example {
description: "Exit the current shell",
example: "exit",
result: None,
2020-05-12 07:17:17 +02:00
},
Example {
description: "Exit all shells (exiting Nu)",
example: "exit --now",
result: None,
2020-05-12 07:17:17 +02:00
},
]
}
2019-08-07 19:49:11 +02:00
}
pub async fn exit(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
let command_action = if args.call_info.args.has("now") {
CommandAction::Exit
} else {
CommandAction::LeaveShell
};
Ok(OutputStream::one(ReturnSuccess::action(command_action)))
2019-06-13 23:47:25 +02:00
}
#[cfg(test)]
mod tests {
use super::Exit;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Exit {})
}
}