zsh: allow option stacking for short options without arguments

This enables Zsh to understand commands like "docker run -it
ubuntu". However, by enabling this, this also makes Zsh completes
"docker run -u<tab>" with "docker run -uapprox" which is not valid. The
users have to put the space or the equal sign themselves before trying
to complete.

Therefore, this behavior is disabled by default. To enable it:

    zstyle ':completion:*:*:docker:*' option-stacking yes
    zstyle ':completion:*:*:docker-*:*' option-stacking yes

Signed-off-by: Vincent Bernat <vincent@bernat.im>
This commit is contained in:
Vincent Bernat 2015-10-16 23:29:15 +02:00 committed by Tibor Vass
parent 2102746489
commit b10fb43048
1 changed files with 60 additions and 51 deletions

View File

@ -38,6 +38,15 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
# Short-option stacking can be enabled with:
# zstyle ':completion:*:*:docker:*' option-stacking yes
# zstyle ':completion:*:*:docker-*:*' option-stacking yes
__docker_arguments() {
if zstyle -t ":completion:${curcontext}:" option-stacking; then
print -- -s
fi
}
__docker_get_containers() { __docker_get_containers() {
[[ $PREFIX = -* ]] && return 1 [[ $PREFIX = -* ]] && return 1
integer ret=1 integer ret=1
@ -244,13 +253,13 @@ __docker_network_subcommand() {
case "$words[1]" in case "$words[1]" in
(connect|disconnect) (connect|disconnect)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)1:network:__docker_networks" \ "($help -)1:network:__docker_networks" \
"($help -)2:containers:__docker_runningcontainers" && ret=0 "($help -)2:containers:__docker_runningcontainers" && ret=0
;; ;;
(create) (create)
_arguments -A '-*' \ _arguments $(__docker_arguments) -A '-*' \
$opts_help \ $opts_help \
"($help -d --driver)"{-d=,--driver=}"[Driver to manage the Network]:driver:(null host bridge overlay)" \ "($help -d --driver)"{-d=,--driver=}"[Driver to manage the Network]:driver:(null host bridge overlay)" \
"($help)--ipam-driver=[IP Address Management Driver]:driver:(default)" \ "($help)--ipam-driver=[IP Address Management Driver]:driver:(default)" \
@ -262,18 +271,18 @@ __docker_network_subcommand() {
"($help -)1:Network Name: " && ret=0 "($help -)1:Network Name: " && ret=0
;; ;;
(inspect|rm) (inspect|rm)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)*:network:__docker_networks" && ret=0 "($help -)*:network:__docker_networks" && ret=0
;; ;;
(ls) (ls)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help)--no-trunc[Do not truncate the output]" \ "($help)--no-trunc[Do not truncate the output]" \
"($help -q --quiet)"{-q,--quiet}"[Only display numeric IDs]" && ret=0 "($help -q --quiet)"{-q,--quiet}"[Only display numeric IDs]" && ret=0
;; ;;
(help) (help)
_arguments ":subcommand:__docker_network_commands" && ret=0 _arguments $(__docker_arguments) ":subcommand:__docker_network_commands" && ret=0
;; ;;
esac esac
@ -332,31 +341,31 @@ __docker_volume_subcommand() {
case "$words[1]" in case "$words[1]" in
(create) (create)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -d --driver)"{-d=,--driver=}"[Specify volume driver name]:Driver name: " \ "($help -d --driver)"{-d=,--driver=}"[Specify volume driver name]:Driver name: " \
"($help)--name=[Specify volume name]" \ "($help)--name=[Specify volume name]" \
"($help)*"{-o=,--opt=}"[Set driver specific options]:Driver option: " && ret=0 "($help)*"{-o=,--opt=}"[Set driver specific options]:Driver option: " && ret=0
;; ;;
(inspect) (inspect)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -f --format)"{-f=,--format=}"[Format the output using the given go template]:template: " \ "($help -f --format)"{-f=,--format=}"[Format the output using the given go template]:template: " \
"($help -)1:volume:__docker_volumes" && ret=0 "($help -)1:volume:__docker_volumes" && ret=0
;; ;;
(ls) (ls)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help)*"{-f=,--filter=}"[Provide filter values (i.e. 'dangling=true')]:filter: " \ "($help)*"{-f=,--filter=}"[Provide filter values (i.e. 'dangling=true')]:filter: " \
"($help -q --quiet)"{-q,--quiet}"[Only display volume names]" && ret=0 "($help -q --quiet)"{-q,--quiet}"[Only display volume names]" && ret=0
;; ;;
(rm) (rm)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -):volume:__docker_volumes" && ret=0 "($help -):volume:__docker_volumes" && ret=0
;; ;;
(help) (help)
_arguments ":subcommand:__docker_volume_commands" && ret=0 _arguments $(__docker_arguments) ":subcommand:__docker_volume_commands" && ret=0
;; ;;
esac esac
@ -450,14 +459,14 @@ __docker_subcommand() {
case "$words[1]" in case "$words[1]" in
(attach) (attach)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help)--no-stdin[Do not attach stdin]" \ "($help)--no-stdin[Do not attach stdin]" \
"($help)--sig-proxy[Proxy all received signals to the process (non-TTY mode only)]" \ "($help)--sig-proxy[Proxy all received signals to the process (non-TTY mode only)]" \
"($help -):containers:__docker_runningcontainers" && ret=0 "($help -):containers:__docker_runningcontainers" && ret=0
;; ;;
(build) (build)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
$opts_cpumemlimit \ $opts_cpumemlimit \
"($help)*--build-arg[Set build-time variables]:<varname>=<value>: " \ "($help)*--build-arg[Set build-time variables]:<varname>=<value>: " \
@ -471,7 +480,7 @@ __docker_subcommand() {
"($help -):path or URL:_directories" && ret=0 "($help -):path or URL:_directories" && ret=0
;; ;;
(commit) (commit)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -a --author)"{-a=,--author=}"[Author]:author: " \ "($help -a --author)"{-a=,--author=}"[Author]:author: " \
"($help)*"{-c=,--change=}"[Apply Dockerfile instruction to the created image]:Dockerfile:_files" \ "($help)*"{-c=,--change=}"[Apply Dockerfile instruction to the created image]:Dockerfile:_files" \
@ -481,7 +490,7 @@ __docker_subcommand() {
"($help -): :__docker_repositories_with_tags" && ret=0 "($help -): :__docker_repositories_with_tags" && ret=0
;; ;;
(cp) (cp)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)1:container:->container" \ "($help -)1:container:->container" \
"($help -)2:hostpath:_files" && ret=0 "($help -)2:hostpath:_files" && ret=0
@ -496,7 +505,7 @@ __docker_subcommand() {
esac esac
;; ;;
(create) (create)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
$opts_cpumemlimit \ $opts_cpumemlimit \
$opts_create \ $opts_create \
@ -516,7 +525,7 @@ __docker_subcommand() {
;; ;;
(daemon) (daemon)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help)--api-cors-header=[Set CORS headers in the remote API]:CORS headers: " \ "($help)--api-cors-header=[Set CORS headers in the remote API]:CORS headers: " \
"($help -b --bridge)"{-b=,--bridge=}"[Attach containers to a network bridge]:bridge:_net_interfaces" \ "($help -b --bridge)"{-b=,--bridge=}"[Attach containers to a network bridge]:bridge:_net_interfaces" \
@ -584,12 +593,12 @@ __docker_subcommand() {
esac esac
;; ;;
(diff) (diff)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)*:containers:__docker_containers" && ret=0 "($help -)*:containers:__docker_containers" && ret=0
;; ;;
(events) (events)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help)*"{-f=,--filter=}"[Filter values]:filter: " \ "($help)*"{-f=,--filter=}"[Filter values]:filter: " \
"($help)--since=[Events created since this timestamp]:timestamp: " \ "($help)--since=[Events created since this timestamp]:timestamp: " \
@ -597,7 +606,7 @@ __docker_subcommand() {
;; ;;
(exec) (exec)
local state local state
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -d --detach)"{-d,--detach}"[Detached mode: leave the container running in the background]" \ "($help -d --detach)"{-d,--detach}"[Detached mode: leave the container running in the background]" \
"($help -i --interactive)"{-i,--interactive}"[Keep stdin open even if not attached]" \ "($help -i --interactive)"{-i,--interactive}"[Keep stdin open even if not attached]" \
@ -616,13 +625,13 @@ __docker_subcommand() {
esac esac
;; ;;
(export) (export)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -o --output)"{-o=,--output=}"[Write to a file, instead of stdout]:output file:_files" \ "($help -o --output)"{-o=,--output=}"[Write to a file, instead of stdout]:output file:_files" \
"($help -)*:containers:__docker_containers" && ret=0 "($help -)*:containers:__docker_containers" && ret=0
;; ;;
(history) (history)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -H --human)"{-H,--human}"[Print sizes and dates in human readable format]" \ "($help -H --human)"{-H,--human}"[Print sizes and dates in human readable format]" \
"($help)--no-trunc[Do not truncate output]" \ "($help)--no-trunc[Do not truncate output]" \
@ -630,7 +639,7 @@ __docker_subcommand() {
"($help -)*: :__docker_images" && ret=0 "($help -)*: :__docker_images" && ret=0
;; ;;
(images) (images)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -a --all)"{-a,--all}"[Show all images]" \ "($help -a --all)"{-a,--all}"[Show all images]" \
"($help)--digest[Show digests]" \ "($help)--digest[Show digests]" \
@ -640,7 +649,7 @@ __docker_subcommand() {
"($help -): :__docker_repositories" && ret=0 "($help -): :__docker_repositories" && ret=0
;; ;;
(import) (import)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help)*"{-c=,--change=}"[Apply Dockerfile instruction to the created image]:Dockerfile:_files" \ "($help)*"{-c=,--change=}"[Apply Dockerfile instruction to the created image]:Dockerfile:_files" \
"($help -m --message)"{-m=,--message=}"[Set commit message for imported image]:message: " \ "($help -m --message)"{-m=,--message=}"[Set commit message for imported image]:message: " \
@ -648,12 +657,12 @@ __docker_subcommand() {
"($help -): :__docker_repositories_with_tags" && ret=0 "($help -): :__docker_repositories_with_tags" && ret=0
;; ;;
(info|version) (info|version)
_arguments \ _arguments $(__docker_arguments) \
$opts_help && ret=0 $opts_help && ret=0
;; ;;
(inspect) (inspect)
local state local state
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -f --format)"{-f=,--format=}"[Format the output using the given go template]:template: " \ "($help -f --format)"{-f=,--format=}"[Format the output using the given go template]:template: " \
"($help -s --size)"{-s,--size}"[Display total file sizes if the type is container]" \ "($help -s --size)"{-s,--size}"[Display total file sizes if the type is container]" \
@ -673,18 +682,18 @@ __docker_subcommand() {
esac esac
;; ;;
(kill) (kill)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -s --signal)"{-s=,--signal=}"[Signal to send]:signal:_signals" \ "($help -s --signal)"{-s=,--signal=}"[Signal to send]:signal:_signals" \
"($help -)*:containers:__docker_runningcontainers" && ret=0 "($help -)*:containers:__docker_runningcontainers" && ret=0
;; ;;
(load) (load)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -i --input)"{-i=,--input=}"[Read from tar archive file]:archive file:_files -g "*.((tar|TAR)(.gz|.GZ|.Z|.bz2|.lzma|.xz|)|(tbz|tgz|txz))(-.)"" && ret=0 "($help -i --input)"{-i=,--input=}"[Read from tar archive file]:archive file:_files -g "*.((tar|TAR)(.gz|.GZ|.Z|.bz2|.lzma|.xz|)|(tbz|tgz|txz))(-.)"" && ret=0
;; ;;
(login) (login)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -e --email)"{-e=,--email=}"[Email]:email: " \ "($help -e --email)"{-e=,--email=}"[Email]:email: " \
"($help -p --password)"{-p=,--password=}"[Password]:password: " \ "($help -p --password)"{-p=,--password=}"[Password]:password: " \
@ -692,12 +701,12 @@ __docker_subcommand() {
"($help -)1:server: " && ret=0 "($help -)1:server: " && ret=0
;; ;;
(logout) (logout)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)1:server: " && ret=0 "($help -)1:server: " && ret=0
;; ;;
(logs) (logs)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -f --follow)"{-f,--follow}"[Follow log output]" \ "($help -f --follow)"{-f,--follow}"[Follow log output]" \
"($help -s --since)"{-s=,--since=}"[Show logs since this timestamp]:timestamp: " \ "($help -s --since)"{-s=,--since=}"[Show logs since this timestamp]:timestamp: " \
@ -707,7 +716,7 @@ __docker_subcommand() {
;; ;;
(network) (network)
local curcontext="$curcontext" state local curcontext="$curcontext" state
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -): :->command" \ "($help -): :->command" \
"($help -)*:: :->option-or-argument" && ret=0 "($help -)*:: :->option-or-argument" && ret=0
@ -723,18 +732,18 @@ __docker_subcommand() {
esac esac
;; ;;
(pause|unpause) (pause|unpause)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)*:containers:__docker_runningcontainers" && ret=0 "($help -)*:containers:__docker_runningcontainers" && ret=0
;; ;;
(port) (port)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)1:containers:__docker_runningcontainers" \ "($help -)1:containers:__docker_runningcontainers" \
"($help -)2:port:_ports" && ret=0 "($help -)2:port:_ports" && ret=0
;; ;;
(ps) (ps)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -a --all)"{-a,--all}"[Show all containers]" \ "($help -a --all)"{-a,--all}"[Show all containers]" \
"($help)--before=[Show only container created before...]:containers:__docker_containers" \ "($help)--before=[Show only container created before...]:containers:__docker_containers" \
@ -748,30 +757,30 @@ __docker_subcommand() {
"($help)--since=[Show only containers created since...]:containers:__docker_containers" && ret=0 "($help)--since=[Show only containers created since...]:containers:__docker_containers" && ret=0
;; ;;
(pull) (pull)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -a --all-tags)"{-a,--all-tags}"[Download all tagged images]" \ "($help -a --all-tags)"{-a,--all-tags}"[Download all tagged images]" \
"($help -):name:__docker_search" && ret=0 "($help -):name:__docker_search" && ret=0
;; ;;
(push) (push)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -): :__docker_images" && ret=0 "($help -): :__docker_images" && ret=0
;; ;;
(rename) (rename)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -):old name:__docker_containers" \ "($help -):old name:__docker_containers" \
"($help -):new name: " && ret=0 "($help -):new name: " && ret=0
;; ;;
(restart|stop) (restart|stop)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -t --time)"{-t=,--time=}"[Number of seconds to try to stop for before killing the container]:seconds to before killing:(1 5 10 30 60)" \ "($help -t --time)"{-t=,--time=}"[Number of seconds to try to stop for before killing the container]:seconds to before killing:(1 5 10 30 60)" \
"($help -)*:containers:__docker_runningcontainers" && ret=0 "($help -)*:containers:__docker_runningcontainers" && ret=0
;; ;;
(rm) (rm)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -f --force)"{-f,--force}"[Force removal]" \ "($help -f --force)"{-f,--force}"[Force removal]" \
"($help -l --link)"{-l,--link}"[Remove the specified link and not the underlying container]" \ "($help -l --link)"{-l,--link}"[Remove the specified link and not the underlying container]" \
@ -779,14 +788,14 @@ __docker_subcommand() {
"($help -)*:containers:__docker_stoppedcontainers" && ret=0 "($help -)*:containers:__docker_stoppedcontainers" && ret=0
;; ;;
(rmi) (rmi)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -f --force)"{-f,--force}"[Force removal]" \ "($help -f --force)"{-f,--force}"[Force removal]" \
"($help)--no-prune[Do not delete untagged parents]" \ "($help)--no-prune[Do not delete untagged parents]" \
"($help -)*: :__docker_images" && ret=0 "($help -)*: :__docker_images" && ret=0
;; ;;
(run) (run)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
$opts_cpumemlimit \ $opts_cpumemlimit \
$opts_create \ $opts_create \
@ -810,13 +819,13 @@ __docker_subcommand() {
;; ;;
(save) (save)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -o --output)"{-o=,--output=}"[Write to file]:file:_files" \ "($help -o --output)"{-o=,--output=}"[Write to file]:file:_files" \
"($help -)*: :__docker_images" && ret=0 "($help -)*: :__docker_images" && ret=0
;; ;;
(search) (search)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help)--automated[Only show automated builds]" \ "($help)--automated[Only show automated builds]" \
"($help)--no-trunc[Do not truncate output]" \ "($help)--no-trunc[Do not truncate output]" \
@ -824,27 +833,27 @@ __docker_subcommand() {
"($help -):term: " && ret=0 "($help -):term: " && ret=0
;; ;;
(start) (start)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -a --attach)"{-a,--attach}"[Attach container's stdout/stderr and forward all signals]" \ "($help -a --attach)"{-a,--attach}"[Attach container's stdout/stderr and forward all signals]" \
"($help -i --interactive)"{-i,--interactive}"[Attach container's stding]" \ "($help -i --interactive)"{-i,--interactive}"[Attach container's stding]" \
"($help -)*:containers:__docker_stoppedcontainers" && ret=0 "($help -)*:containers:__docker_stoppedcontainers" && ret=0
;; ;;
(stats) (stats)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help)--no-stream[Disable streaming stats and only pull the first result]" \ "($help)--no-stream[Disable streaming stats and only pull the first result]" \
"($help -)*:containers:__docker_runningcontainers" && ret=0 "($help -)*:containers:__docker_runningcontainers" && ret=0
;; ;;
(tag) (tag)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -f --force)"{-f,--force}"[force]"\ "($help -f --force)"{-f,--force}"[force]"\
"($help -):source:__docker_images"\ "($help -):source:__docker_images"\
"($help -):destination:__docker_repositories_with_tags" && ret=0 "($help -):destination:__docker_repositories_with_tags" && ret=0
;; ;;
(top) (top)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)1:containers:__docker_runningcontainers" \ "($help -)1:containers:__docker_runningcontainers" \
"($help -)*:: :->ps-arguments" && ret=0 "($help -)*:: :->ps-arguments" && ret=0
@ -857,7 +866,7 @@ __docker_subcommand() {
;; ;;
(volume) (volume)
local curcontext="$curcontext" state local curcontext="$curcontext" state
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -): :->command" \ "($help -): :->command" \
"($help -)*:: :->option-or-argument" && ret=0 "($help -)*:: :->option-or-argument" && ret=0
@ -873,12 +882,12 @@ __docker_subcommand() {
esac esac
;; ;;
(wait) (wait)
_arguments \ _arguments $(__docker_arguments) \
$opts_help \ $opts_help \
"($help -)*:containers:__docker_runningcontainers" && ret=0 "($help -)*:containers:__docker_runningcontainers" && ret=0
;; ;;
(help) (help)
_arguments ":subcommand:__docker_commands" && ret=0 _arguments $(__docker_arguments) ":subcommand:__docker_commands" && ret=0
;; ;;
esac esac
@ -897,7 +906,7 @@ _docker() {
integer ret=1 integer ret=1
typeset -A opt_args typeset -A opt_args
_arguments -C \ _arguments $(__docker_arguments) -C \
"(: -)"{-h,--help}"[Print usage]" \ "(: -)"{-h,--help}"[Print usage]" \
"($help)--config[Location of client config files]:path:_directories" \ "($help)--config[Location of client config files]:path:_directories" \
"($help -D --debug)"{-D,--debug}"[Enable debug mode]" \ "($help -D --debug)"{-D,--debug}"[Enable debug mode]" \