Add exit code argument (#3132)

This commit is contained in:
Tiffany Bennett
2021-03-05 21:46:27 -08:00
committed by GitHub
parent 983de8974b
commit d43489a6a0
5 changed files with 55 additions and 12 deletions

View File

@ -9,7 +9,7 @@ pub enum CommandAction {
/// Change to a new directory or path (in non-filesystem situations)
ChangePath(String),
/// Exit out of Nu
Exit,
Exit(i32),
/// Display an error
Error(ShellError),
/// Enter a new shell at the given path
@ -27,7 +27,7 @@ pub enum CommandAction {
/// Go to the next shell in the shell ring buffer
NextShell,
/// Leave the current shell. If it's the last shell, exit out of Nu
LeaveShell,
LeaveShell(i32),
}
impl PrettyDebug for CommandAction {
@ -37,7 +37,7 @@ impl PrettyDebug for CommandAction {
CommandAction::ChangePath(path) => {
DbgDocBldr::typed("change path", DbgDocBldr::description(path))
}
CommandAction::Exit => DbgDocBldr::description("exit"),
CommandAction::Exit(_) => DbgDocBldr::description("exit"),
CommandAction::Error(_) => DbgDocBldr::error("error"),
CommandAction::AutoConvert(_, extension) => {
DbgDocBldr::typed("auto convert", DbgDocBldr::description(extension))
@ -50,7 +50,7 @@ impl PrettyDebug for CommandAction {
CommandAction::AddPlugins(..) => DbgDocBldr::description("add plugins"),
CommandAction::PreviousShell => DbgDocBldr::description("previous shell"),
CommandAction::NextShell => DbgDocBldr::description("next shell"),
CommandAction::LeaveShell => DbgDocBldr::description("leave shell"),
CommandAction::LeaveShell(_) => DbgDocBldr::description("leave shell"),
}
}
}

View File

@ -445,6 +445,14 @@ impl Value {
}
}
/// View the Value as signed 64-bit, if possible
pub fn as_i32(&self) -> Result<i32, ShellError> {
match &self.value {
UntaggedValue::Primitive(primitive) => primitive.as_i32(self.tag.span),
_ => Err(ShellError::type_error("integer", self.spanned_type_name())),
}
}
/// View the Value as boolean, if possible
pub fn as_bool(&self) -> Result<bool, ShellError> {
match &self.value {

View File

@ -107,6 +107,29 @@ impl Primitive {
}
}
pub fn as_i32(&self, span: Span) -> Result<i32, ShellError> {
match self {
Primitive::Int(int) => int.to_i32().ok_or_else(|| {
ShellError::range_error(
ExpectedRange::I32,
&format!("{}", int).spanned(span),
"converting an integer into a signed 32-bit integer",
)
}),
Primitive::Decimal(decimal) => decimal.to_i32().ok_or_else(|| {
ShellError::range_error(
ExpectedRange::I32,
&format!("{}", decimal).spanned(span),
"converting a decimal into a signed 32-bit integer",
)
}),
other => Err(ShellError::type_error(
"number",
other.type_name().spanned(span),
)),
}
}
// FIXME: This is a bad name, but no other way to differentiate with our own Duration.
pub fn into_chrono_duration(self, span: Span) -> Result<chrono::Duration, ShellError> {
match self {