mirror of
https://github.com/nushell/nushell.git
synced 2025-01-23 06:39:17 +01:00
str-expand: add path flag (#9856)
Related issues: #9838 Changes: - added `--path` flag, for ease of use if the piped data is Path (replaces all backslashes with double backslashes)
This commit is contained in:
parent
28ed21864d
commit
94bec72079
@ -29,6 +29,11 @@ impl Command for SubCommand {
|
|||||||
Type::List(Box::new(Type::List(Box::new(Type::String)))),
|
Type::List(Box::new(Type::List(Box::new(Type::String)))),
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
.switch(
|
||||||
|
"path",
|
||||||
|
"Replaces all backslashes with double backslashes, useful for Path.",
|
||||||
|
None,
|
||||||
|
)
|
||||||
.allow_variants_without_examples(true)
|
.allow_variants_without_examples(true)
|
||||||
.category(Category::Strings)
|
.category(Category::Strings)
|
||||||
}
|
}
|
||||||
@ -85,6 +90,18 @@ impl Command for SubCommand {
|
|||||||
},)
|
},)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Example {
|
||||||
|
description: "If the piped data is path, you may want to use --path flag, or else manually replace the backslashes with double backslashes.",
|
||||||
|
example: "'C:\\{Users,Windows}' | str expand --path",
|
||||||
|
result: Some(Value::List{
|
||||||
|
vals: vec![
|
||||||
|
Value::test_string("C:\\Users"),
|
||||||
|
Value::test_string("C:\\Windows"),
|
||||||
|
],
|
||||||
|
span: Span::test_data()
|
||||||
|
},)
|
||||||
|
},
|
||||||
|
|
||||||
Example {
|
Example {
|
||||||
description: "Brace expressions can be used one after another.",
|
description: "Brace expressions can be used one after another.",
|
||||||
example: "\"A{b,c}D{e,f}G\" | str expand",
|
example: "\"A{b,c}D{e,f}G\" | str expand",
|
||||||
@ -165,6 +182,7 @@ impl Command for SubCommand {
|
|||||||
if matches!(input, PipelineData::Empty) {
|
if matches!(input, PipelineData::Empty) {
|
||||||
return Err(ShellError::PipelineEmpty { dst_span: span });
|
return Err(ShellError::PipelineEmpty { dst_span: span });
|
||||||
}
|
}
|
||||||
|
let is_path = call.has_flag("path");
|
||||||
input.map(
|
input.map(
|
||||||
move |v| {
|
move |v| {
|
||||||
let value_span = match v.span() {
|
let value_span = match v.span() {
|
||||||
@ -172,7 +190,10 @@ impl Command for SubCommand {
|
|||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
};
|
};
|
||||||
match v.as_string() {
|
match v.as_string() {
|
||||||
Ok(s) => str_expand(&s, span, v.expect_span()),
|
Ok(s) => {
|
||||||
|
let contents = if is_path { s.replace('\\', "\\\\") } else { s };
|
||||||
|
str_expand(&contents, span, v.expect_span())
|
||||||
|
}
|
||||||
Err(_) => Value::Error {
|
Err(_) => Value::Error {
|
||||||
error: Box::new(ShellError::PipelineMismatch {
|
error: Box::new(ShellError::PipelineMismatch {
|
||||||
exp_input_type: "string".into(),
|
exp_input_type: "string".into(),
|
||||||
|
Loading…
Reference in New Issue
Block a user