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:
A. Taha Baki 2023-07-31 15:48:29 +03:00 committed by GitHub
parent 28ed21864d
commit 94bec72079
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,6 +29,11 @@ impl Command for SubCommand {
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)
.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 {
description: "Brace expressions can be used one after another.",
example: "\"A{b,c}D{e,f}G\" | str expand",
@ -165,6 +182,7 @@ impl Command for SubCommand {
if matches!(input, PipelineData::Empty) {
return Err(ShellError::PipelineEmpty { dst_span: span });
}
let is_path = call.has_flag("path");
input.map(
move |v| {
let value_span = match v.span() {
@ -172,7 +190,10 @@ impl Command for SubCommand {
Ok(v) => v,
};
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 {
error: Box::new(ShellError::PipelineMismatch {
exp_input_type: "string".into(),