github.com/rajnmithun/docker@v1.6.0-rc2/contrib/completion/bash/docker (about)

     1  #!/bin/bash
     2  #
     3  # bash completion file for core docker commands
     4  #
     5  # This script provides completion of:
     6  #  - commands and their options
     7  #  - container ids and names
     8  #  - image repos and tags
     9  #  - filepaths
    10  #
    11  # To enable the completions either:
    12  #  - place this file in /etc/bash_completion.d
    13  #  or
    14  #  - copy this file to e.g. ~/.docker-completion.sh and add the line
    15  #    below to your .bashrc after bash completion features are loaded
    16  #    . ~/.docker-completion.sh
    17  #
    18  # Note:
    19  # Currently, the completions will not work if the docker daemon is not
    20  # bound to the default communication port/socket
    21  # If the docker daemon is using a unix socket for communication your user
    22  # must have access to the socket for the completions to function correctly
    23  #
    24  # Note for developers:
    25  # Please arrange options sorted alphabetically by long name with the short 
    26  # options immediately following their corresponding long form.
    27  # This order should be applied to lists, alternatives and code blocks.
    28  
    29  __docker_q() {
    30  	docker 2>/dev/null "$@"
    31  }
    32  
    33  __docker_containers_all() {
    34  	local IFS=$'\n'
    35  	local containers=( $(__docker_q ps -aq --no-trunc) )
    36  	if [ "$1" ]; then
    37  		containers=( $(__docker_q inspect --format "{{if $1}}{{.Id}}{{end}}" "${containers[@]}") )
    38  	fi
    39  	local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
    40  	names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
    41  	unset IFS
    42  	COMPREPLY=( $(compgen -W "${names[*]} ${containers[*]}" -- "$cur") )
    43  }
    44  
    45  __docker_containers_running() {
    46  	__docker_containers_all '.State.Running'
    47  }
    48  
    49  __docker_containers_stopped() {
    50  	__docker_containers_all 'not .State.Running'
    51  }
    52  
    53  __docker_containers_pauseable() {
    54  	__docker_containers_all 'and .State.Running (not .State.Paused)'
    55  }
    56  
    57  __docker_containers_unpauseable() {
    58  	__docker_containers_all '.State.Paused'
    59  }
    60  
    61  __docker_image_repos() {
    62  	local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
    63  	COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
    64  }
    65  
    66  __docker_image_repos_and_tags() {
    67  	local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')"
    68  	COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") )
    69  	__ltrim_colon_completions "$cur"
    70  }
    71  
    72  __docker_image_repos_and_tags_and_ids() {
    73  	local images="$(__docker_q images -a --no-trunc | awk 'NR>1 { print $3; if ($1 != "<none>") { print $1; print $1":"$2 } }')"
    74  	COMPREPLY=( $(compgen -W "$images" -- "$cur") )
    75  	__ltrim_colon_completions "$cur"
    76  }
    77  
    78  __docker_containers_and_images() {
    79  	__docker_containers_all
    80  	local containers=( "${COMPREPLY[@]}" )
    81  	__docker_image_repos_and_tags_and_ids
    82  	COMPREPLY+=( "${containers[@]}" )
    83  }
    84  
    85  __docker_pos_first_nonflag() {
    86  	local argument_flags=$1
    87  
    88  	local counter=$cpos
    89  	while [ $counter -le $cword ]; do
    90  		if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
    91  			(( counter++ ))
    92  		else
    93  			case "${words[$counter]}" in
    94  				-*)
    95  					;;
    96  				*)
    97  					break
    98  					;;
    99  			esac
   100  		fi
   101  		(( counter++ ))
   102  	done
   103  
   104  	echo $counter
   105  }
   106  
   107  # Transforms a multiline list of strings into a single line string
   108  # with the words separated by "|".
   109  # This is used to prepare arguments to __docker_pos_first_nonflag().
   110  __docker_to_alternatives() {
   111  	local parts=( $1 )
   112  	local IFS='|'
   113  	echo "${parts[*]}"
   114  }
   115  
   116  # Transforms a multiline list of options into an extglob pattern
   117  # suitable for use in case statements.
   118  __docker_to_extglob() {
   119  	local extglob=$( __docker_to_alternatives "$1" )
   120  	echo "@($extglob)"
   121  }
   122  
   123  __docker_resolve_hostname() {
   124  	command -v host >/dev/null 2>&1 || return
   125  	COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') )
   126  }
   127  
   128  __docker_capabilities() {
   129  	# The list of capabilities is defined in types.go, ALL was added manually.
   130  	COMPREPLY=( $( compgen -W "
   131  		ALL
   132  		AUDIT_CONTROL
   133  		AUDIT_WRITE
   134  		AUDIT_READ
   135  		BLOCK_SUSPEND
   136  		CHOWN
   137  		DAC_OVERRIDE
   138  		DAC_READ_SEARCH
   139  		FOWNER
   140  		FSETID
   141  		IPC_LOCK
   142  		IPC_OWNER
   143  		KILL
   144  		LEASE
   145  		LINUX_IMMUTABLE
   146  		MAC_ADMIN
   147  		MAC_OVERRIDE
   148  		MKNOD
   149  		NET_ADMIN
   150  		NET_BIND_SERVICE
   151  		NET_BROADCAST
   152  		NET_RAW
   153  		SETFCAP
   154  		SETGID
   155  		SETPCAP
   156  		SETUID
   157  		SYS_ADMIN
   158  		SYS_BOOT
   159  		SYS_CHROOT
   160  		SYSLOG
   161  		SYS_MODULE
   162  		SYS_NICE
   163  		SYS_PACCT
   164  		SYS_PTRACE
   165  		SYS_RAWIO
   166  		SYS_RESOURCE
   167  		SYS_TIME
   168  		SYS_TTY_CONFIG
   169  		WAKE_ALARM
   170  	" -- "$cur" ) )
   171  }
   172  
   173  # a selection of the available signals that is most likely of interest in the
   174  # context of docker containers.
   175  __docker_signals() {
   176  	local signals=(
   177  		SIGCONT
   178  		SIGHUP
   179  		SIGINT
   180  		SIGKILL
   181  		SIGQUIT
   182  		SIGSTOP
   183  		SIGTERM
   184  		SIGUSR1
   185  		SIGUSR2
   186  	)
   187  	COMPREPLY=( $( compgen -W "${signals[*]} ${signals[*]#SIG}" -- "$( echo $cur | tr '[:lower:]' '[:upper:]')" ) )
   188  }
   189  
   190  _docker_docker() {
   191  	local boolean_options="
   192  		--daemon -d
   193  		--debug -D
   194  		--help -h
   195  		--icc
   196  		--ip-forward
   197  		--ip-masq
   198  		--iptables
   199  		--ipv6
   200  		--selinux-enabled
   201  		--tls
   202  		--tlsverify
   203  		--version -v
   204  	"
   205  
   206  	case "$prev" in
   207  		--graph|-g)
   208  			_filedir -d
   209  			return
   210  			;;
   211  		--log-level|-l)
   212  			COMPREPLY=( $( compgen -W "debug info warn error fatal" -- "$cur" ) )
   213  			return
   214  			;;
   215  		--pidfile|-p|--tlscacert|--tlscert|--tlskey)
   216  			_filedir
   217  			return
   218  			;;
   219  		--storage-driver|-s)
   220  			COMPREPLY=( $( compgen -W "aufs devicemapper btrfs overlay" -- "$(echo $cur | tr '[:upper:]' '[:lower:]')" ) )
   221  			return
   222  			;;
   223  		$main_options_with_args_glob )
   224  			return
   225  			;;
   226  	esac
   227  
   228  	case "$cur" in
   229  		-*)
   230  			COMPREPLY=( $( compgen -W "$boolean_options $main_options_with_args" -- "$cur" ) )
   231  			;;
   232  		*)
   233  			COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
   234  			;;
   235  	esac
   236  }
   237  
   238  _docker_attach() {
   239  	case "$cur" in
   240  		-*)
   241  			COMPREPLY=( $( compgen -W "--help --no-stdin --sig-proxy" -- "$cur" ) )
   242  			;;
   243  		*)
   244  			local counter="$(__docker_pos_first_nonflag)"
   245  			if [ $cword -eq $counter ]; then
   246  				__docker_containers_running
   247  			fi
   248  			;;
   249  	esac
   250  }
   251  
   252  _docker_build() {
   253  	case "$prev" in
   254  		--tag|-t)
   255  			__docker_image_repos_and_tags
   256  			return
   257  			;;
   258  		--file|-f)
   259  			_filedir
   260  			return	
   261  			;;	
   262  	esac
   263  
   264  	case "$cur" in
   265  		-*)
   266  			COMPREPLY=( $( compgen -W "--file -f --force-rm --help --no-cache --pull --quiet -q --rm --tag -t" -- "$cur" ) )
   267  			;;
   268  		*)
   269  			local counter="$(__docker_pos_first_nonflag '--tag|-t')"
   270  			if [ $cword -eq $counter ]; then
   271  				_filedir -d
   272  			fi
   273  			;;
   274  	esac
   275  }
   276  
   277  _docker_commit() {
   278  	case "$prev" in
   279  		--author|-a|--change|-c|--message|-m)
   280  			return
   281  			;;
   282  	esac
   283  
   284  	case "$cur" in
   285  		-*)
   286  			COMPREPLY=( $( compgen -W "--author -a --change -c --help --message -m --pause -p" -- "$cur" ) )
   287  			;;
   288  		*)
   289  			local counter=$(__docker_pos_first_nonflag '--author|-a|--change|-c|--message|-m')
   290  
   291  			if [ $cword -eq $counter ]; then
   292  				__docker_containers_all
   293  				return
   294  			fi
   295  			(( counter++ ))
   296  
   297  			if [ $cword -eq $counter ]; then
   298  				__docker_image_repos_and_tags
   299  				return
   300  			fi
   301  			;;
   302  	esac
   303  }
   304  
   305  _docker_cp() {
   306  	case "$cur" in
   307  		-*)
   308  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   309  			;;
   310  		*)
   311  			local counter=$(__docker_pos_first_nonflag)
   312  			if [ $cword -eq $counter ]; then
   313  				case "$cur" in
   314  					*:)
   315  						return
   316  						;;
   317  					*)
   318  						__docker_containers_all
   319  						COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
   320  						compopt -o nospace
   321  						return
   322  						;;
   323  				esac
   324  			fi
   325  			(( counter++ ))
   326  
   327  			if [ $cword -eq $counter ]; then
   328  				_filedir -d
   329  				return
   330  			fi
   331  			;;
   332  	esac
   333  }
   334  
   335  _docker_create() {
   336  	_docker_run
   337  }
   338  
   339  _docker_diff() {
   340  	case "$cur" in
   341  		-*)
   342  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   343  			;;
   344  		*)
   345  			local counter=$(__docker_pos_first_nonflag)
   346  			if [ $cword -eq $counter ]; then
   347  				__docker_containers_all
   348  			fi
   349  			;;
   350  	esac
   351  }
   352  
   353  _docker_events() {
   354  	case "$prev" in
   355  		--filter|-f)
   356  			COMPREPLY=( $( compgen -S = -W "container event image" -- "$cur" ) )
   357  			compopt -o nospace
   358  			return
   359  			;;
   360  		--since|--until)
   361  			return
   362  			;;
   363  	esac
   364  
   365  	# "=" gets parsed to a word and assigned to either $cur or $prev depending on whether
   366  	# it is the last character or not. So we search for "xxx=" in the the last two words.
   367  	case "${words[$cword-2]}$prev=" in
   368  		*container=*)
   369  			cur="${cur#=}"
   370  			__docker_containers_all
   371  			return
   372  			;;
   373  		*event=*)
   374  			COMPREPLY=( $( compgen -W "create destroy die export kill pause restart start stop unpause" -- "${cur#=}" ) )
   375  			return
   376  			;;
   377  		*image=*)
   378  			cur="${cur#=}"
   379  			__docker_image_repos_and_tags_and_ids
   380  			return
   381  			;;
   382  	esac
   383  
   384  	case "$cur" in
   385  		-*)
   386  			COMPREPLY=( $( compgen -W "--filter -f --help --since --until" -- "$cur" ) )
   387  			;;
   388  	esac
   389  }
   390  
   391  _docker_exec() {
   392  	case "$cur" in
   393  		-*)
   394  			COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i -t --tty" -- "$cur" ) )
   395  			;;
   396  		*)
   397  			__docker_containers_running
   398  			;;
   399  	esac
   400  }
   401  
   402  _docker_export() {
   403  	case "$cur" in
   404  		-*)
   405  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   406  			;;
   407  		*)
   408  			local counter=$(__docker_pos_first_nonflag)
   409  			if [ $cword -eq $counter ]; then
   410  				__docker_containers_all
   411  			fi
   412  			;;
   413  	esac
   414  }
   415  
   416  _docker_help() {
   417  	local counter=$(__docker_pos_first_nonflag)
   418  	if [ $cword -eq $counter ]; then
   419  		COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
   420  	fi
   421  }
   422  
   423  _docker_history() {
   424  	case "$cur" in
   425  		-*)
   426  			COMPREPLY=( $( compgen -W "--help --no-trunc --quiet -q" -- "$cur" ) )
   427  			;;
   428  		*)
   429  			local counter=$(__docker_pos_first_nonflag)
   430  			if [ $cword -eq $counter ]; then
   431  				__docker_image_repos_and_tags_and_ids
   432  			fi
   433  			;;
   434  	esac
   435  }
   436  
   437  _docker_images() {
   438  	case "$prev" in
   439  		--filter|-f)
   440  			COMPREPLY=( $( compgen -W "dangling=true" -- "$cur" ) )
   441  			return
   442  			;;
   443  	esac
   444  
   445  	case "${words[$cword-2]}$prev=" in
   446  		*dangling=*)
   447  			COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
   448  			return
   449  			;;
   450  	esac
   451  
   452  	case "$cur" in
   453  		-*)
   454  			COMPREPLY=( $( compgen -W "--all -a --filter -f --help --no-trunc --quiet -q" -- "$cur" ) )
   455  			;;
   456  		*)
   457  			local counter=$(__docker_pos_first_nonflag)
   458  			if [ $cword -eq $counter ]; then
   459  				__docker_image_repos
   460  			fi
   461  			;;
   462  	esac
   463  }
   464  
   465  _docker_import() {
   466  	case "$cur" in
   467  		-*)
   468  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   469  			;;
   470  		*)
   471  			local counter=$(__docker_pos_first_nonflag)
   472  			if [ $cword -eq $counter ]; then
   473  				return
   474  			fi
   475  			(( counter++ ))
   476  
   477  			if [ $cword -eq $counter ]; then
   478  				__docker_image_repos_and_tags
   479  				return
   480  			fi
   481  			;;
   482  	esac
   483  }
   484  
   485  _docker_info() {
   486  	case "$cur" in
   487  		-*)
   488  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   489  			;;
   490  	esac
   491  }
   492  
   493  _docker_inspect() {
   494  	case "$prev" in
   495  		--format|-f)
   496  			return
   497  			;;
   498  	esac
   499  
   500  	case "$cur" in
   501  		-*)
   502  			COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
   503  			;;
   504  		*)
   505  			__docker_containers_and_images
   506  			;;
   507  	esac
   508  }
   509  
   510  _docker_kill() {
   511  	case "$prev" in
   512  		--signal|-s)
   513  			__docker_signals
   514  			return
   515  			;;
   516  	esac
   517  
   518  	case "$cur" in
   519  		-*)
   520  			COMPREPLY=( $( compgen -W "--help --signal -s" -- "$cur" ) )
   521  			;;
   522  		*)
   523  			__docker_containers_running
   524  			;;
   525  	esac
   526  }
   527  
   528  _docker_load() {
   529  	case "$prev" in
   530  		--input|-i)
   531  			_filedir
   532  			return
   533  			;;
   534  	esac
   535  
   536  	case "$cur" in
   537  		-*)
   538  			COMPREPLY=( $( compgen -W "--help --input -i" -- "$cur" ) )
   539  			;;
   540  	esac
   541  }
   542  
   543  _docker_login() {
   544  	case "$prev" in
   545  		--email|-e|--password|-p|--username|-u)
   546  			return
   547  			;;
   548  	esac
   549  
   550  	case "$cur" in
   551  		-*)
   552  			COMPREPLY=( $( compgen -W "--email -e --help --password -p --username -u" -- "$cur" ) )
   553  			;;
   554  	esac
   555  }
   556  
   557  _docker_logout() {
   558  	case "$cur" in
   559  		-*)
   560  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   561  			;;
   562  	esac
   563  }
   564  
   565  _docker_logs() {
   566  	case "$prev" in
   567  		--tail)
   568  			return
   569  			;;
   570  	esac
   571  
   572  	case "$cur" in
   573  		-*)
   574  			COMPREPLY=( $( compgen -W "--follow -f --help --tail --timestamps -t" -- "$cur" ) )
   575  			;;
   576  		*)
   577  			local counter=$(__docker_pos_first_nonflag '--tail')
   578  			if [ $cword -eq $counter ]; then
   579  				__docker_containers_all
   580  			fi
   581  			;;
   582  	esac
   583  }
   584  
   585  _docker_pause() {
   586  	case "$cur" in
   587  		-*)
   588  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   589  			;;
   590  		*)
   591  			local counter=$(__docker_pos_first_nonflag)
   592  			if [ $cword -eq $counter ]; then
   593  				__docker_containers_pauseable
   594  			fi
   595  			;;
   596  	esac
   597  }
   598  
   599  _docker_port() {
   600  	case "$cur" in
   601  		-*)
   602  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   603  			;;
   604  		*)
   605  			local counter=$(__docker_pos_first_nonflag)
   606  			if [ $cword -eq $counter ]; then
   607  				__docker_containers_all
   608  			fi
   609  			;;
   610  	esac
   611  }
   612  
   613  _docker_ps() {
   614  	case "$prev" in
   615  		--before|--since)
   616  			__docker_containers_all
   617  			;;
   618  		--filter|-f)
   619  			COMPREPLY=( $( compgen -S = -W "exited status" -- "$cur" ) )
   620  			compopt -o nospace
   621  			return
   622  			;;
   623  		-n)
   624  			return
   625  			;;
   626  	esac
   627  
   628  	case "${words[$cword-2]}$prev=" in
   629  		*status=*)
   630  			COMPREPLY=( $( compgen -W "exited paused restarting running" -- "${cur#=}" ) )
   631  			return
   632  			;;
   633  	esac
   634  
   635  	case "$cur" in
   636  		-*)
   637  			COMPREPLY=( $( compgen -W "--all -a --before --filter -f --help --latest -l -n --no-trunc --quiet -q --size -s --since" -- "$cur" ) )
   638  			;;
   639  	esac
   640  }
   641  
   642  _docker_pull() {
   643  	case "$cur" in
   644  		-*)
   645  			COMPREPLY=( $( compgen -W "--all-tags -a --help" -- "$cur" ) )
   646  			;;
   647  		*)
   648  			local counter=$(__docker_pos_first_nonflag)
   649  			if [ $cword -eq $counter ]; then
   650  				__docker_image_repos_and_tags
   651  			fi
   652  			;;
   653  	esac
   654  }
   655  
   656  _docker_push() {
   657  	case "$cur" in
   658  		-*)
   659  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   660  			;;
   661  		*)
   662  			local counter=$(__docker_pos_first_nonflag)
   663  			if [ $cword -eq $counter ]; then
   664  				__docker_image_repos_and_tags
   665  			fi
   666  			;;
   667  	esac
   668  }
   669  
   670  _docker_rename() {
   671  	case "$cur" in
   672  		-*)
   673  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   674  			;;
   675  		*)
   676  			local counter=$(__docker_pos_first_nonflag)
   677  			if [ $cword -eq $counter ]; then
   678  				__docker_containers_all
   679  			fi
   680  			;;
   681  	esac
   682  }
   683  
   684  _docker_restart() {
   685  	case "$prev" in
   686  		--time|-t)
   687  			return
   688  			;;
   689  	esac
   690  
   691  	case "$cur" in
   692  		-*)
   693  			COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
   694  			;;
   695  		*)
   696  			__docker_containers_all
   697  			;;
   698  	esac
   699  }
   700  
   701  _docker_rm() {
   702  	case "$cur" in
   703  		-*)
   704  			COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) )
   705  			;;
   706  		*)
   707  			for arg in "${COMP_WORDS[@]}"; do
   708  				case "$arg" in
   709  					--force|-f)
   710  						__docker_containers_all
   711  						return
   712  						;;
   713  				esac
   714  			done
   715  			__docker_containers_stopped
   716  			;;
   717  	esac
   718  }
   719  
   720  _docker_rmi() {
   721  	case "$cur" in
   722  		-*)
   723  			COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
   724  			;;
   725  		*)
   726  			__docker_image_repos_and_tags_and_ids
   727  			;;
   728  	esac
   729  }
   730  
   731  _docker_run() {
   732  	local options_with_args="
   733  		--add-host
   734  		--attach -a
   735  		--cap-add
   736  		--cap-drop
   737  		--cidfile
   738  		--cpuset
   739  		--cpu-shares -c
   740  		--device
   741  		--dns
   742  		--dns-search
   743  		--entrypoint
   744  		--env -e
   745  		--env-file
   746  		--expose
   747  		--hostname -h
   748  		--ipc
   749  		--link
   750  		--lxc-conf
   751  		--mac-address
   752  		--memory -m
   753  		--memory-swap
   754  		--name
   755  		--net
   756  		--pid
   757  		--publish -p
   758  		--restart
   759  		--security-opt
   760  		--user -u
   761  		--ulimit
   762  		--volumes-from
   763  		--volume -v
   764  		--workdir -w
   765  	"
   766  
   767  	local all_options="$options_with_args
   768  		--help
   769  		--interactive -i
   770  		--privileged
   771  		--publish-all -P
   772  		--read-only
   773  		--tty -t
   774  	"
   775  
   776  	[ "$command" = "run" ] && all_options="$all_options
   777  		--detach -d
   778  		--rm
   779  		--sig-proxy
   780  	"
   781  
   782  	local options_with_args_glob=$(__docker_to_extglob "$options_with_args")
   783  
   784  	case "$prev" in
   785  		--add-host)
   786  			case "$cur" in
   787  				*:)
   788  					__docker_resolve_hostname
   789  					return
   790  					;;
   791  			esac
   792  			;;
   793  		--attach|-a)
   794  			COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
   795  			return
   796  			;;
   797  		--cap-add|--cap-drop)
   798  			__docker_capabilities
   799  			return
   800  			;;
   801  		--cidfile|--env-file)
   802  			_filedir
   803  			return
   804  			;;
   805  		--device|--volume|-v)
   806  			case "$cur" in
   807  				*:*)
   808  					# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
   809  					;;
   810  				'')
   811  					COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
   812  					compopt -o nospace
   813  					;;
   814  				/*)
   815  					_filedir
   816  					compopt -o nospace
   817  					;;
   818  			esac
   819  			return
   820  			;;
   821  		--env|-e)
   822  			COMPREPLY=( $( compgen -e -- "$cur" ) )
   823  			compopt -o nospace
   824  			return
   825  			;;
   826  		--ipc)
   827  			case "$cur" in
   828  				*:*)
   829  					cur="${cur#*:}"
   830  					__docker_containers_running
   831  					;;
   832  				*)
   833  					COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
   834  					if [ "$COMPREPLY" = "container:" ]; then
   835  						compopt -o nospace
   836  					fi
   837  					;;
   838  			esac
   839  			return
   840  			;;
   841  		--link)
   842  			case "$cur" in
   843  				*:*)
   844  					;;
   845  				*)
   846  					__docker_containers_running
   847  					COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
   848  					compopt -o nospace
   849  					;;
   850  			esac
   851  			return
   852  			;;
   853  		--net)
   854  			case "$cur" in
   855  				container:*)
   856  					local cur=${cur#*:}
   857  					__docker_containers_all
   858  					;;
   859  				*)
   860  					COMPREPLY=( $( compgen -W "bridge none container: host" -- "$cur") )
   861  					if [ "${COMPREPLY[*]}" = "container:" ] ; then
   862  						compopt -o nospace
   863  					fi
   864  					;;
   865  			esac
   866  			return
   867  			;;
   868  		--restart)
   869  			case "$cur" in
   870  				on-failure:*)
   871  					;;
   872  				*)
   873  					COMPREPLY=( $( compgen -W "no on-failure on-failure: always" -- "$cur") )
   874  					;;
   875  			esac
   876  			return
   877  			;;
   878  		--security-opt)
   879  			case "$cur" in
   880  				label:*:*)
   881  					;;
   882  				label:*)
   883  					local cur=${cur##*:}
   884  					COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
   885  					if [ "${COMPREPLY[*]}" != "disable" ] ; then
   886  						compopt -o nospace
   887  					fi
   888  					;;
   889  				*)
   890  					COMPREPLY=( $( compgen -W "label apparmor" -S ":" -- "$cur") )
   891  					compopt -o nospace
   892  					;;
   893  			esac
   894  			return
   895  			;;
   896  		--volumes-from)
   897  			__docker_containers_all
   898  			return
   899  			;;
   900  		$options_with_args_glob )
   901  			return
   902  			;;
   903  	esac
   904  
   905  	case "$cur" in
   906  		-*)
   907  			COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
   908  			;;
   909  		*)
   910  			local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
   911  
   912  			if [ $cword -eq $counter ]; then
   913  				__docker_image_repos_and_tags_and_ids
   914  			fi
   915  			;;
   916  	esac
   917  }
   918  
   919  _docker_save() {
   920  	case "$prev" in
   921  		--output|-o)
   922  			_filedir
   923  			return
   924  			;;
   925  	esac
   926  
   927  	case "$cur" in
   928  		-*)
   929  			COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
   930  			;;
   931  		*)
   932  			__docker_image_repos_and_tags_and_ids
   933  			;;
   934  	esac
   935  }
   936  
   937  _docker_search() {
   938  	case "$prev" in
   939  		--stars|-s)
   940  			return
   941  			;;
   942  	esac
   943  
   944  	case "$cur" in
   945  		-*)
   946  			COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
   947  			;;
   948  	esac
   949  }
   950  
   951  _docker_start() {
   952  	case "$cur" in
   953  		-*)
   954  			COMPREPLY=( $( compgen -W "--attach -a --help --interactive -i" -- "$cur" ) )
   955  			;;
   956  		*)
   957  			__docker_containers_stopped
   958  			;;
   959  	esac
   960  }
   961  
   962  _docker_stats() {
   963  	case "$cur" in
   964  		-*)
   965  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   966  			;;
   967  		*)
   968  			__docker_containers_running
   969  			;;
   970  	esac
   971  }
   972  
   973  _docker_stop() {
   974  	case "$prev" in
   975  		--time|-t)
   976  			return
   977  			;;
   978  	esac
   979  
   980  	case "$cur" in
   981  		-*)
   982  			COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
   983  			;;
   984  		*)
   985  			__docker_containers_running
   986  			;;
   987  	esac
   988  }
   989  
   990  _docker_tag() {
   991  	case "$cur" in
   992  		-*)
   993  			COMPREPLY=( $( compgen -W "--force -f --help" -- "$cur" ) )
   994  			;;
   995  		*)
   996  			local counter=$(__docker_pos_first_nonflag)
   997  
   998  			if [ $cword -eq $counter ]; then
   999  				__docker_image_repos_and_tags
  1000  				return
  1001  			fi
  1002  			(( counter++ ))
  1003  
  1004  			if [ $cword -eq $counter ]; then
  1005  				__docker_image_repos_and_tags
  1006  				return
  1007  			fi
  1008  			;;
  1009  	esac
  1010  }
  1011  
  1012  _docker_unpause() {
  1013  	case "$cur" in
  1014  		-*)
  1015  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1016  			;;
  1017  		*)
  1018  			local counter=$(__docker_pos_first_nonflag)
  1019  			if [ $cword -eq $counter ]; then
  1020  				__docker_containers_unpauseable
  1021  			fi
  1022  			;;
  1023  	esac
  1024  }
  1025  
  1026  _docker_top() {
  1027  	case "$cur" in
  1028  		-*)
  1029  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1030  			;;
  1031  		*)
  1032  			local counter=$(__docker_pos_first_nonflag)
  1033  			if [ $cword -eq $counter ]; then
  1034  				__docker_containers_running
  1035  			fi
  1036  			;;
  1037  	esac
  1038  }
  1039  
  1040  _docker_version() {
  1041  	case "$cur" in
  1042  		-*)
  1043  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1044  			;;
  1045  	esac
  1046  }
  1047  
  1048  _docker_wait() {
  1049  	case "$cur" in
  1050  		-*)
  1051  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1052  			;;
  1053  		*)
  1054  			__docker_containers_all
  1055  			;;
  1056  	esac
  1057  }
  1058  
  1059  _docker() {
  1060  	local previous_extglob_setting=$(shopt -p extglob)
  1061  	shopt -s extglob
  1062  
  1063  	local commands=(
  1064  		attach
  1065  		build
  1066  		commit
  1067  		cp
  1068  		create
  1069  		diff
  1070  		events
  1071  		exec
  1072  		export
  1073  		history
  1074  		images
  1075  		import
  1076  		info
  1077  		inspect
  1078  		kill
  1079  		load
  1080  		login
  1081  		logout
  1082  		logs
  1083  		pause
  1084  		port
  1085  		ps
  1086  		pull
  1087  		push
  1088  		rename
  1089  		restart
  1090  		rm
  1091  		rmi
  1092  		run
  1093  		save
  1094  		search
  1095  		start
  1096  		stats
  1097  		stop
  1098  		tag
  1099  		top
  1100  		unpause
  1101  		version
  1102  		wait
  1103  	)
  1104  
  1105  	local main_options_with_args="
  1106  		--api-cors-header
  1107  		--bip
  1108  		--bridge -b
  1109  		--default-ulimit
  1110  		--dns
  1111  		--dns-search
  1112  		--exec-driver -e
  1113  		--fixed-cidr
  1114  		--fixed-cidr-v6
  1115  		--graph -g
  1116  		--group -G
  1117  		--host -H
  1118  		--insecure-registry
  1119  		--ip
  1120  		--label
  1121  		--log-level -l
  1122  		--mtu
  1123  		--pidfile -p
  1124  		--registry-mirror
  1125  		--storage-driver -s
  1126  		--storage-opt
  1127  		--tlscacert
  1128  		--tlscert
  1129  		--tlskey
  1130  	"
  1131  
  1132  	local main_options_with_args_glob=$(__docker_to_extglob "$main_options_with_args")
  1133  
  1134  	COMPREPLY=()
  1135  	local cur prev words cword
  1136  	_get_comp_words_by_ref -n : cur prev words cword
  1137  
  1138  	local command='docker' cpos=0
  1139  	local counter=1
  1140  	while [ $counter -lt $cword ]; do
  1141  		case "${words[$counter]}" in
  1142  			$main_options_with_args_glob )
  1143  				(( counter++ ))
  1144  				;;
  1145  			-*)
  1146  				;;
  1147  			*)
  1148  				command="${words[$counter]}"
  1149  				cpos=$counter
  1150  				(( cpos++ ))
  1151  				break
  1152  				;;
  1153  		esac
  1154  		(( counter++ ))
  1155  	done
  1156  
  1157  	local completions_func=_docker_${command}
  1158  	declare -F $completions_func >/dev/null && $completions_func
  1159  
  1160  	eval "$previous_extglob_setting"
  1161  	return 0
  1162  }
  1163  
  1164  complete -F _docker docker