From 48e29e9ed63d18ebdec5d2d4985b9abeecfda914 Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Tue, 14 Jun 2022 19:34:00 +0800 Subject: [PATCH] path join support multi path (#5775) --- crates/nu-command/src/path/join.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/nu-command/src/path/join.rs b/crates/nu-command/src/path/join.rs index 53c2e5e444..32e88e084a 100644 --- a/crates/nu-command/src/path/join.rs +++ b/crates/nu-command/src/path/join.rs @@ -13,7 +13,7 @@ use super::PathSubcommandArguments; struct Arguments { columns: Option>, - append: Option>, + append: Vec>, } impl PathSubcommandArguments for Arguments { @@ -38,7 +38,7 @@ impl Command for SubCommand { "Optionally operate by column path", Some('c'), ) - .optional("append", SyntaxShape::String, "Path to append to the input") + .rest("append", SyntaxShape::String, "Path to append to the input") } fn usage(&self) -> &str { @@ -60,7 +60,7 @@ the output of 'path parse' and 'path split' subcommands."# let head = call.head; let args = Arguments { columns: call.get_flag(engine_state, stack, "columns")?, - append: call.opt(engine_state, stack, 0)?, + append: call.rest(engine_state, stack, 0)?, }; let metadata = input.metadata(); @@ -88,6 +88,11 @@ the output of 'path parse' and 'path split' subcommands."# example: r"'C:\Users\viking' | path join spam.txt", result: Some(Value::test_string(r"C:\Users\viking\spam.txt")), }, + Example { + description: "Append a filename to a path", + example: r"'C:\Users\viking' | path join spams this_spam.txt", + result: Some(Value::test_string(r"C:\Users\viking\spams\this_spam.txt")), + }, Example { description: "Append a filename to a path inside a column", example: r"ls | path join spam.txt -c [ name ]", @@ -117,6 +122,11 @@ the output of 'path parse' and 'path split' subcommands."# example: r"'/home/viking' | path join spam.txt", result: Some(Value::test_string(r"/home/viking/spam.txt")), }, + Example { + description: "Append a filename to a path", + example: r"'/home/viking' | path join spams this_spam.txt", + result: Some(Value::test_string(r"/home/viking/spams/this_spam.txt")), + }, Example { description: "Append a filename to a path inside a column", example: r"ls | path join spam.txt -c [ name ]", @@ -150,13 +160,12 @@ fn handle_value(v: Value, args: &Arguments, head: Span) -> Value { } fn join_single(path: &Path, span: Span, args: &Arguments) -> Value { - let path = if let Some(ref append) = args.append { - path.join(Path::new(&append.item)) - } else { - path.to_path_buf() - }; + let mut result = path.to_path_buf(); + for path_to_append in &args.append { + result.push(&path_to_append.item) + } - Value::string(path.to_string_lossy(), span) + Value::string(result.to_string_lossy(), span) } fn join_list(parts: &[Value], span: Span, args: &Arguments) -> Value {