feat(clap_complete_fig): Add subcommand aliases support

This commit is contained in:
Federico Ciardi 2022-03-20 21:06:19 +01:00
parent fb39216caf
commit a404ab8680
2 changed files with 30 additions and 8 deletions

View file

@ -52,13 +52,35 @@ fn gen_fig_inner(
buffer.push_str(&format!("{:indent$}subcommands: [\n", "", indent = indent));
// generate subcommands
for subcommand in cmd.get_subcommands() {
buffer.push_str(&format!(
"{:indent$}{{\n{:indent$} name: \"{}\",\n",
"",
"",
subcommand.get_name(),
indent = indent + 2
));
let mut aliases: Vec<&str> = subcommand.get_all_aliases().collect();
if !aliases.is_empty() {
aliases.insert(0, subcommand.get_name());
buffer.push_str(&format!(
"{:indent$}{{\n{:indent$} name: [",
"",
"",
indent = indent + 2
));
buffer.push_str(
&aliases
.iter()
.map(|name| format!("\"{}\"", name))
.collect::<Vec<_>>()
.join(", "),
);
buffer.push_str("],\n");
} else {
buffer.push_str(&format!(
"{:indent$}{{\n{:indent$} name: \"{}\",\n",
"",
"",
subcommand.get_name(),
indent = indent + 2
));
}
if let Some(data) = subcommand.get_about() {
buffer.push_str(&format!(

View file

@ -53,7 +53,7 @@ const completion: Fig.Spec = {
},
},
{
name: "some-cmd-with-hyphens",
name: ["some-cmd-with-hyphens", "hyphen"],
options: [
{
name: ["-h", "--help"],