Skip to content

Commit

Permalink
fix argument type (nushell#5695)
Browse files Browse the repository at this point in the history
* fix argument type

* while run external, convert list argument to str

* fix argument converting logic

* using parse_list_expression instead of parse_full_cell_path

* make parsing logic more explicit

* revert changes

* add tests
  • Loading branch information
WindSoilder authored and fennewald committed Jun 27, 2022
1 parent 129094f commit da76422
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
26 changes: 15 additions & 11 deletions crates/nu-command/src/system/run_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,24 @@ impl Command for External {
})
}

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 mut spanned_args = vec![];
for one_arg in args {
match one_arg {
Value::List { vals, .. } => {
// turn all the strings in the array into params.
// Example: one_arg may be something like ["ls" "-a"]
// convert it to "ls" "-a"
for v in vals {
spanned_args.push(value_as_spanned(v)?)
}
}
val => spanned_args.push(value_as_spanned(val)?),
}
}

let command = ExternalCommand {
name,
args,
args: spanned_args,
redirect_stdout,
redirect_stderr,
env_vars: env_vars_str,
Expand Down
10 changes: 7 additions & 3 deletions crates/nu-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,13 @@ pub fn parse_external_call(
let (arg, err) = parse_dollar_expr(working_set, *span, expand_aliases_denylist);
error = error.or(err);
args.push(arg);
} else if contents.starts_with(b"(") {
let (arg, err) =
parse_full_cell_path(working_set, None, *span, expand_aliases_denylist);
} else if contents.starts_with(b"[") {
let (arg, err) = parse_list_expression(
working_set,
*span,
&SyntaxShape::Any,
expand_aliases_denylist,
);
error = error.or(err);
args.push(arg);
} else {
Expand Down
24 changes: 24 additions & 0 deletions tests/shell/pipeline/commands/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,30 @@ mod nu_commands {

assert_eq!(actual.out, "");
}

#[test]
fn command_list_arg_test() {
let actual = nu!(cwd: ".", r#"
nu ['-c' 'version']
"#);

assert!(actual.out.contains("version"));
assert!(actual.out.contains("rust_version"));
assert!(actual.out.contains("rust_channel"));
assert!(actual.out.contains("pkg_version"));
}

#[test]
fn command_cell_path_arg_test() {
let actual = nu!(cwd: ".", r#"
nu ([ '-c' 'version' ])
"#);

assert!(actual.out.contains("version"));
assert!(actual.out.contains("rust_version"));
assert!(actual.out.contains("rust_channel"));
assert!(actual.out.contains("pkg_version"));
}
}

mod nu_script {
Expand Down

0 comments on commit da76422

Please sign in to comment.