Add examples to commands (#1752)

* Pass &dyn WholeStreamCommand to get_help

* Add an optional example to the WholeStreamCommand trait

* Add an example to the alias command
This commit is contained in:
Elichai Turkel
2020-05-11 23:05:44 +03:00
committed by GitHub
parent 42eb658c37
commit c3a066eeb4
6 changed files with 44 additions and 31 deletions

View File

@ -350,6 +350,11 @@ impl EvaluatedCommandArgs {
}
}
pub struct Example {
pub example: &'static str,
pub description: &'static str,
}
pub trait WholeStreamCommand: Send + Sync {
fn name(&self) -> &str;
@ -368,6 +373,10 @@ pub trait WholeStreamCommand: Send + Sync {
fn is_binary(&self) -> bool {
false
}
fn examples(&self) -> &[Example] {
&[]
}
}
#[derive(Clone)]
@ -407,7 +416,7 @@ impl Command {
pub fn run(&self, args: CommandArgs, registry: &CommandRegistry) -> OutputStream {
if args.call_info.switch_present("help") {
get_help(self.name(), self.usage(), self.signature(), registry).into()
get_help(&*self.0, registry).into()
} else {
match self.0.run(args, registry) {
Ok(stream) => stream,
@ -419,6 +428,10 @@ impl Command {
pub fn is_binary(&self) -> bool {
self.0.is_binary()
}
pub fn stream_command(&self) -> &dyn WholeStreamCommand {
&*self.0
}
}
pub struct FnFilterCommand {