From b380322174c9b67da99ce743ff49382bab5cf351 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 26 Jul 2021 15:26:22 -0400 Subject: [PATCH] bash-completion: refactor unique subcommand processing Let's keep the main processing loop free of subcommand implementations by pulling the existing help & start commands into dedicated functions. Having a single giant function is harder to track as we add more and more logic in. Bug: https://crbug.com/gerrit/14797 Change-Id: I2b62dc430c0e7574f09aa4838f4ef03fbe4bf7fb Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/312903 Reviewed-by: Xin Li Tested-by: Mike Frysinger --- completion.bash | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/completion.bash b/completion.bash index 3c1fd683..04347ce3 100644 --- a/completion.bash +++ b/completion.bash @@ -66,6 +66,31 @@ __complete_repo_command_projects() { COMPREPLY=($(compgen -W "$(__complete_repo_list_projects)" -- "${current}")) } +# Complete `repo help`. +__complete_repo_command_help() { + local current=$1 + # CWORD=1 is "start". + # CWORD=2 is the which we complete here. + if [[ ${COMP_CWORD} -eq 2 ]]; then + COMPREPLY=( + $(compgen -W "$(__complete_repo_list_commands)" -- "${current}") + ) + fi +} + +# Complete `repo start`. +__complete_repo_command_start() { + local current=$1 + # CWORD=1 is "start". + # CWORD=2 is the which we don't complete. + # CWORD=3+ are which we complete here. + if [[ ${COMP_CWORD} -gt 2 ]]; then + COMPREPLY=( + $(compgen -W "$(__complete_repo_list_projects)" -- "${current}") + ) + fi +} + # Complete the repo subcommand arguments. __complete_repo_arg() { if [[ ${COMP_CWORD} -le 1 ]]; then @@ -86,21 +111,8 @@ __complete_repo_arg() { return 0 ;; - help) - if [[ ${COMP_CWORD} -eq 2 ]]; then - COMPREPLY=( - $(compgen -W "$(__complete_repo_list_commands)" -- "${current}") - ) - fi - return 0 - ;; - - start) - if [[ ${COMP_CWORD} -gt 2 ]]; then - COMPREPLY=( - $(compgen -W "$(__complete_repo_list_projects)" -- "${current}") - ) - fi + help|start) + __complete_repo_command_${command} "${current}" return 0 ;;