mirror of
https://github.com/nushell/nushell.git
synced 2025-05-08 17:02:59 +00:00
# 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
79 lines
2.2 KiB
Rust
79 lines
2.2 KiB
Rust
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
|
|
use nu_protocol::{
|
|
IntoSpanned, LabeledError, PipelineData, Record, Signature, Spanned, SyntaxShape, Value,
|
|
};
|
|
|
|
use crate::ExamplePlugin;
|
|
|
|
pub struct CallDecl;
|
|
|
|
impl PluginCommand for CallDecl {
|
|
type Plugin = ExamplePlugin;
|
|
|
|
fn name(&self) -> &str {
|
|
"example call-decl"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.required(
|
|
"name",
|
|
SyntaxShape::String,
|
|
"the name of the command to call",
|
|
)
|
|
.optional(
|
|
"named_args",
|
|
SyntaxShape::Record(vec![]),
|
|
"named arguments to pass to the command",
|
|
)
|
|
.rest(
|
|
"positional_args",
|
|
SyntaxShape::Any,
|
|
"positional arguments to pass to the command",
|
|
)
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Demonstrates calling other commands from plugins using `call_decl()`."
|
|
}
|
|
|
|
fn extra_description(&self) -> &str {
|
|
"
|
|
The arguments will not be typechecked at parse time. This command is for
|
|
demonstration only, and should not be used for anything real.
|
|
"
|
|
.trim()
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
_plugin: &ExamplePlugin,
|
|
engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, LabeledError> {
|
|
let name: Spanned<String> = call.req(0)?;
|
|
let named_args: Option<Record> = call.opt(1)?;
|
|
let positional_args: Vec<Value> = call.rest(2)?;
|
|
|
|
let decl_id = engine.find_decl(&name.item)?.ok_or_else(|| {
|
|
LabeledError::new(format!("Can't find `{}`", name.item))
|
|
.with_label("not in scope", name.span)
|
|
})?;
|
|
|
|
let mut new_call = EvaluatedCall::new(call.head);
|
|
|
|
for (key, val) in named_args.into_iter().flatten() {
|
|
new_call.add_named(key.into_spanned(val.span()), val);
|
|
}
|
|
|
|
for val in positional_args {
|
|
new_call.add_positional(val);
|
|
}
|
|
|
|
let result = engine.call_decl(decl_id, new_call, input, true, false)?;
|
|
|
|
Ok(result)
|
|
}
|
|
}
|