nu_command/help/
help_.rs

1use crate::help::{help_aliases, help_commands, help_modules};
2use nu_engine::command_prelude::*;
3
4#[derive(Clone)]
5pub struct Help;
6
7impl Command for Help {
8    fn name(&self) -> &str {
9        "help"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("help")
14            .input_output_types(vec![(Type::Nothing, Type::Any)])
15            .rest(
16                "rest",
17                SyntaxShape::String,
18                "The name of command, alias or module to get help on.",
19            )
20            .named(
21                "find",
22                SyntaxShape::String,
23                "string to find in command names, descriptions, and search terms",
24                Some('f'),
25            )
26            .category(Category::Core)
27    }
28
29    fn description(&self) -> &str {
30        "Display help information about different parts of Nushell."
31    }
32
33    fn extra_description(&self) -> &str {
34        r#"`help word` searches for "word" in commands, aliases and modules, in that order.
35If you want your own help implementation, create a custom command named `help` and it will also be used for `--help` invocations.
36There already is an alternative `help` command in the standard library you can try with `use std/help`."#
37    }
38
39    fn run(
40        &self,
41        engine_state: &EngineState,
42        stack: &mut Stack,
43        call: &Call,
44        _input: PipelineData,
45    ) -> Result<PipelineData, ShellError> {
46        let head = call.head;
47        let find: Option<Spanned<String>> = call.get_flag(engine_state, stack, "find")?;
48        let rest: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
49
50        if rest.is_empty() && find.is_none() {
51            let msg = r#"Welcome to Nushell.
52
53Here are some tips to help you get started.
54  * help -h or help help - show available `help` subcommands and examples
55  * help commands - list all available commands
56  * help <name> - display help about a particular command, alias, or module
57  * help --find <text to search> - search through all help commands table
58
59Nushell works on the idea of a "pipeline". Pipelines are commands connected with the '|' character.
60Each stage in the pipeline works together to load, parse, and display information to you.
61
62[Examples]
63
64List the files in the current directory, sorted by size:
65    ls | sort-by size
66
67Get the current system host name:
68    sys host | get hostname
69
70Get the processes on your system actively using CPU:
71    ps | where cpu > 0
72
73You can also learn more at https://www.nushell.sh/book/"#;
74
75            Ok(Value::string(msg, head).into_pipeline_data())
76        } else if find.is_some() {
77            help_commands(engine_state, stack, call)
78        } else {
79            let result = help_aliases(engine_state, stack, call);
80
81            let result = if let Err(ShellError::AliasNotFound { .. }) = result {
82                help_commands(engine_state, stack, call)
83            } else {
84                result
85            };
86
87            let result = if let Err(ShellError::CommandNotFound { .. }) = result {
88                help_modules(engine_state, stack, call)
89            } else {
90                result
91            };
92
93            if let Err(ShellError::ModuleNotFoundAtRuntime {
94                mod_name: _,
95                span: _,
96            }) = result
97            {
98                Err(ShellError::NotFound {
99                    span: Span::merge_many(rest.iter().map(|s| s.span)),
100                })
101            } else {
102                result
103            }
104        }
105    }
106
107    fn examples(&self) -> Vec<Example> {
108        vec![
109            Example {
110                description: "show help for single command, alias, or module",
111                example: "help match",
112                result: None,
113            },
114            Example {
115                description: "show help for single sub-command, alias, or module",
116                example: "help str join",
117                result: None,
118            },
119            Example {
120                description: "search for string in command names, descriptions, and search terms",
121                example: "help --find char",
122                result: None,
123            },
124        ]
125    }
126}