mirror of
https://github.com/nushell/nushell
synced 2025-01-14 14:14:13 +00:00
allow int and float as strings for arguments (#4615)
* allow int and float as strings for arguments * consume iterator
This commit is contained in:
parent
4b18fdcc6e
commit
21c0f7d738
2 changed files with 26 additions and 32 deletions
|
@ -58,43 +58,35 @@ impl Command for External {
|
|||
let config = stack.get_config().unwrap_or_default();
|
||||
let env_vars_str = env_to_strings(engine_state, stack, &config)?;
|
||||
|
||||
let mut args_strs = vec![];
|
||||
fn value_as_spanned(value: Value) -> Result<Spanned<String>, ShellError> {
|
||||
let span = value.span()?;
|
||||
|
||||
for arg in args {
|
||||
let span = if let Ok(span) = arg.span() {
|
||||
span
|
||||
} else {
|
||||
Span { start: 0, end: 0 }
|
||||
};
|
||||
|
||||
if let Ok(s) = arg.as_string() {
|
||||
args_strs.push(Spanned { item: s, span });
|
||||
} else if let Value::List { vals, span } = arg {
|
||||
// Interpret a list as a series of arguments
|
||||
for val in vals {
|
||||
if let Ok(s) = val.as_string() {
|
||||
args_strs.push(Spanned { item: s, span });
|
||||
} else {
|
||||
return Err(ShellError::ExternalCommand(
|
||||
"Cannot convert argument to a string".into(),
|
||||
"All arguments to an external command need to be string-compatible"
|
||||
.into(),
|
||||
val.span()?,
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(ShellError::ExternalCommand(
|
||||
"Cannot convert argument to a string".into(),
|
||||
"All arguments to an external command need to be string-compatible".into(),
|
||||
arg.span()?,
|
||||
));
|
||||
}
|
||||
value
|
||||
.as_string()
|
||||
.map(|item| Spanned { item, span })
|
||||
.map_err(|_| {
|
||||
ShellError::ExternalCommand(
|
||||
"Cannot convert argument to a string".into(),
|
||||
"All arguments to an external command need to be string-compatible".into(),
|
||||
span,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
let args = args
|
||||
.into_iter()
|
||||
.flat_map(|arg| match arg {
|
||||
Value::List { vals, .. } => vals
|
||||
.into_iter()
|
||||
.map(value_as_spanned)
|
||||
.collect::<Vec<Result<Spanned<String>, ShellError>>>(),
|
||||
val => vec![value_as_spanned(val)],
|
||||
})
|
||||
.collect::<Result<Vec<Spanned<String>>, ShellError>>()?;
|
||||
|
||||
let command = ExternalCommand {
|
||||
name,
|
||||
args: args_strs,
|
||||
args,
|
||||
redirect_stdout,
|
||||
redirect_stderr,
|
||||
env_vars: env_vars_str,
|
||||
|
|
|
@ -173,6 +173,8 @@ impl Clone for Value {
|
|||
impl Value {
|
||||
pub fn as_string(&self) -> Result<String, ShellError> {
|
||||
match self {
|
||||
Value::Int { val, .. } => Ok(val.to_string()),
|
||||
Value::Float { val, .. } => Ok(val.to_string()),
|
||||
Value::String { val, .. } => Ok(val.to_string()),
|
||||
Value::Binary { val, .. } => Ok(match std::str::from_utf8(val) {
|
||||
Ok(s) => s.to_string(),
|
||||
|
|
Loading…
Reference in a new issue