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."#
35 }
36
37 fn run(
38 &self,
39 engine_state: &EngineState,
40 stack: &mut Stack,
41 call: &Call,
42 _input: PipelineData,
43 ) -> Result<PipelineData, ShellError> {
44 let head = call.head;
45 let find: Option<Spanned<String>> = call.get_flag(engine_state, stack, "find")?;
46 let rest: Vec<Spanned<String>> = call.rest(engine_state, stack, 0)?;
47
48 if rest.is_empty() && find.is_none() {
49 let msg = r#"Welcome to Nushell.
50
51Here are some tips to help you get started.
52 * help -h or help help - show available `help` subcommands and examples
53 * help commands - list all available commands
54 * help <name> - display help about a particular command, alias, or module
55 * help --find <text to search> - search through all help commands table
56
57Nushell works on the idea of a "pipeline". Pipelines are commands connected with the '|' character.
58Each stage in the pipeline works together to load, parse, and display information to you.
59
60[Examples]
61
62List the files in the current directory, sorted by size:
63 ls | sort-by size
64
65Get the current system host name:
66 sys host | get hostname
67
68Get the processes on your system actively using CPU:
69 ps | where cpu > 0
70
71You can also learn more at https://www.nushell.sh/book/"#;
72
73 Ok(Value::string(msg, head).into_pipeline_data())
74 } else if find.is_some() {
75 help_commands(engine_state, stack, call)
76 } else {
77 let result = help_aliases(engine_state, stack, call);
78
79 let result = if let Err(ShellError::AliasNotFound { .. }) = result {
80 help_commands(engine_state, stack, call)
81 } else {
82 result
83 };
84
85 let result = if let Err(ShellError::CommandNotFound { .. }) = result {
86 help_modules(engine_state, stack, call)
87 } else {
88 result
89 };
90
91 if let Err(ShellError::ModuleNotFoundAtRuntime {
92 mod_name: _,
93 span: _,
94 }) = result
95 {
96 Err(ShellError::NotFound {
97 span: Span::merge_many(rest.iter().map(|s| s.span)),
98 })
99 } else {
100 result
101 }
102 }
103 }
104
105 fn examples(&self) -> Vec<Example> {
106 vec![
107 Example {
108 description: "show help for single command, alias, or module",
109 example: "help match",
110 result: None,
111 },
112 Example {
113 description: "show help for single sub-command, alias, or module",
114 example: "help str join",
115 result: None,
116 },
117 Example {
118 description: "search for string in command names, descriptions, and search terms",
119 example: "help --find char",
120 result: None,
121 },
122 ]
123 }
124}