tweak parse usage and examples to be more clear

This commit is contained in:
Darren Schroeder 2024-07-12 09:33:01 -05:00
parent 4bd87d0496
commit 3d1ac809e5

View File

@ -12,13 +12,17 @@ impl Command for Parse {
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Parse columns from string data using a simple pattern." "Parse columns from string data using a simple pattern or a supplied regular expression."
} }
fn search_terms(&self) -> Vec<&str> { fn search_terms(&self) -> Vec<&str> {
vec!["pattern", "match", "regex", "str extract"] vec!["pattern", "match", "regex", "str extract"]
} }
fn extra_usage(&self) -> &str {
"The parse command always uses regular expressions even when you use a simple pattern. If a simple pattern is supplied, parse will transform that pattern into a regular expression."
}
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("parse") Signature::build("parse")
.required("pattern", SyntaxShape::String, "The pattern to match.") .required("pattern", SyntaxShape::String, "The pattern to match.")
@ -32,21 +36,24 @@ impl Command for Parse {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
let result = Value::test_list(vec![Value::test_record(record! {
"foo" => Value::test_string("hi"),
"bar" => Value::test_string("there"),
})]);
vec![ vec![
Example { Example {
description: "Parse a string into two named columns", description: "Parse a string into two named columns",
example: "\"hi there\" | parse \"{foo} {bar}\"", example: "\"hi there\" | parse \"{foo} {bar}\"",
result: Some(result.clone()), result: Some(Value::test_list(
vec![Value::test_record(record! {
"foo" => Value::test_string("hi"),
"bar" => Value::test_string("there"),
})])),
}, },
Example { Example {
description: "Parse a string using regex pattern", description: "This is how the first example is interpreted in the source code",
example: "\"hi there\" | parse --regex '(?P<foo>\\w+) (?P<bar>\\w+)'", example: "\"hi there\" | parse --regex '(?s)\\A(?P<foo>.*?) (?P<bar>.*?)\\z'",
result: Some(result), result: Some(Value::test_list(
vec![Value::test_record(record! {
"foo" => Value::test_string("hi"),
"bar" => Value::test_string("there"),
})])),
}, },
Example { Example {
description: "Parse a string using fancy-regex named capture group pattern", description: "Parse a string using fancy-regex named capture group pattern",