Improve key specific bash subcompletions

The obscure `case "${words[$cword-2]}$prev=" in` idiom is no
longer used for key specific completions in options with map values.

The `__docker_map_key_of_current_option()` function does a much
better job.

Signed-off-by: Harald Albers <github@albersweb.de>
This commit is contained in:
Harald Albers 2016-02-07 10:05:43 -08:00 committed by Tibor Vass
parent 904481becd
commit fcad8ce206
1 changed files with 69 additions and 63 deletions

View File

@ -227,7 +227,6 @@ __docker_pos_first_nonflag() {
# If we are currently completing the value of a map option (key=value) # If we are currently completing the value of a map option (key=value)
# which matches the extglob given as an argument, returns key. # which matches the extglob given as an argument, returns key.
# This function is needed for key-specific completions. # This function is needed for key-specific completions.
# TODO use this in all "${words[$cword-2]}$prev=" occurrences
__docker_map_key_of_current_option() { __docker_map_key_of_current_option() {
local glob="$1" local glob="$1"
@ -452,21 +451,20 @@ __docker_complete_log_options() {
} }
__docker_complete_log_driver_options() { __docker_complete_log_driver_options() {
# "=" gets parsed to a word and assigned to either $cur or $prev depending on whether local key=$(__docker_map_key_of_current_option '--log-opt')
# it is the last character or not. So we search for "xxx=" in the the last two words. case "$key" in
case "${words[$cword-2]}$prev=" in gelf-address)
*gelf-address=*) COMPREPLY=( $( compgen -W "udp" -S "://" -- "${cur##*=}" ) )
COMPREPLY=( $( compgen -W "udp" -S "://" -- "${cur#=}" ) )
__docker_nospace __docker_nospace
return return
;; ;;
*syslog-address=*) syslog-address)
COMPREPLY=( $( compgen -W "tcp:// tcp+tls:// udp:// unix://" -- "${cur#=}" ) ) COMPREPLY=( $( compgen -W "tcp:// tcp+tls:// udp:// unix://" -- "${cur##*=}" ) )
__docker_nospace __docker_nospace
__ltrim_colon_completions "${cur}" __ltrim_colon_completions "${cur}"
return return
;; ;;
*syslog-facility=*) syslog-facility)
COMPREPLY=( $( compgen -W " COMPREPLY=( $( compgen -W "
auth auth
authpriv authpriv
@ -488,26 +486,25 @@ __docker_complete_log_driver_options() {
syslog syslog
user user
uucp uucp
" -- "${cur#=}" ) ) " -- "${cur##*=}" ) )
return return
;; ;;
*syslog-tls-@(ca-cert|cert|key)=*) syslog-tls-@(ca-cert|cert|key))
_filedir _filedir
return return
;; ;;
*syslog-tls-skip-verify=*) syslog-tls-skip-verify)
COMPREPLY=( $( compgen -W "true" -- "${cur#=}" ) ) COMPREPLY=( $( compgen -W "true" -- "${cur##*=}" ) )
return return
;; ;;
*splunk-url=*) splunk-url)
COMPREPLY=( $( compgen -W "http:// https://" -- "${cur#=}" ) ) COMPREPLY=( $( compgen -W "http:// https://" -- "${cur##*=}" ) )
__docker_nospace __docker_nospace
__ltrim_colon_completions "${cur}" __ltrim_colon_completions "${cur}"
return return
;; ;;
*splunk-insecureskipverify=*) splunk-insecureskipverify)
COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) ) COMPREPLY=( $( compgen -W "false true" -- "${cur##*=}" ) )
__docker_nospace
return return
;; ;;
esac esac
@ -789,26 +786,31 @@ _docker_daemon() {
__docker_complete_log_driver_options && return __docker_complete_log_driver_options && return
case "${words[$cword-2]}$prev=" in key=$(__docker_map_key_of_current_option '--cluster-store-opt')
# completions for --storage-opt case "$key" in
*dm.@(blkdiscard|override_udev_sync_check|use_deferred_@(removal|deletion))=*) kv.*file)
COMPREPLY=( $( compgen -W "false true" -- "${cur#=}" ) ) cur=${cur##*=}
return _filedir
;; return
*dm.fs=*) ;;
COMPREPLY=( $( compgen -W "ext4 xfs" -- "${cur#=}" ) ) esac
return
;; local key=$(__docker_map_key_of_current_option '--storage-opt')
*dm.thinpooldev=*) case "$key" in
_filedir dm.@(blkdiscard|override_udev_sync_check|use_deferred_@(removal|deletion)))
return COMPREPLY=( $( compgen -W "false true" -- "${cur##*=}" ) )
;; return
# completions for --cluster-store-opt ;;
*kv.*file=*) dm.fs)
_filedir COMPREPLY=( $( compgen -W "ext4 xfs" -- "${cur##*=}" ) )
return return
;; ;;
esac dm.thinpooldev)
cur=${cur##*=}
_filedir
return
;;
esac
case "$prev" in case "$prev" in
--authorization-plugin) --authorization-plugin)
@ -914,8 +916,8 @@ _docker_diff() {
} }
_docker_events() { _docker_events() {
local filter=$(__docker_map_key_of_current_option '-f|--filter') local key=$(__docker_map_key_of_current_option '-f|--filter')
case "$filter" in case "$key" in
container) container)
cur="${cur##*=}" cur="${cur##*=}"
__docker_complete_containers_all __docker_complete_containers_all
@ -1051,12 +1053,13 @@ _docker_history() {
} }
_docker_images() { _docker_images() {
case "${words[$cword-2]}$prev=" in local key=$(__docker_map_key_of_current_option '--filter|-f')
*dangling=*) case "$key" in
COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) ) dangling)
COMPREPLY=( $( compgen -W "false true" -- "${cur##*=}" ) )
return return
;; ;;
*label=*) label)
return return
;; ;;
esac esac
@ -1329,19 +1332,20 @@ _docker_network_inspect() {
} }
_docker_network_ls() { _docker_network_ls() {
case "${words[$cword-2]}$prev=" in local key=$(__docker_map_key_of_current_option '--filter|-f')
*id=*) case "$key" in
cur="${cur#=}" id)
cur="${cur##*=}"
__docker_complete_network_ids __docker_complete_network_ids
return return
;; ;;
*name=*) name)
cur="${cur#=}" cur="${cur##*=}"
__docker_complete_network_names __docker_complete_network_names
return return
;; ;;
*type=*) type)
COMPREPLY=( $( compgen -W "builtin custom" -- "${cur#=}" ) ) COMPREPLY=( $( compgen -W "builtin custom" -- "${cur##*=}" ) )
return return
;; ;;
esac esac
@ -1421,24 +1425,25 @@ _docker_port() {
} }
_docker_ps() { _docker_ps() {
case "${words[$cword-2]}$prev=" in local key=$(__docker_map_key_of_current_option '--filter|-f')
*ancestor=*) case "$key" in
cur="${cur#=}" ancestor)
cur="${cur##*=}"
__docker_complete_images __docker_complete_images
return return
;; ;;
*id=*) id)
cur="${cur#=}" cur="${cur##*=}"
__docker_complete_container_ids __docker_complete_container_ids
return return
;; ;;
*name=*) name)
cur="${cur#=}" cur="${cur##*=}"
__docker_complete_container_names __docker_complete_container_names
return return
;; ;;
*status=*) status)
COMPREPLY=( $( compgen -W "created dead exited paused restarting running" -- "${cur#=}" ) ) COMPREPLY=( $( compgen -W "created dead exited paused restarting running" -- "${cur##*=}" ) )
return return
;; ;;
esac esac
@ -2015,9 +2020,10 @@ _docker_volume_inspect() {
} }
_docker_volume_ls() { _docker_volume_ls() {
case "${words[$cword-2]}$prev=" in local key=$(__docker_map_key_of_current_option '--filter|-f')
*dangling=*) case "$key" in
COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) ) dangling)
COMPREPLY=( $( compgen -W "true false" -- "${cur##*=}" ) )
return return
;; ;;
esac esac