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();
|
||||
sorted_names.sort();
|
||||
|
||||
Ok(
|
||||
futures::stream::iter(sorted_names.into_iter().filter_map(move |cmd_name| {
|
||||
// If it's a subcommand, don't list it during the commands list
|
||||
if cmd_name.contains(' ') {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (mut subcommand_names, command_names) = sorted_names
|
||||
.into_iter()
|
||||
// Internal only commands shouldn't be displayed
|
||||
let command = match registry.get_command(&cmd_name) {
|
||||
Some(c) => c,
|
||||
None => return None,
|
||||
};
|
||||
if command.is_internal() {
|
||||
return None;
|
||||
};
|
||||
.filter(|cmd_name| {
|
||||
registry
|
||||
.get_command(&cmd_name)
|
||||
.filter(|command| !command.is_internal())
|
||||
.is_some()
|
||||
})
|
||||
.partition::<Vec<_>, _>(|cmd_name| cmd_name.contains(' '));
|
||||
|
||||
let mut short_desc = TaggedDictBuilder::new(name.clone());
|
||||
fn process_name(
|
||||
dict: &mut TaggedDictBuilder,
|
||||
cmd_name: &str,
|
||||
registry: CommandRegistry,
|
||||
rest: Vec<Tagged<String>>,
|
||||
name: Tag,
|
||||
) -> Result<(), ShellError> {
|
||||
let document_tag = rest[0].tag.clone();
|
||||
let value = command_dict(
|
||||
match registry.get_command(&cmd_name).ok_or_else(|| {
|
||||
registry.get_command(&cmd_name).ok_or_else(|| {
|
||||
ShellError::labeled_error(
|
||||
format!("Could not load {}", cmd_name),
|
||||
"could not load command",
|
||||
document_tag,
|
||||
)
|
||||
}) {
|
||||
Ok(ok) => ok,
|
||||
Err(err) => return Some(Err(err)),
|
||||
},
|
||||
name.clone(),
|
||||
})?,
|
||||
name,
|
||||
);
|
||||
|
||||
short_desc.insert_untagged("name", cmd_name);
|
||||
short_desc.insert_untagged(
|
||||
dict.insert_untagged("name", cmd_name);
|
||||
dict.insert_untagged(
|
||||
"description",
|
||||
match match get_data_by_key(&value, "usage".spanned_unknown()).ok_or_else(
|
||||
|| {
|
||||
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)),
|
||||
},
|
||||
})?
|
||||
.as_string()?,
|
||||
);
|
||||
|
||||
Some(ReturnSuccess::value(short_desc.into_value()))
|
||||
}))
|
||||
.to_output_stream(),
|
||||
//ReturnSuccess::value(dict.into_value())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn make_subcommands_table(
|
||||
subcommand_names: &mut Vec<String>,
|
||||
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" {
|
||||
Ok(OutputStream::one(ReturnSuccess::value(generate_docs(
|
||||
®istry,
|
||||
|
|
|
@ -77,7 +77,7 @@ pub fn generate_docs(registry: &CommandRegistry) -> Value {
|
|||
};
|
||||
}
|
||||
// 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();
|
||||
for name in sorted_names.iter() {
|
||||
// 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(_)))
|
||||
}
|
||||
|
||||
/// Returns true if this value represents a duration
|
||||
pub fn is_duration(&self) -> bool {
|
||||
matches!(self, UntaggedValue::Primitive(Primitive::Duration(_)))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue