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

View File

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

View File

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