path join support multi path (#5775)

This commit is contained in:
WindSoilder 2022-06-14 19:34:00 +08:00 committed by GitHub
parent ff53352afe
commit 48e29e9ed6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,7 +13,7 @@ use super::PathSubcommandArguments;
struct Arguments { struct Arguments {
columns: Option<Vec<String>>, columns: Option<Vec<String>>,
append: Option<Spanned<String>>, append: Vec<Spanned<String>>,
} }
impl PathSubcommandArguments for Arguments { impl PathSubcommandArguments for Arguments {
@ -38,7 +38,7 @@ impl Command for SubCommand {
"Optionally operate by column path", "Optionally operate by column path",
Some('c'), 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 { fn usage(&self) -> &str {
@ -60,7 +60,7 @@ the output of 'path parse' and 'path split' subcommands."#
let head = call.head; let head = call.head;
let args = Arguments { let args = Arguments {
columns: call.get_flag(engine_state, stack, "columns")?, 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(); 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", example: r"'C:\Users\viking' | path join spam.txt",
result: Some(Value::test_string(r"C:\Users\viking\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 { Example {
description: "Append a filename to a path inside a column", description: "Append a filename to a path inside a column",
example: r"ls | path join spam.txt -c [ name ]", 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", example: r"'/home/viking' | path join spam.txt",
result: Some(Value::test_string(r"/home/viking/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 { Example {
description: "Append a filename to a path inside a column", description: "Append a filename to a path inside a column",
example: r"ls | path join spam.txt -c [ name ]", 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 { fn join_single(path: &Path, span: Span, args: &Arguments) -> Value {
let path = if let Some(ref append) = args.append { let mut result = path.to_path_buf();
path.join(Path::new(&append.item)) for path_to_append in &args.append {
} else { result.push(&path_to_append.item)
path.to_path_buf() }
};
Value::string(path.to_string_lossy(), span) Value::string(result.to_string_lossy(), span)
} }
fn join_list(parts: &[Value], span: Span, args: &Arguments) -> Value { fn join_list(parts: &[Value], span: Span, args: &Arguments) -> Value {