Use better quoting for commandline args (#5271)

This commit is contained in:
JT 2022-04-21 15:31:52 +12:00 committed by GitHub
parent ded9d1cedb
commit 96253c69fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 3 deletions

View file

@ -22,7 +22,10 @@ pub fn evaluate_commands(
let (block, delta) = { let (block, delta) = {
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
let (input, _) = if commands.item.starts_with('\'') || commands.item.starts_with('"') { let (input, _) = if commands.item.starts_with('\'')
|| commands.item.starts_with('"')
|| commands.item.starts_with('`')
{
( (
trim_quotes(commands.item.as_bytes()), trim_quotes(commands.item.as_bytes()),
commands.span.start + 1, commands.span.start + 1,

View file

@ -512,6 +512,7 @@ fn trim_enclosing_quotes(input: &str) -> String {
match (chars.next(), chars.next_back()) { match (chars.next(), chars.next_back()) {
(Some('"'), Some('"')) => chars.collect(), (Some('"'), Some('"')) => chars.collect(),
(Some('\''), Some('\'')) => chars.collect(), (Some('\''), Some('\'')) => chars.collect(),
(Some('`'), Some('`')) => chars.collect(),
_ => input.to_string(), _ => input.to_string(),
} }
} }

View file

@ -83,13 +83,13 @@ fn main() -> Result<()> {
for arg in std::env::args().skip(1) { for arg in std::env::args().skip(1) {
if !script_name.is_empty() { if !script_name.is_empty() {
args_to_script.push(if arg.contains(' ') { args_to_script.push(if arg.contains(' ') {
format!("'{}'", arg) format!("`{}`", arg)
} else { } else {
arg arg
}); });
} else if collect_arg_nushell { } else if collect_arg_nushell {
args_to_nushell.push(if arg.contains(' ') { args_to_nushell.push(if arg.contains(' ') {
format!("'{}'", arg) format!("`{}`", arg)
} else { } else {
arg arg
}); });

View file

@ -297,6 +297,15 @@ mod nu_commands {
assert_eq!(actual.out, "foo"); assert_eq!(actual.out, "foo");
} }
#[test]
fn better_arg_quoting() {
let actual = nu!(cwd: ".", r#"
nu -c "\# '"
"#);
assert_eq!(actual.out, "");
}
} }
mod nu_script { mod nu_script {