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