diff --git a/crates/nu-command/src/strings/str_/expand.rs b/crates/nu-command/src/strings/str_/expand.rs index 7cbc8d170b..24f6d33fba 100644 --- a/crates/nu-command/src/strings/str_/expand.rs +++ b/crates/nu-command/src/strings/str_/expand.rs @@ -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(),