Improve bash generator

This commit is contained in:
Ajeet D'Souza 2021-10-14 22:51:40 +01:00 committed by Pavan Kumar Sunkara
parent 11453065c5
commit cad1879ab2
2 changed files with 30 additions and 34 deletions

View file

@ -1,9 +1,10 @@
// Std // Std
use std::io::Write; use std::{fmt::Write as _, io::Write};
// Internal // Internal
use crate::utils; use crate::utils;
use crate::Generator; use crate::Generator;
use clap::*; use clap::*;
/// Generate bash completion file /// Generate bash completion file
@ -191,32 +192,27 @@ fn all_options_for_path(app: &App, path: &str) -> String {
debug!("all_options_for_path: path={}", path); debug!("all_options_for_path: path={}", path);
let p = utils::find_subcommand_with_path(app, path.split("__").skip(1).collect()); let p = utils::find_subcommand_with_path(app, path.split("__").skip(1).collect());
let scs: Vec<_> = utils::subcommands(p).iter().map(|x| x.0.clone()).collect();
let opts = format!( let mut opts = String::new();
"{shorts} {longs} {pos} {subcmds}", for short in utils::shorts_and_visible_aliases(p) {
shorts = utils::shorts_and_visible_aliases(p) write!(&mut opts, "-{} ", short).unwrap();
.iter() }
.fold(String::new(), |acc, s| format!("{} -{}", acc, s)), for long in utils::longs_and_visible_aliases(p) {
longs = utils::longs_and_visible_aliases(p) write!(&mut opts, "--{} ", long).unwrap();
.iter() }
.fold(String::new(), |acc, l| format!("{} --{}", acc, l)), for pos in p.get_positionals() {
pos = p.get_positionals().fold(String::new(), |acc, p| { if let Some(vals) = pos.get_possible_values() {
if let Some(vals) = p.get_possible_values() { for value in vals {
format!( write!(&mut opts, "{} ", value.get_name()).unwrap();
"{} {}",
acc,
vals.iter()
.map(|x| x.get_name())
.collect::<Vec<_>>()
.join(" ")
)
} else {
format!("{} {}", acc, p)
} }
}), } else {
subcmds = scs.join(" "), write!(&mut opts, "{} ", pos).unwrap();
); }
}
for (sc, _) in utils::subcommands(p) {
write!(&mut opts, "{} ", sc).unwrap();
}
opts.pop();
opts opts
} }

View file

@ -58,7 +58,7 @@ static BASH: &str = r#"_myapp() {
case "${cmd}" in case "${cmd}" in
myapp) myapp)
opts=" -h -V --help --version <file> first second test help" opts="-h -V --help --version <file> first second test help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -72,7 +72,7 @@ static BASH: &str = r#"_myapp() {
return 0 return 0
;; ;;
myapp__help) myapp__help)
opts=" -h --help " opts="-h --help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -86,7 +86,7 @@ static BASH: &str = r#"_myapp() {
return 0 return 0
;; ;;
myapp__test) myapp__test)
opts=" -h -V --case --help --version " opts="-h -V --case --help --version"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -161,7 +161,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() {
case "${cmd}" in case "${cmd}" in
my_app) my_app)
opts=" -h -V --help --version <file> first second test some_cmd some-cmd-with-hypens help" opts="-h -V --help --version <file> first second test some_cmd some-cmd-with-hypens help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -175,7 +175,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() {
return 0 return 0
;; ;;
my_app__help) my_app__help)
opts=" -h --help " opts="-h --help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -189,7 +189,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() {
return 0 return 0
;; ;;
my_app__some__cmd__with__hypens) my_app__some__cmd__with__hypens)
opts=" -h -V --help --version " opts="-h -V --help --version"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -203,7 +203,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() {
return 0 return 0
;; ;;
my_app__some_cmd) my_app__some_cmd)
opts=" -h -V --config --help --version " opts="-h -V --config --help --version"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -221,7 +221,7 @@ static BASH_SPECIAL_CMDS: &str = r#"_my_app() {
return 0 return 0
;; ;;
my_app__test) my_app__test)
opts=" -h -V --case --help --version " opts="-h -V --case --help --version"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
@ -295,7 +295,7 @@ static BASH_ALIASES: &str = r#"_cmd() {
case "${cmd}" in case "${cmd}" in
cmd) cmd)
opts=" -h -V -F -f -O -o --help --version --flg --flag --opt --option <positional> " opts="-h -V -F -f -O -o --help --version --flg --flag --opt --option <positional>"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0