Merge pull request #9863 from albers/bash-completion

Add options to bash completion and sort options
This commit is contained in:
Tianon Gravi 2015-01-01 09:49:29 -07:00
commit b2ab733c99
1 changed files with 117 additions and 73 deletions

View File

@ -20,6 +20,11 @@
# bound to the default communication port/socket # bound to the default communication port/socket
# If the docker daemon is using a unix socket for communication your user # If the docker daemon is using a unix socket for communication your user
# must have access to the socket for the completions to function correctly # must have access to the socket for the completions to function correctly
#
# Note for developers:
# Please arrange options sorted alphabetically by long name with the short
# options immediately following their corresponding long form.
# This order should be applied to lists, alternatives and code blocks.
__docker_q() { __docker_q() {
docker 2>/dev/null "$@" docker 2>/dev/null "$@"
@ -181,7 +186,7 @@ _docker_attach() {
_docker_build() { _docker_build() {
case "$prev" in case "$prev" in
-t|--tag) --tag|-t)
__docker_image_repos_and_tags __docker_image_repos_and_tags
return return
;; ;;
@ -189,10 +194,10 @@ _docker_build() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-t --tag -q --quiet --no-cache --rm --force-rm" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--force-rm --no-cache --quiet -q --rm --tag -t" -- "$cur" ) )
;; ;;
*) *)
local counter="$(__docker_pos_first_nonflag '-t|--tag')" local counter="$(__docker_pos_first_nonflag '--tag|-t')"
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
_filedir -d _filedir -d
fi fi
@ -202,17 +207,17 @@ _docker_build() {
_docker_commit() { _docker_commit() {
case "$prev" in case "$prev" in
-m|--message|-a|--author|--run) --author|-a|--message|-m|--run)
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-m --message -a --author --run" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--author -a --message -m --run" -- "$cur" ) )
;; ;;
*) *)
local counter=$(__docker_pos_first_nonflag '-m|--message|-a|--author|--run') local counter=$(__docker_pos_first_nonflag '--author|-a|--message|-m|--run')
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_containers_all __docker_containers_all
@ -279,7 +284,7 @@ _docker_events() {
_docker_exec() { _docker_exec() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-d --detach -i --interactive -t --tty" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--detach -d --interactive -i -t --tty" -- "$cur" ) )
;; ;;
*) *)
__docker_containers_running __docker_containers_running
@ -304,7 +309,7 @@ _docker_help() {
_docker_history() { _docker_history() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-q --quiet --no-trunc" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--no-trunc --quiet -q" -- "$cur" ) )
;; ;;
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
@ -318,7 +323,7 @@ _docker_history() {
_docker_images() { _docker_images() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-q --quiet -a --all --no-trunc -v --viz -t --tree" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--all -a --no-trunc --quiet -q" -- "$cur" ) )
;; ;;
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
@ -348,14 +353,14 @@ _docker_info() {
_docker_inspect() { _docker_inspect() {
case "$prev" in case "$prev" in
-f|--format) --format|-f)
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-f --format" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--format -f" -- "$cur" ) )
;; ;;
*) *)
__docker_containers_and_images __docker_containers_and_images
@ -368,19 +373,30 @@ _docker_kill() {
} }
_docker_load() { _docker_load() {
return
}
_docker_login() {
case "$prev" in case "$prev" in
-u|--username|-p|--password|-e|--email) --input|-i)
_filedir
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-u --username -p --password -e --email" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--input -i" -- "$cur" ) )
;;
esac
}
_docker_login() {
case "$prev" in
--email|-e|--password|-p|--username|-u)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--email -e --password -p --username -u" -- "$cur" ) )
;; ;;
esac esac
} }
@ -388,7 +404,7 @@ _docker_login() {
_docker_logs() { _docker_logs() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-f --follow" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--follow -f" -- "$cur" ) )
;; ;;
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)
@ -415,7 +431,7 @@ _docker_port() {
_docker_ps() { _docker_ps() {
case "$prev" in case "$prev" in
--since|--before) --before|--since)
__docker_containers_all __docker_containers_all
;; ;;
-n) -n)
@ -425,24 +441,24 @@ _docker_ps() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-q --quiet -s --size -a --all --no-trunc -l --latest --since --before -n" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--all -a --before --latest -l --no-trunc -n --quiet -q --size -s --since" -- "$cur" ) )
;; ;;
esac esac
} }
_docker_pull() { _docker_pull() {
case "$prev" in case "$prev" in
-t|--tag) --tag|-t)
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-t --tag" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--tag -t" -- "$cur" ) )
;; ;;
*) *)
local counter=$(__docker_pos_first_nonflag '-t|--tag') local counter=$(__docker_pos_first_nonflag '--tag|-t')
if [ $cword -eq $counter ]; then if [ $cword -eq $counter ]; then
__docker_image_repos_and_tags __docker_image_repos_and_tags
fi fi
@ -459,14 +475,14 @@ _docker_push() {
_docker_restart() { _docker_restart() {
case "$prev" in case "$prev" in
-t|--time) --time|-t)
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--time -t" -- "$cur" ) )
;; ;;
*) *)
__docker_containers_all __docker_containers_all
@ -477,13 +493,13 @@ _docker_restart() {
_docker_rm() { _docker_rm() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-f --force -l --link -v --volumes" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--force -f --link -l --volumes -v" -- "$cur" ) )
return return
;; ;;
*) *)
for arg in "${COMP_WORDS[@]}"; do for arg in "${COMP_WORDS[@]}"; do
case "$arg" in case "$arg" in
-f|--force) --force|-f)
__docker_containers_all __docker_containers_all
return return
;; ;;
@ -501,62 +517,72 @@ _docker_rmi() {
_docker_run() { _docker_run() {
local options_with_args=" local options_with_args="
-a --attach
--add-host --add-host
--attach -a
--cap-add --cap-add
--cap-drop --cap-drop
-c --cpu-shares
--cidfile --cidfile
--cpuset --cpuset
--cpu-shares -c
--device --device
--dns --dns
--dns-search --dns-search
-e --env
--entrypoint --entrypoint
--env -e
--env-file --env-file
--expose --expose
-h --hostname --hostname -h
--ipc
--link --link
--lxc-conf --lxc-conf
-m --memory --mac-address
--memory -m
--name --name
--net --net
-p --publish --publish -p
--restart --restart
--security-opt --security-opt
-u --user --user -u
--volumes-from --volumes-from
-v --volume --volume -v
-w --workdir --workdir -w
" "
local all_options="$options_with_args local all_options="$options_with_args
-i --interactive --interactive -i
-P --publish-all
--privileged --privileged
-t --tty --publish-all -P
--tty -t
" "
[ "$command" = "run" ] && all_options="$all_options [ "$command" = "run" ] && all_options="$all_options
-d --detach --detach -d
--rm --rm
--sig-proxy --sig-proxy
" "
case "$prev" in case "$prev" in
-a|--attach) --add-host)
case "$cur" in
*:)
__docker_resolve_hostname
return
;;
esac
;;
--attach|-a)
COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) ) COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
return return
;; ;;
--cap-add|--cap-drop)
__docker_capabilities
return
;;
--cidfile|--env-file) --cidfile|--env-file)
_filedir _filedir
return return
;; ;;
--volumes-from) --device|-d|--volume)
__docker_containers_all
return
;;
-v|--volume|--device)
case "$cur" in case "$cur" in
*:*) *:*)
# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine) # TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
@ -572,11 +598,26 @@ _docker_run() {
esac esac
return return
;; ;;
-e|--env) --env|-e)
COMPREPLY=( $( compgen -e -- "$cur" ) ) COMPREPLY=( $( compgen -e -- "$cur" ) )
compopt -o nospace compopt -o nospace
return return
;; ;;
--ipc)
case "$cur" in
*:*)
cur="${cur#*:}"
__docker_containers_running
;;
*)
COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
if [ "$COMPREPLY" = "container:" ]; then
compopt -o nospace
fi
;;
esac
return
;;
--link) --link)
case "$cur" in case "$cur" in
*:*) *:*)
@ -589,18 +630,6 @@ _docker_run() {
esac esac
return return
;; ;;
--add-host)
case "$cur" in
*:)
__docker_resolve_hostname
return
;;
esac
;;
--cap-add|--cap-drop)
__docker_capabilities
return
;;
--net) --net)
case "$cur" in case "$cur" in
container:*) container:*)
@ -644,7 +673,11 @@ _docker_run() {
esac esac
return return
;; ;;
--entrypoint|-h|--hostname|-m|--memory|-u|--user|-w|--workdir|--cpuset|-c|--cpu-shares|-n|--name|-p|--publish|--expose|--dns|--lxc-conf|--dns-search) --volumes-from)
__docker_containers_all
return
;;
--cpuset|--cpu-shares|-c|--dns|--dns-search|--entrypoint|--expose|--hostname|-h|--lxc-conf|--mac-address|--memory|-m|--name|-n|--publish|-p|--user|-u|--workdir|-w)
return return
;; ;;
esac esac
@ -664,22 +697,33 @@ _docker_run() {
} }
_docker_save() { _docker_save() {
local counter=$(__docker_pos_first_nonflag)
if [ $cword -eq $counter ]; then
__docker_image_repos_and_tags_and_ids
fi
}
_docker_search() {
case "$prev" in case "$prev" in
-s|--stars) --output|-o)
_filedir
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "--no-trunc --automated -s --stars" -- "$cur" ) ) COMPREPLY=( $( compgen -W "-o --output" -- "$cur" ) )
;;
*)
__docker_image_repos_and_tags_and_ids
;;
esac
}
_docker_search() {
case "$prev" in
--stars|-s)
return
;;
esac
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--automated --no-trunc --stars -s" -- "$cur" ) )
;; ;;
esac esac
} }
@ -687,7 +731,7 @@ _docker_search() {
_docker_start() { _docker_start() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-a --attach -i --interactive" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--attach -a --interactive -i" -- "$cur" ) )
;; ;;
*) *)
__docker_containers_stopped __docker_containers_stopped
@ -697,14 +741,14 @@ _docker_start() {
_docker_stop() { _docker_stop() {
case "$prev" in case "$prev" in
-t|--time) --time|-t)
return return
;; ;;
esac esac
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-t --time" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--time -t" -- "$cur" ) )
;; ;;
*) *)
__docker_containers_running __docker_containers_running
@ -715,7 +759,7 @@ _docker_stop() {
_docker_tag() { _docker_tag() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "-f --force" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--force -f" -- "$cur" ) )
;; ;;
*) *)
local counter=$(__docker_pos_first_nonflag) local counter=$(__docker_pos_first_nonflag)