mirror of
https://github.com/nushell/nushell
synced 2024-12-27 21:43:09 +00:00
maintain quotes for arguments (#6161)
This commit is contained in:
parent
2ea209bcc0
commit
e2a21afca8
1 changed files with 40 additions and 3 deletions
|
@ -531,12 +531,12 @@ fn trim_enclosing_quotes(input: &str) -> (String, bool) {
|
|||
fn remove_quotes(input: String) -> String {
|
||||
let mut chars = input.chars();
|
||||
|
||||
match chars.next_back() {
|
||||
Some('"') => chars
|
||||
match (chars.next_back(), input.contains('=')) {
|
||||
(Some('"'), true) => chars
|
||||
.collect::<String>()
|
||||
.replacen('"', "", 1)
|
||||
.replace(r#"\""#, "\""),
|
||||
Some('\'') => chars.collect::<String>().replacen('\'', "", 1),
|
||||
(Some('\''), true) => chars.collect::<String>().replacen('\'', "", 1),
|
||||
_ => input,
|
||||
}
|
||||
}
|
||||
|
@ -586,3 +586,40 @@ impl Iterator for ValueReceiver {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn remove_quotes_argument_with_equal_test() {
|
||||
let input = r#"--file="my_file.txt""#.into();
|
||||
let res = remove_quotes(input);
|
||||
|
||||
assert_eq!("--file=my_file.txt", res)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn argument_without_equal_test() {
|
||||
let input = r#"--file "my_file.txt""#.into();
|
||||
let res = remove_quotes(input);
|
||||
|
||||
assert_eq!(r#"--file "my_file.txt""#, res)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_quotes_argument_with_single_quotes_test() {
|
||||
let input = r#"--file='my_file.txt'"#.into();
|
||||
let res = remove_quotes(input);
|
||||
|
||||
assert_eq!("--file=my_file.txt", res)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn argument_with_inner_quotes_test() {
|
||||
let input = r#"bash -c 'echo a'"#.into();
|
||||
let res = remove_quotes(input);
|
||||
|
||||
assert_eq!("bash -c 'echo a'", res)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue