mirror of
https://github.com/nushell/nushell
synced 2024-11-15 01:17:07 +00:00
Include subcommands in help commands
(#2575)
* Add minor fixes to comments * Include subcommands in `help commands`
This commit is contained in:
parent
422b6ca871
commit
193c4cc6d5
3 changed files with 104 additions and 53 deletions
|
@ -63,64 +63,114 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
|
||||||
let mut sorted_names = registry.names();
|
let mut sorted_names = registry.names();
|
||||||
sorted_names.sort();
|
sorted_names.sort();
|
||||||
|
|
||||||
Ok(
|
let (mut subcommand_names, command_names) = sorted_names
|
||||||
futures::stream::iter(sorted_names.into_iter().filter_map(move |cmd_name| {
|
.into_iter()
|
||||||
// If it's a subcommand, don't list it during the commands list
|
// Internal only commands shouldn't be displayed
|
||||||
if cmd_name.contains(' ') {
|
.filter(|cmd_name| {
|
||||||
return None;
|
registry
|
||||||
}
|
.get_command(&cmd_name)
|
||||||
|
.filter(|command| !command.is_internal())
|
||||||
|
.is_some()
|
||||||
|
})
|
||||||
|
.partition::<Vec<_>, _>(|cmd_name| cmd_name.contains(' '));
|
||||||
|
|
||||||
// Internal only commands shouldn't be displayed
|
fn process_name(
|
||||||
let command = match registry.get_command(&cmd_name) {
|
dict: &mut TaggedDictBuilder,
|
||||||
Some(c) => c,
|
cmd_name: &str,
|
||||||
None => return None,
|
registry: CommandRegistry,
|
||||||
};
|
rest: Vec<Tagged<String>>,
|
||||||
if command.is_internal() {
|
name: Tag,
|
||||||
return None;
|
) -> Result<(), ShellError> {
|
||||||
};
|
let document_tag = rest[0].tag.clone();
|
||||||
|
let value = command_dict(
|
||||||
|
registry.get_command(&cmd_name).ok_or_else(|| {
|
||||||
|
ShellError::labeled_error(
|
||||||
|
format!("Could not load {}", cmd_name),
|
||||||
|
"could not load command",
|
||||||
|
document_tag,
|
||||||
|
)
|
||||||
|
})?,
|
||||||
|
name,
|
||||||
|
);
|
||||||
|
|
||||||
let mut short_desc = TaggedDictBuilder::new(name.clone());
|
dict.insert_untagged("name", cmd_name);
|
||||||
let document_tag = rest[0].tag.clone();
|
dict.insert_untagged(
|
||||||
let value = command_dict(
|
"description",
|
||||||
match registry.get_command(&cmd_name).ok_or_else(|| {
|
get_data_by_key(&value, "usage".spanned_unknown())
|
||||||
|
.ok_or_else(|| {
|
||||||
ShellError::labeled_error(
|
ShellError::labeled_error(
|
||||||
format!("Could not load {}", cmd_name),
|
"Expected a usage key",
|
||||||
"could not load command",
|
"expected a 'usage' key",
|
||||||
document_tag,
|
&value.tag,
|
||||||
)
|
)
|
||||||
}) {
|
})?
|
||||||
Ok(ok) => ok,
|
.as_string()?,
|
||||||
Err(err) => return Some(Err(err)),
|
);
|
||||||
},
|
|
||||||
name.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
short_desc.insert_untagged("name", cmd_name);
|
//ReturnSuccess::value(dict.into_value())
|
||||||
short_desc.insert_untagged(
|
Ok(())
|
||||||
"description",
|
}
|
||||||
match match get_data_by_key(&value, "usage".spanned_unknown()).ok_or_else(
|
|
||||||
|| {
|
|
||||||
ShellError::labeled_error(
|
|
||||||
"Expected a usage key",
|
|
||||||
"expected a 'usage' key",
|
|
||||||
&value.tag,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
Ok(ok) => ok,
|
|
||||||
Err(err) => return Some(Err(err)),
|
|
||||||
}
|
|
||||||
.as_string()
|
|
||||||
{
|
|
||||||
Ok(ok) => ok,
|
|
||||||
Err(err) => return Some(Err(err)),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
Some(ReturnSuccess::value(short_desc.into_value()))
|
fn make_subcommands_table(
|
||||||
}))
|
subcommand_names: &mut Vec<String>,
|
||||||
.to_output_stream(),
|
cmd_name: &str,
|
||||||
)
|
registry: CommandRegistry,
|
||||||
|
rest: Vec<Tagged<String>>,
|
||||||
|
name: Tag,
|
||||||
|
) -> Result<Value, ShellError> {
|
||||||
|
let (matching, not_matching) = subcommand_names
|
||||||
|
.drain(..)
|
||||||
|
.partition(|subcommand_name| subcommand_name.starts_with(cmd_name));
|
||||||
|
*subcommand_names = not_matching;
|
||||||
|
Ok(if !matching.is_empty() {
|
||||||
|
UntaggedValue::table(
|
||||||
|
&(matching
|
||||||
|
.into_iter()
|
||||||
|
.map(|cmd_name: String| -> Result<_, ShellError> {
|
||||||
|
let mut short_desc = TaggedDictBuilder::new(name.clone());
|
||||||
|
process_name(
|
||||||
|
&mut short_desc,
|
||||||
|
&cmd_name,
|
||||||
|
registry.clone(),
|
||||||
|
rest.clone(),
|
||||||
|
name.clone(),
|
||||||
|
)?;
|
||||||
|
Ok(short_desc.into_value())
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<_>, _>>()?[..]),
|
||||||
|
)
|
||||||
|
.into_value(name)
|
||||||
|
} else {
|
||||||
|
UntaggedValue::nothing().into_value(name)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let iterator =
|
||||||
|
command_names
|
||||||
|
.into_iter()
|
||||||
|
.map(move |cmd_name| -> Result<_, ShellError> {
|
||||||
|
let mut short_desc = TaggedDictBuilder::new(name.clone());
|
||||||
|
process_name(
|
||||||
|
&mut short_desc,
|
||||||
|
&cmd_name,
|
||||||
|
registry.clone(),
|
||||||
|
rest.clone(),
|
||||||
|
name.clone(),
|
||||||
|
)?;
|
||||||
|
short_desc.insert_value(
|
||||||
|
"subcommands",
|
||||||
|
make_subcommands_table(
|
||||||
|
&mut subcommand_names,
|
||||||
|
&cmd_name,
|
||||||
|
registry.clone(),
|
||||||
|
rest.clone(),
|
||||||
|
name.clone(),
|
||||||
|
)?,
|
||||||
|
);
|
||||||
|
ReturnSuccess::value(short_desc.into_value())
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(futures::stream::iter(iterator).to_output_stream())
|
||||||
} else if rest[0].item == "generate_docs" {
|
} else if rest[0].item == "generate_docs" {
|
||||||
Ok(OutputStream::one(ReturnSuccess::value(generate_docs(
|
Ok(OutputStream::one(ReturnSuccess::value(generate_docs(
|
||||||
®istry,
|
®istry,
|
||||||
|
|
|
@ -77,7 +77,7 @@ pub fn generate_docs(registry: &CommandRegistry) -> Value {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// Return documentation for each command
|
// Return documentation for each command
|
||||||
// Sub-commands are nested under there respective parent commands
|
// Sub-commands are nested under their respective parent commands
|
||||||
let mut table = Vec::new();
|
let mut table = Vec::new();
|
||||||
for name in sorted_names.iter() {
|
for name in sorted_names.iter() {
|
||||||
// Must be a sub-command, skip since it's being handled underneath when we hit the parent command
|
// Must be a sub-command, skip since it's being handled underneath when we hit the parent command
|
||||||
|
|
|
@ -83,6 +83,7 @@ impl UntaggedValue {
|
||||||
matches!(self, UntaggedValue::Primitive(Primitive::Filesize(_)))
|
matches!(self, UntaggedValue::Primitive(Primitive::Filesize(_)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true if this value represents a duration
|
||||||
pub fn is_duration(&self) -> bool {
|
pub fn is_duration(&self) -> bool {
|
||||||
matches!(self, UntaggedValue::Primitive(Primitive::Duration(_)))
|
matches!(self, UntaggedValue::Primitive(Primitive::Duration(_)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue