From f4a129a792770426e19ea7d9ff44812fb26c1229 Mon Sep 17 00:00:00 2001 From: Yethal <26117918+Yethal@users.noreply.github.com> Date: Wed, 22 Feb 2023 20:01:15 +0100 Subject: [PATCH] Added examples to query web plugin (#8171) # Description _(Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes.)_ _(Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience.)_ # User-Facing Changes _(List of all changes that impact the user experience here. This helps us keep track of breaking changes.)_ # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --------- Co-authored-by: Yethal --- crates/nu_plugin_query/src/nu/mod.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/nu_plugin_query/src/nu/mod.rs b/crates/nu_plugin_query/src/nu/mod.rs index 65ca0e4c2e..27ce7a4b98 100644 --- a/crates/nu_plugin_query/src/nu/mod.rs +++ b/crates/nu_plugin_query/src/nu/mod.rs @@ -1,6 +1,6 @@ use crate::Query; use nu_plugin::{EvaluatedCall, LabeledError, Plugin}; -use nu_protocol::{Category, PluginSignature, Spanned, SyntaxShape, Value}; +use nu_protocol::{Category, PluginExample, PluginSignature, Spanned, SyntaxShape, Value}; impl Plugin for Query { fn signature(&self) -> Vec { @@ -23,6 +23,7 @@ impl Plugin for Query { .usage("execute selector query on html/web") .named("query", SyntaxShape::String, "selector query", Some('q')) .switch("as-html", "return the query output as html", Some('m')) + .plugin_examples(web_examples()) .named( "attribute", SyntaxShape::String, @@ -68,3 +69,26 @@ impl Plugin for Query { } } } + +pub fn web_examples() -> Vec { + vec![PluginExample { + example: "http get https://phoronix.com | query web -q 'header'".into(), + description: "Retrieve all
elements from phoronix.com website".into(), + result: None, + }, PluginExample { + example: "http get https://en.wikipedia.org/wiki/List_of_cities_in_India_by_population + | query web -t [Rank City 'Population(2011)[3]' 'Population(2001)' 'State or union territory']".into(), + description: "Retrieve a html table from Wikipedia and parse it into a nushell table using table headers as guides".into(), + result: None + }, + PluginExample { + example: "http get https://www.nushell.sh | query web -q 'h2, h2 + p' | group 2 | each {rotate --ccw tagline description} | flatten".into(), + description: "Pass multiple css selectors to extract several elements within single query, group the query results together and rotate them to create a table".into(), + result: None, + }, + PluginExample { + example: "http get https://example.org | query web --query a --attribute href".into(), + description: "Retrieve a specific html attribute instead of the default text".into(), + result: None, + }] +}