bash_completion: Refactor and simplify

Change cros_portage_upgrade to an already defined completion.
Change the name of said completion to not be _board_sysroot,
since it also completes flags from help.

BUG=none
TEST=unset _comp_cache
     declare -A _comp_cache
     ./build_package --<tab><tab>
TEST=unset _comp_cache
     declare -A _comp_cache
     cros_workon --<tab><tab>

Change-Id: I48d24a3bfb4d067f67011ffa8d86818be9ea119b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/1913091
Tested-by: Craig Hesling <hesling@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/bash_completion b/bash_completion
index bd4e3cf..b721699 100644
--- a/bash_completion
+++ b/bash_completion
@@ -32,7 +32,7 @@
 
 # Complete flags, i.e., current words starting with --. Return 1 if
 # the current word doesn't start with --, 0 otherwise.
-_flag_complete() {
+_complete_flag_help() {
   COMPREPLY=()
   local cur="${COMP_WORDS[COMP_CWORD]}"
   if [[ "${cur}" == --* ]]; then
@@ -95,8 +95,8 @@
 }
 
 # Completion for --board= argument for existing board sysroots
-_board_sysroot() {
-  _flag_complete && return 0
+_complete_basic() {
+  _complete_flag_help && return 0
   _complete_board_sysroot_flag && return 0
 }
 
@@ -110,7 +110,7 @@
 
 # Completion for --board= argument for existing board overlays
 _board_overlay() {
-  _flag_complete && return 0
+  _complete_flag_help && return 0
 
   COMPREPLY=()
   local arg=$(_argeq --board)
@@ -127,7 +127,7 @@
 }
 
 _autotest_complete() {
-  _flag_complete && return 0
+  _complete_flag_help && return 0
 
   local arg=$(_argeq -c)
   if [[ ${arg} == -c=* ]]; then
@@ -145,14 +145,14 @@
 }
 
 _test_that_complete() {
-  _flag_complete && return 0
+  _complete_flag_help && return 0
   return 0
 }
 
 # Complete cros_workon's <command> argument.
 #
 # TODO(petkov): We should probably extract the list of commands from
-# cros_workon --help, just like we do for flags (see _flag_complete).
+# cros_workon --help, just like we do for flags (see _complete_flag_help).
 #
 # TODO(petkov): Currently, this assumes that the command is the first
 # argument. In practice, the command is the first non-flag
@@ -220,7 +220,7 @@
 # Complete cros_workon arguments.
 _cros_workon() {
   COMPREPLY=()
-  _flag_complete && return 0
+  _complete_flag_help && return 0
   _complete_board_sysroot_flag && return 0
   _complete_cros_workon_command && return 0
   _complete_cros_workon_package && return 0
@@ -245,20 +245,13 @@
 # Complete cros arguments.
 _cros() {
   COMPREPLY=()
-  _flag_complete && return 0
+  _complete_flag_help && return 0
   _complete_board_sysroot_flag && return 0
   _complete_cros_command && return 0
   # TODO(hesling): Add package completion like cros_workon.
   return 0
 }
 
-_cros_portage_upgrade() {
-  COMPREPLY=()
-  _flag_complete && return 0
-  _complete_board_sysroot_flag && return 0
-  return 0
-}
-
 # Complete equery's <module-name> argument.
 _complete_equery_module_name() {
   [ ${COMP_CWORD} -eq 1 ] || return 1
@@ -276,18 +269,18 @@
   return 0
 }
 
-complete -o bashdefault -o default -F _board_sysroot \
+complete -o bashdefault -o default -F _complete_basic \
   build_autotest.sh \
   build_image \
   build_packages \
-  mod_image_for_test.sh
+  mod_image_for_test.sh \
+  cros_portage_upgrade
+
 complete -o bashdefault -o default -F _board_overlay setup_board
 complete -o bashdefault -o default -o nospace -F _autotest_complete autotest
 complete -o bashdefault -o default -o nospace -F _test_that_complete test_that
 complete -F _cros cros
 complete -F _cros_workon cros_workon
-complete -o bashdefault -o default -o nospace -F _cros_portage_upgrade \
-            cros_portage_upgrade
 complete -o bashdefault -o default -F _complete_equery equery
 
 # Use equery completion for equery-$board for known boards