Files
nushell/crates/nu_plugin_example/src/commands/config.rs
Stefan Holderbach 95b78eee25 Change the usage misnomer to "description" (#13598)
# Description
    
The meaning of the word usage is specific to describing how a command
function is *used* and not a synonym for general description. Usage can
be used to describe the SYNOPSIS or EXAMPLES sections of a man page
where the permitted argument combinations are shown or example *uses*
are given.
Let's not confuse people and call it what it is a description.

Our `help` command already creates its own *Usage* section based on the
available arguments and doesn't refer to the description with usage.

# User-Facing Changes

`help commands` and `scope commands` will now use `description` or
`extra_description`
`usage`-> `description`
`extra_usage` -> `extra_description`

Breaking change in the plugin protocol:

In the signature record communicated with the engine.
`usage`-> `description`
`extra_usage` -> `extra_description`

The same rename also takes place for the methods on
`SimplePluginCommand` and `PluginCommand`

# Tests + Formatting
- Updated plugin protocol specific changes
# After Submitting
- [ ] update plugin protocol doc
2024-08-22 12:02:08 +02:00

81 lines
2.3 KiB
Rust

use std::path::PathBuf;
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{Category, FromValue, LabeledError, Signature, Spanned, Type, Value};
use crate::ExamplePlugin;
pub struct Config;
/// Example config struct.
///
/// Using the `FromValue` derive macro, structs can be easily loaded from [`Value`]s,
/// similar to serde's `Deserialize` macro.
/// This is handy for plugin configs or piped data.
/// All fields must implement [`FromValue`].
/// For [`Option`] fields, they can be omitted in the config.
///
/// This example shows that nested and spanned data work too, so you can describe nested
/// structures and get spans of values wrapped in [`Spanned`].
/// Since this config uses only `Option`s, no field is required in the config.
#[allow(dead_code)]
#[derive(Debug, FromValue)]
struct PluginConfig {
path: Option<Spanned<PathBuf>>,
nested: Option<SubConfig>,
}
#[allow(dead_code)]
#[derive(Debug, FromValue)]
struct SubConfig {
bool: bool,
string: String,
}
impl SimplePluginCommand for Config {
type Plugin = ExamplePlugin;
fn name(&self) -> &str {
"example config"
}
fn description(&self) -> &str {
"Show plugin configuration"
}
fn extra_description(&self) -> &str {
"The configuration is set under $env.config.plugins.example"
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.category(Category::Experimental)
.input_output_type(Type::Nothing, Type::table())
}
fn search_terms(&self) -> Vec<&str> {
vec!["example", "configuration"]
}
fn run(
&self,
_plugin: &ExamplePlugin,
engine: &EngineInterface,
call: &EvaluatedCall,
_input: &Value,
) -> Result<Value, LabeledError> {
let config = engine.get_plugin_config()?;
match config {
Some(value) => {
let config = PluginConfig::from_value(value.clone())?;
println!("got config {config:?}");
Ok(value)
}
None => Err(LabeledError::new("No config sent").with_label(
"configuration for this plugin was not found in `$env.config.plugins.example`",
call.head,
)),
}
}
}