1use crate::help::{help_aliases, help_commands, help_modules};
2use nu_engine::{HELP_DECL_ID_PARSER_INFO, command_prelude::*, get_full_help};
3use nu_protocol::{DeclId, ast::Expr, engine::CommandType};
4
5#[derive(Clone)]
6pub struct Help;
7
8impl Command for Help {
9 fn name(&self) -> &str {
10 "help"
11 }
12
13 fn signature(&self) -> Signature {
14 Signature::build("help")
15 .input_output_types(vec![(Type::Nothing, Type::Any)])
16 .rest(
17 "rest",
18 SyntaxShape::String,
19 "The name of command, alias or module to get help on.",
20 )
21 .named(
22 "find",
23 SyntaxShape::String,
24 "String to find in command names, descriptions, and search terms.",
25 Some('f'),
26 )
27 .category(Category::Core)
28 }
29
30 fn description(&self) -> &str {
31 "Display help information about different parts of Nushell."
32 }
33
34 fn extra_description(&self) -> &str {
35 r#"`help word` searches for "word" in commands, aliases and modules, in that order.
36If you want your own help implementation, create a custom command named `help` and it will also be used for `--help` invocations.
37There already is an alternative `help` command in the standard library you can try with `use std/help`."#
38 }
39
40 fn run(
41 &self,
42 engine_state: &EngineState,
43 stack: &mut Stack,
44 call: &Call,
45 _input: PipelineData,
46 ) -> Result<PipelineData, ShellError> {
47 let head = call.head;
48 let find: Option<Spanned<String>> = call.get_flag(engine_state, stack, "find")?;
49 let rest: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
50
51 if let Some(resolved_decl_id) = resolved_help_decl_id(call, stack, engine_state) {
52 return Ok(help_for_decl_id(
53 engine_state,
54 stack,
55 head,
56 resolved_decl_id,
57 ));
58 }
59
60 if find.is_none()
62 && let Some(name) = builtin_help_lookup_name(&rest)
63 {
64 if let Some(decl_id) = find_builtin_decl_id(engine_state, &name) {
65 return Ok(help_for_decl_id(engine_state, stack, head, decl_id));
66 }
67
68 return Err(ShellError::NotFound {
69 span: Span::merge_many(rest.iter().map(|s| s.span)),
70 });
71 }
72
73 fn help_for_decl_id(
74 engine_state: &EngineState,
75 stack: &mut Stack,
76 head: Span,
77 decl_id: DeclId,
78 ) -> PipelineData {
79 let decl = engine_state.get_decl(decl_id);
80 let help = get_full_help(decl, engine_state, stack, head);
81 Value::string(help, head).into_pipeline_data()
82 }
83 if rest.is_empty() && find.is_none() {
84 let msg = r#"Welcome to Nushell.
85
86Here are some tips to help you get started.
87 * help -h or help help - show available `help` subcommands and examples
88 * help commands - list all available commands
89 * help <name> - display help about a particular command, alias, or module
90 * help --find <text to search> - search through all help commands table
91
92Nushell works on the idea of a "pipeline". Pipelines are commands connected with the '|' character.
93Each stage in the pipeline works together to load, parse, and display information to you.
94
95[Examples]
96
97List the files in the current directory, sorted by size:
98 ls | sort-by size
99
100Get the current system host name:
101 sys host | get hostname
102
103Get the processes on your system actively using CPU:
104 ps | where cpu > 0
105
106You can also learn more at https://www.nushell.sh/book/"#;
107
108 Ok(Value::string(msg, head).into_pipeline_data())
109 } else if find.is_some() {
110 help_commands(engine_state, stack, call)
111 } else {
112 let result = help_aliases(engine_state, stack, call);
113
114 let result = if let Err(ShellError::AliasNotFound { .. }) = result {
115 help_commands(engine_state, stack, call)
116 } else {
117 result
118 };
119
120 let result = if let Err(ShellError::CommandNotFound { .. }) = result {
121 help_modules(engine_state, stack, call)
122 } else {
123 result
124 };
125
126 if let Err(ShellError::ModuleNotFoundAtRuntime {
127 mod_name: _,
128 span: _,
129 }) = result
130 {
131 Err(ShellError::NotFound {
132 span: Span::merge_many(rest.iter().map(|s| s.span)),
133 })
134 } else {
135 result
136 }
137 }
138 }
139
140 fn examples(&self) -> Vec<Example<'_>> {
141 vec![
142 Example {
143 description: "show help for single command, alias, or module.",
144 example: "help match",
145 result: None,
146 },
147 Example {
148 description: "show help for single sub-command, alias, or module.",
149 example: "help str join",
150 result: None,
151 },
152 Example {
153 description: "search for string in command names, descriptions, and search terms.",
154 example: "help --find char",
155 result: None,
156 },
157 ]
158 }
159}
160
161fn resolved_help_decl_id(call: &Call, stack: &Stack, engine_state: &EngineState) -> Option<DeclId> {
164 call.get_parser_info(stack, HELP_DECL_ID_PARSER_INFO)
165 .and_then(|expr| match expr.expr {
166 Expr::Int(id) => usize::try_from(id).ok().map(DeclId::new),
167 _ => None,
168 })
169 .filter(|decl_id| decl_id.get() < engine_state.num_decls())
170}
171
172fn builtin_help_lookup_name(rest: &[Spanned<String>]) -> Option<String> {
175 let (first, tail) = rest.split_first()?;
176 let first = first.item.strip_prefix('%')?;
177
178 let mut name = String::new();
179 if !first.is_empty() {
180 name.push_str(first);
181 }
182
183 for item in tail {
184 if !name.is_empty() {
185 name.push(' ');
186 }
187 name.push_str(&item.item);
188 }
189
190 Some(name)
191}
192
193fn find_builtin_decl_id(engine_state: &EngineState, name: &str) -> Option<DeclId> {
195 for idx in (0..engine_state.num_decls()).rev() {
196 let decl_id = DeclId::new(idx);
197 let decl = engine_state.get_decl(decl_id);
198
199 if decl.command_type() == CommandType::Builtin && decl.name() == name {
200 return Some(decl_id);
201 }
202 }
203
204 None
205}