Include subcommands in help commands (#2575)

* Add minor fixes to comments

* Include subcommands in `help commands`
This commit is contained in:
Shaurya 2020-09-20 02:07:47 +05:30 committed by GitHub
parent 422b6ca871
commit 193c4cc6d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 53 deletions

View File

@ -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
if cmd_name.contains(' ') {
return None;
}
// Internal only commands shouldn't be displayed // Internal only commands shouldn't be displayed
let command = match registry.get_command(&cmd_name) { .filter(|cmd_name| {
Some(c) => c, registry
None => return None, .get_command(&cmd_name)
}; .filter(|command| !command.is_internal())
if command.is_internal() { .is_some()
return None; })
}; .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 document_tag = rest[0].tag.clone();
let value = command_dict( let value = command_dict(
match registry.get_command(&cmd_name).ok_or_else(|| { registry.get_command(&cmd_name).ok_or_else(|| {
ShellError::labeled_error( ShellError::labeled_error(
format!("Could not load {}", cmd_name), format!("Could not load {}", cmd_name),
"could not load command", "could not load command",
document_tag, document_tag,
) )
}) { })?,
Ok(ok) => ok, name,
Err(err) => return Some(Err(err)),
},
name.clone(),
); );
short_desc.insert_untagged("name", cmd_name); dict.insert_untagged("name", cmd_name);
short_desc.insert_untagged( dict.insert_untagged(
"description", "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( ShellError::labeled_error(
"Expected a usage key", "Expected a usage key",
"expected a 'usage' key", "expected a 'usage' key",
&value.tag, &value.tag,
) )
}, })?
) { .as_string()?,
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())) //ReturnSuccess::value(dict.into_value())
})) Ok(())
.to_output_stream(), }
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" { } else if rest[0].item == "generate_docs" {
Ok(OutputStream::one(ReturnSuccess::value(generate_docs( Ok(OutputStream::one(ReturnSuccess::value(generate_docs(
&registry, &registry,

View File

@ -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

View File

@ -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(_)))
} }