github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/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  # Configuration:
    19  #
    20  # For several commands, the amount of completions can be configured by
    21  # setting environment variables.
    22  #
    23  # DOCKER_COMPLETION_SHOW_NETWORK_IDS
    24  #   "no"  - Show names only (default)
    25  #   "yes" - Show names and ids
    26  #
    27  # You can tailor completion for the "events", "history", "inspect", "run",
    28  # "rmi" and "save" commands by settings the following environment
    29  # variables:
    30  #
    31  # DOCKER_COMPLETION_SHOW_IMAGE_IDS
    32  #   "none" - Show names only (default)
    33  #   "non-intermediate" - Show names and ids, but omit intermediate image IDs
    34  #   "all" - Show names and ids, including intermediate image IDs
    35  #
    36  # DOCKER_COMPLETION_SHOW_TAGS
    37  #   "yes" - include tags in completion options (default)
    38  #   "no"  - don't include tags in completion options
    39  
    40  #
    41  # Note:
    42  # Currently, the completions will not work if the docker daemon is not
    43  # bound to the default communication port/socket
    44  # If the docker daemon is using a unix socket for communication your user
    45  # must have access to the socket for the completions to function correctly
    46  #
    47  # Note for developers:
    48  # Please arrange options sorted alphabetically by long name with the short
    49  # options immediately following their corresponding long form.
    50  # This order should be applied to lists, alternatives and code blocks.
    51  
    52  __docker_previous_extglob_setting=$(shopt -p extglob)
    53  shopt -s extglob
    54  
    55  __docker_q() {
    56  	docker ${host:+-H "$host"} ${config:+--config "$config"} 2>/dev/null "$@"
    57  }
    58  
    59  __docker_complete_containers_all() {
    60  	local IFS=$'\n'
    61  	local containers=( $(__docker_q ps -aq --no-trunc) )
    62  	if [ "$1" ]; then
    63  		containers=( $(__docker_q inspect --format "{{if $1}}{{.Id}}{{end}}" "${containers[@]}") )
    64  	fi
    65  	local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
    66  	names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
    67  	unset IFS
    68  	COMPREPLY=( $(compgen -W "${names[*]} ${containers[*]}" -- "$cur") )
    69  }
    70  
    71  __docker_complete_containers_running() {
    72  	__docker_complete_containers_all '.State.Running'
    73  }
    74  
    75  __docker_complete_containers_stopped() {
    76  	__docker_complete_containers_all 'not .State.Running'
    77  }
    78  
    79  __docker_complete_containers_pauseable() {
    80  	__docker_complete_containers_all 'and .State.Running (not .State.Paused)'
    81  }
    82  
    83  __docker_complete_containers_unpauseable() {
    84  	__docker_complete_containers_all '.State.Paused'
    85  }
    86  
    87  __docker_complete_container_names() {
    88  	local containers=( $(__docker_q ps -aq --no-trunc) )
    89  	local names=( $(__docker_q inspect --format '{{.Name}}' "${containers[@]}") )
    90  	names=( "${names[@]#/}" ) # trim off the leading "/" from the container names
    91  	COMPREPLY=( $(compgen -W "${names[*]}" -- "$cur") )
    92  }
    93  
    94  __docker_complete_container_ids() {
    95  	local containers=( $(__docker_q ps -aq) )
    96  	COMPREPLY=( $(compgen -W "${containers[*]}" -- "$cur") )
    97  }
    98  
    99  __docker_complete_images() {
   100  	local images_args=""
   101  
   102  	case "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" in
   103  		all)
   104  			images_args="--no-trunc -a"
   105  			;;
   106  		non-intermediate)
   107  			images_args="--no-trunc"
   108  			;;
   109  	esac
   110  
   111  	local repo_print_command
   112  	if [ "${DOCKER_COMPLETION_SHOW_TAGS:-yes}" = "yes" ]; then
   113  		repo_print_command='print $1; print $1":"$2'
   114  	else
   115  		repo_print_command='print $1'
   116  	fi
   117  
   118  	local awk_script
   119  	case "$DOCKER_COMPLETION_SHOW_IMAGE_IDS" in
   120  		all|non-intermediate)
   121  			awk_script='NR>1 { print $3; if ($1 != "<none>") { '"$repo_print_command"' } }'
   122  			;;
   123  		none|*)
   124  			awk_script='NR>1 && $1 != "<none>" { '"$repo_print_command"' }'
   125  			;;
   126  	esac
   127  
   128  	local images=$(__docker_q images $images_args | awk "$awk_script")
   129  	COMPREPLY=( $(compgen -W "$images" -- "$cur") )
   130  	__ltrim_colon_completions "$cur"
   131  }
   132  
   133  __docker_complete_image_repos() {
   134  	local repos="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1 }')"
   135  	COMPREPLY=( $(compgen -W "$repos" -- "$cur") )
   136  }
   137  
   138  __docker_complete_image_repos_and_tags() {
   139  	local reposAndTags="$(__docker_q images | awk 'NR>1 && $1 != "<none>" { print $1; print $1":"$2 }')"
   140  	COMPREPLY=( $(compgen -W "$reposAndTags" -- "$cur") )
   141  	__ltrim_colon_completions "$cur"
   142  }
   143  
   144  __docker_complete_containers_and_images() {
   145  	__docker_complete_containers_all
   146  	local containers=( "${COMPREPLY[@]}" )
   147  	__docker_complete_images
   148  	COMPREPLY+=( "${containers[@]}" )
   149  }
   150  
   151  # Returns the names and optionally IDs of networks.
   152  # The selection can be narrowed by an optional filter parameter, e.g. 'type=custom'
   153  __docker_networks() {
   154  	local filter="$1"
   155  	# By default, only network names are completed.
   156  	# Set DOCKER_COMPLETION_SHOW_NETWORK_IDS=yes to also complete network IDs.
   157  	local fields='$2'
   158  	[ "${DOCKER_COMPLETION_SHOW_NETWORK_IDS}" = yes ] && fields='$1,$2'
   159  	__docker_q network ls --no-trunc ${filter:+-f "$filter"} | awk "NR>1 {print $fields}"
   160  	#__docker_q network ls --no-trunc | awk "NR>1 {print $fields}"
   161  }
   162  
   163  __docker_complete_networks() {
   164  	COMPREPLY=( $(compgen -W "$(__docker_networks $@)" -- "$cur") )
   165  }
   166  
   167  __docker_complete_network_ids() {
   168  	COMPREPLY=( $(compgen -W "$(__docker_q network ls -q --no-trunc)" -- "$cur") )
   169  }
   170  
   171  __docker_complete_network_names() {
   172  	COMPREPLY=( $(compgen -W "$(__docker_q network ls | awk 'NR>1 {print $2}')" -- "$cur") )
   173  }
   174  
   175  __docker_complete_containers_in_network() {
   176  	local containers=$(__docker_q network inspect -f '{{range $i, $c := .Containers}}{{$i}} {{$c.Name}} {{end}}' "$1")
   177  	COMPREPLY=( $(compgen -W "$containers" -- "$cur") )
   178  }
   179  
   180  __docker_complete_volumes() {
   181  	COMPREPLY=( $(compgen -W "$(__docker_q volume ls -q)" -- "$cur") )
   182  }
   183  
   184  __docker_plugins() {
   185  	__docker_q info | sed -n "/^Plugins/,/^[^ ]/s/ $1: //p"
   186  }
   187  
   188  __docker_complete_plugins() {
   189  	COMPREPLY=( $(compgen -W "$(__docker_plugins $1)" -- "$cur") )
   190  }
   191  
   192  # Finds the position of the first word that is neither option nor an option's argument.
   193  # If there are options that require arguments, you should pass a glob describing those
   194  # options, e.g. "--option1|-o|--option2"
   195  # Use this function to restrict completions to exact positions after the argument list.
   196  __docker_pos_first_nonflag() {
   197  	local argument_flags=$1
   198  
   199  	local counter=$((${subcommand_pos:-${command_pos}} + 1))
   200  	while [ $counter -le $cword ]; do
   201  		if [ -n "$argument_flags" ] && eval "case '${words[$counter]}' in $argument_flags) true ;; *) false ;; esac"; then
   202  			(( counter++ ))
   203  			# eat "=" in case of --option=arg syntax
   204  			[ "${words[$counter]}" = "=" ] && (( counter++ ))
   205  		else
   206  			case "${words[$counter]}" in
   207  				-*)
   208  					;;
   209  				*)
   210  					break
   211  					;;
   212  			esac
   213  		fi
   214  
   215  		# Bash splits words at "=", retaining "=" as a word, examples:
   216  		# "--debug=false" => 3 words, "--log-opt syslog-facility=daemon" => 4 words
   217  		while [ "${words[$counter + 1]}" = "=" ] ; do
   218  			counter=$(( counter + 2))
   219  		done
   220  
   221  		(( counter++ ))
   222  	done
   223  
   224  	echo $counter
   225  }
   226  
   227  # If we are currently completing the value of a map option (key=value)
   228  # which matches the extglob given as an argument, returns key.
   229  # This function is needed for key-specific completions.
   230  # TODO use this in all "${words[$cword-2]}$prev=" occurrences
   231  __docker_map_key_of_current_option() {
   232  	local glob="$1"
   233  
   234  	local key glob_pos
   235  	if [ "$cur" = "=" ] ; then        # key= case
   236  		key="$prev"
   237  		glob_pos=$((cword - 2))
   238  	elif [[ $cur == *=* ]] ; then     # key=value case (OSX)
   239  		key=${cur%=*}
   240  		glob_pos=$((cword - 1))
   241  	elif [ "$prev" = "=" ] ; then
   242  		key=${words[$cword - 2]}  # key=value case
   243  		glob_pos=$((cword - 3))
   244  	else
   245  		return
   246  	fi
   247  
   248  	[ "${words[$glob_pos]}" = "=" ] && ((glob_pos--))  # --option=key=value syntax
   249  
   250  	[[ ${words[$glob_pos]} == @($glob) ]] && echo "$key"
   251  }
   252  
   253  # Returns the value of the first option matching option_glob.
   254  # Valid values for option_glob are option names like '--log-level' and
   255  # globs like '--log-level|-l'
   256  # Only positions between the command and the current word are considered.
   257  __docker_value_of_option() {
   258  	local option_extglob=$(__docker_to_extglob "$1")
   259  
   260  	local counter=$((command_pos + 1))
   261  	while [ $counter -lt $cword ]; do
   262  		case ${words[$counter]} in
   263  			$option_extglob )
   264  				echo ${words[$counter + 1]}
   265  				break
   266  				;;
   267  		esac
   268  		(( counter++ ))
   269  	done
   270  }
   271  
   272  # Transforms a multiline list of strings into a single line string
   273  # with the words separated by "|".
   274  # This is used to prepare arguments to __docker_pos_first_nonflag().
   275  __docker_to_alternatives() {
   276  	local parts=( $1 )
   277  	local IFS='|'
   278  	echo "${parts[*]}"
   279  }
   280  
   281  # Transforms a multiline list of options into an extglob pattern
   282  # suitable for use in case statements.
   283  __docker_to_extglob() {
   284  	local extglob=$( __docker_to_alternatives "$1" )
   285  	echo "@($extglob)"
   286  }
   287  
   288  # Subcommand processing.
   289  # Locates the first occurrence of any of the subcommands contained in the
   290  # first argument. In case of a match, calls the corresponding completion
   291  # function and returns 0.
   292  # If no match is found, 1 is returned. The calling function can then
   293  # continue processing its completion.
   294  #
   295  # TODO if the preceding command has options that accept arguments and an
   296  # argument is equal ot one of the subcommands, this is falsely detected as
   297  # a match.
   298  __docker_subcommands() {
   299  	local subcommands="$1"
   300  
   301  	local counter=$(($command_pos + 1))
   302  	while [ $counter -lt $cword ]; do
   303  		case "${words[$counter]}" in
   304  			$(__docker_to_extglob "$subcommands") )
   305  				subcommand_pos=$counter
   306  				local subcommand=${words[$counter]}
   307  				local completions_func=_docker_${command}_${subcommand}
   308  				declare -F $completions_func >/dev/null && $completions_func
   309  				return 0
   310  				;;
   311  		esac
   312  		(( counter++ ))
   313  	done
   314  	return 1
   315  }
   316  
   317  # suppress trailing whitespace
   318  __docker_nospace() {
   319  	# compopt is not available in ancient bash versions
   320  	type compopt &>/dev/null && compopt -o nospace
   321  }
   322  
   323  __docker_complete_resolved_hostname() {
   324  	command -v host >/dev/null 2>&1 || return
   325  	COMPREPLY=( $(host 2>/dev/null "${cur%:}" | awk '/has address/ {print $4}') )
   326  }
   327  
   328  __docker_complete_capabilities() {
   329  	# The list of capabilities is defined in types.go, ALL was added manually.
   330  	COMPREPLY=( $( compgen -W "
   331  		ALL
   332  		AUDIT_CONTROL
   333  		AUDIT_WRITE
   334  		AUDIT_READ
   335  		BLOCK_SUSPEND
   336  		CHOWN
   337  		DAC_OVERRIDE
   338  		DAC_READ_SEARCH
   339  		FOWNER
   340  		FSETID
   341  		IPC_LOCK
   342  		IPC_OWNER
   343  		KILL
   344  		LEASE
   345  		LINUX_IMMUTABLE
   346  		MAC_ADMIN
   347  		MAC_OVERRIDE
   348  		MKNOD
   349  		NET_ADMIN
   350  		NET_BIND_SERVICE
   351  		NET_BROADCAST
   352  		NET_RAW
   353  		SETFCAP
   354  		SETGID
   355  		SETPCAP
   356  		SETUID
   357  		SYS_ADMIN
   358  		SYS_BOOT
   359  		SYS_CHROOT
   360  		SYSLOG
   361  		SYS_MODULE
   362  		SYS_NICE
   363  		SYS_PACCT
   364  		SYS_PTRACE
   365  		SYS_RAWIO
   366  		SYS_RESOURCE
   367  		SYS_TIME
   368  		SYS_TTY_CONFIG
   369  		WAKE_ALARM
   370  	" -- "$cur" ) )
   371  }
   372  
   373  __docker_complete_detach-keys() {
   374  	case "$prev" in
   375  		--detach-keys)
   376  			case "$cur" in
   377  				*,)
   378  					COMPREPLY=( $( compgen -W "${cur}ctrl-" -- "$cur" ) )
   379  					;;
   380  				*)
   381  					COMPREPLY=( $( compgen -W "ctrl-" -- "$cur" ) )
   382  					;;
   383  			esac
   384  
   385  			__docker_nospace
   386  			return
   387  			;;
   388  	esac
   389  	return 1
   390  }
   391  
   392  __docker_complete_isolation() {
   393  	COMPREPLY=( $( compgen -W "default hyperv process" -- "$cur" ) )
   394  }
   395  
   396  __docker_complete_log_drivers() {
   397  	COMPREPLY=( $( compgen -W "
   398  		awslogs
   399  		fluentd
   400  		gelf
   401  		journald
   402  		json-file
   403  		none
   404  		splunk
   405  		syslog
   406  	" -- "$cur" ) )
   407  }
   408  
   409  __docker_complete_log_options() {
   410  	# see docs/reference/logging/index.md
   411  	local awslogs_options="awslogs-region awslogs-group awslogs-stream"
   412  	local fluentd_options="env fluentd-address labels tag"
   413  	local gelf_options="env gelf-address labels tag"
   414  	local journald_options="env labels tag"
   415  	local json_file_options="env labels max-file max-size"
   416  	local syslog_options="syslog-address syslog-tls-ca-cert syslog-tls-cert syslog-tls-key syslog-tls-skip-verify syslog-facility tag"
   417  	local splunk_options="env labels splunk-caname splunk-capath splunk-index splunk-insecureskipverify splunk-source splunk-sourcetype splunk-token splunk-url tag"
   418  
   419  	local all_options="$fluentd_options $gelf_options $journald_options $json_file_options $syslog_options $splunk_options"
   420  
   421  	case $(__docker_value_of_option --log-driver) in
   422  		'')
   423  			COMPREPLY=( $( compgen -W "$all_options" -S = -- "$cur" ) )
   424  			;;
   425  		awslogs)
   426  			COMPREPLY=( $( compgen -W "$awslogs_options" -S = -- "$cur" ) )
   427  			;;
   428  		fluentd)
   429  			COMPREPLY=( $( compgen -W "$fluentd_options" -S = -- "$cur" ) )
   430  			;;
   431  		gelf)
   432  			COMPREPLY=( $( compgen -W "$gelf_options" -S = -- "$cur" ) )
   433  			;;
   434  		journald)
   435  			COMPREPLY=( $( compgen -W "$journald_options" -S = -- "$cur" ) )
   436  			;;
   437  		json-file)
   438  			COMPREPLY=( $( compgen -W "$json_file_options" -S = -- "$cur" ) )
   439  			;;
   440  		syslog)
   441  			COMPREPLY=( $( compgen -W "$syslog_options" -S = -- "$cur" ) )
   442  			;;
   443  		splunk)
   444  			COMPREPLY=( $( compgen -W "$splunk_options" -S = -- "$cur" ) )
   445  			;;
   446  		*)
   447  			return
   448  			;;
   449  	esac
   450  
   451  	__docker_nospace
   452  }
   453  
   454  __docker_complete_log_driver_options() {
   455  	# "=" gets parsed to a word and assigned to either $cur or $prev depending on whether
   456  	# it is the last character or not. So we search for "xxx=" in the the last two words.
   457  	case "${words[$cword-2]}$prev=" in
   458  		*gelf-address=*)
   459  			COMPREPLY=( $( compgen -W "udp" -S "://" -- "${cur#=}" ) )
   460  			__docker_nospace
   461  			return
   462  			;;
   463  		*syslog-address=*)
   464  			COMPREPLY=( $( compgen -W "tcp:// tcp+tls:// udp:// unix://" -- "${cur#=}" ) )
   465  			__docker_nospace
   466  			__ltrim_colon_completions "${cur}"
   467  			return
   468  			;;
   469  		*syslog-facility=*)
   470  			COMPREPLY=( $( compgen -W "
   471  				auth
   472  				authpriv
   473  				cron
   474  				daemon
   475  				ftp
   476  				kern
   477  				local0
   478  				local1
   479  				local2
   480  				local3
   481  				local4
   482  				local5
   483  				local6
   484  				local7
   485  				lpr
   486  				mail
   487  				news
   488  				syslog
   489  				user
   490  				uucp
   491  			" -- "${cur#=}" ) )
   492  			return
   493  			;;
   494  		*syslog-tls-@(ca-cert|cert|key)=*)
   495  			_filedir
   496  			return
   497  			;;
   498  		*syslog-tls-skip-verify=*)
   499  			COMPREPLY=( $( compgen -W "true" -- "${cur#=}" ) )
   500  			return
   501  			;;
   502  		*splunk-url=*)
   503  			COMPREPLY=( $( compgen -W "http:// https://" -- "${cur#=}" ) )
   504  			__docker_nospace
   505  			__ltrim_colon_completions "${cur}"
   506  			return
   507  			;;
   508  		*splunk-insecureskipverify=*)
   509  			COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
   510  			__docker_nospace
   511  			return
   512  			;;
   513  	esac
   514  	return 1
   515  }
   516  
   517  __docker_complete_log_levels() {
   518  	COMPREPLY=( $( compgen -W "debug info warn error fatal" -- "$cur" ) )
   519  }
   520  
   521  # a selection of the available signals that is most likely of interest in the
   522  # context of docker containers.
   523  __docker_complete_signals() {
   524  	local signals=(
   525  		SIGCONT
   526  		SIGHUP
   527  		SIGINT
   528  		SIGKILL
   529  		SIGQUIT
   530  		SIGSTOP
   531  		SIGTERM
   532  		SIGUSR1
   533  		SIGUSR2
   534  	)
   535  	COMPREPLY=( $( compgen -W "${signals[*]} ${signals[*]#SIG}" -- "$( echo $cur | tr '[:lower:]' '[:upper:]')" ) )
   536  }
   537  
   538  __docker_complete_user_group() {
   539  	if [[ $cur == *:* ]] ; then
   540  		COMPREPLY=( $(compgen -g -- "${cur#*:}") )
   541  	else
   542  		COMPREPLY=( $(compgen -u -S : -- "$cur") )
   543  		__docker_nospace
   544  	fi
   545  }
   546  
   547  # global options that may appear after the docker command
   548  _docker_docker() {
   549  	local boolean_options="
   550  		$global_boolean_options
   551  		--help
   552  		--version -v
   553  	"
   554  
   555  	case "$prev" in
   556  		--config)
   557  			_filedir -d
   558  			return
   559  			;;
   560  		--log-level|-l)
   561  			__docker_complete_log_levels
   562  			return
   563  			;;
   564  		$(__docker_to_extglob "$global_options_with_args") )
   565  			return
   566  			;;
   567  	esac
   568  
   569  	case "$cur" in
   570  		-*)
   571  			COMPREPLY=( $( compgen -W "$boolean_options $global_options_with_args" -- "$cur" ) )
   572  			;;
   573  		*)
   574  			local counter=$( __docker_pos_first_nonflag $(__docker_to_extglob "$global_options_with_args") )
   575  			if [ $cword -eq $counter ]; then
   576  				COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
   577  			fi
   578  			;;
   579  	esac
   580  }
   581  
   582  _docker_attach() {
   583  	__docker_complete_detach-keys && return
   584  
   585   	case "$cur" in
   586  		-*)
   587  			COMPREPLY=( $( compgen -W "--detach-keys --help --no-stdin --sig-proxy" -- "$cur" ) )
   588  			;;
   589  		*)
   590  			local counter=$(__docker_pos_first_nonflag '--detach-keys')
   591  			if [ $cword -eq $counter ]; then
   592  				__docker_complete_containers_running
   593  			fi
   594  			;;
   595  	esac
   596  }
   597  
   598  _docker_build() {
   599  	local options_with_args="
   600  		--build-arg
   601  		--cgroup-parent
   602  		--cpuset-cpus
   603  		--cpuset-mems
   604  		--cpu-shares
   605  		--cpu-period
   606  		--cpu-quota
   607  		--file -f
   608  		--isolation
   609  		--memory -m
   610  		--memory-swap
   611  		--shm-size
   612  		--tag -t
   613  		--ulimit
   614  	"
   615  
   616  	local boolean_options="
   617  		--disable-content-trust=false
   618  		--force-rm
   619  		--help
   620  		--no-cache
   621  		--pull
   622  		--quiet -q
   623  		--rm
   624  	"
   625  
   626  	local all_options="$options_with_args $boolean_options"
   627  
   628  	case "$prev" in
   629  		--build-arg)
   630  			COMPREPLY=( $( compgen -e -- "$cur" ) )
   631  			__docker_nospace
   632  			return
   633  			;;
   634  		--file|-f)
   635  			_filedir
   636  			return
   637  			;;
   638  		--isolation)
   639  			__docker_complete_isolation
   640  			return
   641  			;;
   642  		--tag|-t)
   643  			__docker_complete_image_repos_and_tags
   644  			return
   645  			;;
   646  		$(__docker_to_extglob "$options_with_args") )
   647  			return
   648  			;;
   649  	esac
   650  
   651  	case "$cur" in
   652  		-*)
   653  			COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
   654  			;;
   655  		*)
   656  			local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
   657  			if [ $cword -eq $counter ]; then
   658  				_filedir -d
   659  			fi
   660  			;;
   661  	esac
   662  }
   663  
   664  _docker_commit() {
   665  	case "$prev" in
   666  		--author|-a|--change|-c|--message|-m)
   667  			return
   668  			;;
   669  	esac
   670  
   671  	case "$cur" in
   672  		-*)
   673  			COMPREPLY=( $( compgen -W "--author -a --change -c --help --message -m --pause -p" -- "$cur" ) )
   674  			;;
   675  		*)
   676  			local counter=$(__docker_pos_first_nonflag '--author|-a|--change|-c|--message|-m')
   677  
   678  			if [ $cword -eq $counter ]; then
   679  				__docker_complete_containers_all
   680  				return
   681  			fi
   682  			(( counter++ ))
   683  
   684  			if [ $cword -eq $counter ]; then
   685  				__docker_complete_image_repos_and_tags
   686  				return
   687  			fi
   688  			;;
   689  	esac
   690  }
   691  
   692  _docker_cp() {
   693  	case "$cur" in
   694  		-*)
   695  			COMPREPLY=( $( compgen -W "--follow-link -L --help" -- "$cur" ) )
   696  			;;
   697  		*)
   698  			local counter=$(__docker_pos_first_nonflag)
   699  			if [ $cword -eq $counter ]; then
   700  				case "$cur" in
   701  					*:)
   702  						return
   703  						;;
   704  					*)
   705  						# combined container and filename completion
   706  						_filedir
   707  						local files=( ${COMPREPLY[@]} )
   708  
   709  						__docker_complete_containers_all
   710  						COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
   711  						local containers=( ${COMPREPLY[@]} )
   712  
   713  						COMPREPLY=( $( compgen -W "${files[*]} ${containers[*]}" -- "$cur" ) )
   714  						if [[ "$COMPREPLY" == *: ]]; then
   715  							__docker_nospace
   716  						fi
   717  						return
   718  						;;
   719  				esac
   720  			fi
   721  			(( counter++ ))
   722  
   723  			if [ $cword -eq $counter ]; then
   724  				if [ -e "$prev" ]; then
   725  					__docker_complete_containers_all
   726  					COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
   727  					__docker_nospace
   728  				else
   729  					_filedir
   730  				fi
   731  				return
   732  			fi
   733  			;;
   734  	esac
   735  }
   736  
   737  _docker_create() {
   738  	_docker_run
   739  }
   740  
   741  _docker_daemon() {
   742  	local boolean_options="
   743  		$global_boolean_options
   744  		--disable-legacy-registry
   745  		--help
   746  		--icc=false
   747  		--ip-forward=false
   748  		--ip-masq=false
   749  		--iptables=false
   750  		--ipv6
   751  		--selinux-enabled
   752  		--userland-proxy=false
   753  	"
   754  	local options_with_args="
   755  		$global_options_with_args
   756  		--api-cors-header
   757  		--authorization-plugin
   758  		--bip
   759  		--bridge -b
   760  		--cgroup-parent
   761  		--cluster-advertise
   762  		--cluster-store
   763  		--cluster-store-opt
   764  		--default-gateway
   765  		--default-gateway-v6
   766  		--default-ulimit
   767  		--dns
   768  		--dns-search
   769  		--dns-opt
   770  		--exec-opt
   771  		--exec-root
   772  		--fixed-cidr
   773  		--fixed-cidr-v6
   774  		--graph -g
   775  		--group -G
   776  		--insecure-registry
   777  		--ip
   778  		--label
   779  		--log-driver
   780  		--log-opt
   781  		--mtu
   782  		--pidfile -p
   783  		--registry-mirror
   784  		--storage-driver -s
   785  		--storage-opt
   786  		--userns-remap
   787  	"
   788  
   789  	case "$prev" in
   790  		--authorization-plugin)
   791  			__docker_complete_plugins Authorization
   792  			return
   793  			;;
   794  		--cluster-store)
   795  			COMPREPLY=( $( compgen -W "consul etcd zk" -S "://" -- "$cur" ) )
   796  			__docker_nospace
   797  			return
   798  			;;
   799  		--cluster-store-opt)
   800  			COMPREPLY=( $( compgen -W "discovery.heartbeat discovery.ttl kv.cacertfile kv.certfile kv.keyfile kv.path" -S = -- "$cur" ) )
   801  			__docker_nospace
   802  			return
   803  			;;
   804  		--exec-root|--graph|-g)
   805  			_filedir -d
   806  			return
   807  			;;
   808  		--log-driver)
   809  			__docker_complete_log_drivers
   810  			return
   811  			;;
   812  		--pidfile|-p|--tlscacert|--tlscert|--tlskey)
   813  			_filedir
   814  			return
   815  			;;
   816  		--storage-driver|-s)
   817  			COMPREPLY=( $( compgen -W "aufs btrfs devicemapper overlay vfs zfs" -- "$(echo $cur | tr '[:upper:]' '[:lower:]')" ) )
   818  			return
   819  			;;
   820  		--storage-opt)
   821  			local devicemapper_options="
   822  				dm.basesize
   823  				dm.blkdiscard
   824  				dm.blocksize
   825  				dm.fs
   826  				dm.loopdatasize
   827  				dm.loopmetadatasize
   828  				dm.mkfsarg
   829  				dm.mountopt
   830  				dm.override_udev_sync_check
   831  				dm.thinpooldev
   832  				dm.use_deferred_deletion
   833  				dm.use_deferred_removal
   834  			"
   835  			local zfs_options="zfs.fsname"
   836  
   837  			case $(__docker_value_of_option '--storage-driver|-s') in
   838  				'')
   839  					COMPREPLY=( $( compgen -W "$devicemapper_options $zfs_options" -S = -- "$cur" ) )
   840  					;;
   841  				devicemapper)
   842  					COMPREPLY=( $( compgen -W "$devicemapper_options" -S = -- "$cur" ) )
   843  					;;
   844  				zfs)
   845  					COMPREPLY=( $( compgen -W "$zfs_options" -S = -- "$cur" ) )
   846  					;;
   847  				*)
   848  					return
   849  					;;
   850  			esac
   851  			__docker_nospace
   852  			return
   853  			;;
   854  		--log-level|-l)
   855  			__docker_complete_log_levels
   856  			return
   857  			;;
   858  		--log-opt)
   859  			__docker_complete_log_options
   860  			return
   861  			;;
   862  		--userns-remap)
   863  			__docker_complete_user_group
   864  			return
   865  			;;
   866  		$(__docker_to_extglob "$options_with_args") )
   867  			return
   868  			;;
   869  	esac
   870  
   871  	__docker_complete_log_driver_options && return
   872  
   873  	case "${words[$cword-2]}$prev=" in
   874  		# completions for --storage-opt
   875  		*dm.@(blkdiscard|override_udev_sync_check|use_deferred_@(removal|deletion))=*)
   876  			COMPREPLY=( $( compgen -W "false true" -- "${cur#=}" ) )
   877  			return
   878  			;;
   879  		*dm.fs=*)
   880  			COMPREPLY=( $( compgen -W "ext4 xfs" -- "${cur#=}" ) )
   881  			return
   882  			;;
   883  		*dm.thinpooldev=*)
   884  			_filedir
   885  			return
   886  			;;
   887  		# completions for --cluster-store-opt
   888  		*kv.*file=*)
   889  			_filedir
   890  			return
   891  			;;
   892  	esac
   893  
   894  	case "$cur" in
   895  		-*)
   896  			COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) )
   897  			;;
   898  	esac
   899  }
   900  
   901  _docker_diff() {
   902  	case "$cur" in
   903  		-*)
   904  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
   905  			;;
   906  		*)
   907  			local counter=$(__docker_pos_first_nonflag)
   908  			if [ $cword -eq $counter ]; then
   909  				__docker_complete_containers_all
   910  			fi
   911  			;;
   912  	esac
   913  }
   914  
   915  _docker_events() {
   916  	local filter=$(__docker_map_key_of_current_option '-f|--filter')
   917  	case "$filter" in
   918  		container)
   919  			cur="${cur##*=}"
   920  			__docker_complete_containers_all
   921  			return
   922  			;;
   923  		event)
   924  			COMPREPLY=( $( compgen -W "
   925  				attach
   926  				commit
   927  				connect
   928  				copy
   929  				create
   930  				delete
   931  				destroy
   932  				die
   933  				disconnect
   934  				exec_create
   935  				exec_start
   936  				export
   937  				import
   938  				kill
   939  				mount
   940  				oom
   941  				pause
   942  				pull
   943  				push
   944  				rename
   945  				resize
   946  				restart
   947  				start
   948  				stop
   949  				tag
   950  				top
   951  				unmount
   952  				unpause
   953  				untag
   954  				update
   955  			" -- "${cur##*=}" ) )
   956  			return
   957  			;;
   958  		image)
   959  			cur="${cur##*=}"
   960  			__docker_complete_images
   961  			return
   962  			;;
   963  		network)
   964  			cur="${cur##*=}"
   965  			__docker_complete_networks
   966  			return
   967  			;;
   968  		type)
   969  			COMPREPLY=( $( compgen -W "container image network volume" -- "${cur##*=}" ) )
   970  			return
   971  			;;
   972  		volume)
   973  			cur="${cur##*=}"
   974  			__docker_complete_volumes
   975  			return
   976  			;;
   977  	esac
   978  
   979  	case "$prev" in
   980  		--filter|-f)
   981  			COMPREPLY=( $( compgen -S = -W "container event image label network type volume" -- "$cur" ) )
   982  			__docker_nospace
   983  			return
   984  			;;
   985  		--since|--until)
   986  			return
   987  			;;
   988  	esac
   989  
   990  	case "$cur" in
   991  		-*)
   992  			COMPREPLY=( $( compgen -W "--filter -f --help --since --until" -- "$cur" ) )
   993  			;;
   994  	esac
   995  }
   996  
   997  _docker_exec() {
   998  	__docker_complete_detach-keys && return
   999  
  1000  	case "$prev" in
  1001  		--user|-u)
  1002  			__docker_complete_user_group
  1003  			return
  1004  			;;
  1005  	esac
  1006  
  1007  	case "$cur" in
  1008  		-*)
  1009  			COMPREPLY=( $( compgen -W "--detach -d --detach-keys --help --interactive -i --privileged -t --tty -u --user" -- "$cur" ) )
  1010  			;;
  1011  		*)
  1012  			__docker_complete_containers_running
  1013  			;;
  1014  	esac
  1015  }
  1016  
  1017  _docker_export() {
  1018  	case "$cur" in
  1019  		-*)
  1020  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1021  			;;
  1022  		*)
  1023  			local counter=$(__docker_pos_first_nonflag)
  1024  			if [ $cword -eq $counter ]; then
  1025  				__docker_complete_containers_all
  1026  			fi
  1027  			;;
  1028  	esac
  1029  }
  1030  
  1031  _docker_help() {
  1032  	local counter=$(__docker_pos_first_nonflag)
  1033  	if [ $cword -eq $counter ]; then
  1034  		COMPREPLY=( $( compgen -W "${commands[*]}" -- "$cur" ) )
  1035  	fi
  1036  }
  1037  
  1038  _docker_history() {
  1039  	case "$cur" in
  1040  		-*)
  1041  			COMPREPLY=( $( compgen -W "--help --no-trunc --quiet -q" -- "$cur" ) )
  1042  			;;
  1043  		*)
  1044  			local counter=$(__docker_pos_first_nonflag)
  1045  			if [ $cword -eq $counter ]; then
  1046  				__docker_complete_images
  1047  			fi
  1048  			;;
  1049  	esac
  1050  }
  1051  
  1052  _docker_images() {
  1053  	case "$prev" in
  1054  		--filter|-f)
  1055  			COMPREPLY=( $( compgen -S = -W "dangling label" -- "$cur" ) )
  1056  			__docker_nospace
  1057  			return
  1058  			;;
  1059                  --format)
  1060  			return
  1061  			;;
  1062  	esac
  1063  
  1064  	case "${words[$cword-2]}$prev=" in
  1065  		*dangling=*)
  1066  			COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
  1067  			return
  1068  			;;
  1069  		*label=*)
  1070  			return
  1071  			;;
  1072  	esac
  1073  
  1074  	case "$cur" in
  1075  		-*)
  1076  			COMPREPLY=( $( compgen -W "--all -a --digests --filter -f --format --help --no-trunc --quiet -q" -- "$cur" ) )
  1077  			;;
  1078  		=)
  1079  			return
  1080  			;;
  1081  		*)
  1082  			__docker_complete_image_repos
  1083  			;;
  1084  	esac
  1085  }
  1086  
  1087  _docker_import() {
  1088  	case "$prev" in
  1089  		--change|-c|--message|-m)
  1090  			return
  1091  			;;
  1092  	esac
  1093  
  1094  	case "$cur" in
  1095  		-*)
  1096  			COMPREPLY=( $( compgen -W "--change -c --help --message -m" -- "$cur" ) )
  1097  			;;
  1098  		*)
  1099  			local counter=$(__docker_pos_first_nonflag '--change|-c|--message|-m')
  1100  			if [ $cword -eq $counter ]; then
  1101  				return
  1102  			fi
  1103  			(( counter++ ))
  1104  
  1105  			if [ $cword -eq $counter ]; then
  1106  				__docker_complete_image_repos_and_tags
  1107  				return
  1108  			fi
  1109  			;;
  1110  	esac
  1111  }
  1112  
  1113  _docker_info() {
  1114  	case "$cur" in
  1115  		-*)
  1116  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1117  			;;
  1118  	esac
  1119  }
  1120  
  1121  _docker_inspect() {
  1122  	case "$prev" in
  1123  		--format|-f)
  1124  			return
  1125  			;;
  1126  		--type)
  1127                       COMPREPLY=( $( compgen -W "image container" -- "$cur" ) )
  1128                       return
  1129                          ;;
  1130  
  1131  	esac
  1132  
  1133  	case "$cur" in
  1134  		-*)
  1135  			COMPREPLY=( $( compgen -W "--format -f --help --size -s --type" -- "$cur" ) )
  1136  			;;
  1137  		*)
  1138  			case $(__docker_value_of_option --type) in
  1139  				'')
  1140  					__docker_complete_containers_and_images
  1141  					;;
  1142  				container)
  1143  					__docker_complete_containers_all
  1144  					;;
  1145  				image)
  1146  					__docker_complete_images
  1147  					;;
  1148  			esac
  1149  	esac
  1150  }
  1151  
  1152  _docker_kill() {
  1153  	case "$prev" in
  1154  		--signal|-s)
  1155  			__docker_complete_signals
  1156  			return
  1157  			;;
  1158  	esac
  1159  
  1160  	case "$cur" in
  1161  		-*)
  1162  			COMPREPLY=( $( compgen -W "--help --signal -s" -- "$cur" ) )
  1163  			;;
  1164  		*)
  1165  			__docker_complete_containers_running
  1166  			;;
  1167  	esac
  1168  }
  1169  
  1170  _docker_load() {
  1171  	case "$prev" in
  1172  		--input|-i)
  1173  			_filedir
  1174  			return
  1175  			;;
  1176  	esac
  1177  
  1178  	case "$cur" in
  1179  		-*)
  1180  			COMPREPLY=( $( compgen -W "--help --input -i" -- "$cur" ) )
  1181  			;;
  1182  	esac
  1183  }
  1184  
  1185  _docker_login() {
  1186  	case "$prev" in
  1187  		--email|-e|--password|-p|--username|-u)
  1188  			return
  1189  			;;
  1190  	esac
  1191  
  1192  	case "$cur" in
  1193  		-*)
  1194  			COMPREPLY=( $( compgen -W "--email -e --help --password -p --username -u" -- "$cur" ) )
  1195  			;;
  1196  	esac
  1197  }
  1198  
  1199  _docker_logout() {
  1200  	case "$cur" in
  1201  		-*)
  1202  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1203  			;;
  1204  	esac
  1205  }
  1206  
  1207  _docker_logs() {
  1208  	case "$prev" in
  1209  		--since|--tail)
  1210  			return
  1211  			;;
  1212  	esac
  1213  
  1214  	case "$cur" in
  1215  		-*)
  1216  			COMPREPLY=( $( compgen -W "--follow -f --help --since --tail --timestamps -t" -- "$cur" ) )
  1217  			;;
  1218  		*)
  1219  			local counter=$(__docker_pos_first_nonflag '--tail')
  1220  			if [ $cword -eq $counter ]; then
  1221  				__docker_complete_containers_all
  1222  			fi
  1223  			;;
  1224  	esac
  1225  }
  1226  
  1227  _docker_network_connect() {
  1228  	local options_with_args="
  1229  		--alias
  1230  		--ip
  1231  		--ip6
  1232  		--link
  1233  	"
  1234  
  1235  	local boolean_options="
  1236  		--help
  1237  	"
  1238  
  1239  	case "$prev" in
  1240  		--link)
  1241  			case "$cur" in
  1242  				*:*)
  1243  					;;
  1244  				*)
  1245  					__docker_complete_containers_running
  1246  					COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  1247  					__docker_nospace
  1248  					;;
  1249  			esac
  1250  			return
  1251  			;;
  1252  		$(__docker_to_extglob "$options_with_args") )
  1253  			return
  1254  			;;
  1255  	esac
  1256  
  1257  	case "$cur" in
  1258  		-*)
  1259  			COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) )
  1260  			;;
  1261  		*)
  1262  			local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  1263  			if [ $cword -eq $counter ]; then
  1264  				__docker_complete_networks
  1265  			elif [ $cword -eq $(($counter + 1)) ]; then
  1266  				__docker_complete_containers_all
  1267  			fi
  1268  			;;
  1269  	esac
  1270  }
  1271  
  1272  _docker_network_create() {
  1273  	case "$prev" in
  1274  		--aux-address|--gateway|--ip-range|--ipam-opt|--opt|-o|--subnet)
  1275  			return
  1276  			;;
  1277  		--ipam-driver)
  1278  			COMPREPLY=( $( compgen -W "default" -- "$cur" ) )
  1279  			return
  1280  			;;
  1281  		--driver|-d)
  1282  			local plugins=" $(__docker_plugins Network) "
  1283  			# remove drivers that allow one instance only
  1284  			plugins=${plugins/ host / }
  1285  			plugins=${plugins/ null / }
  1286  			COMPREPLY=( $(compgen -W "$plugins" -- "$cur") )
  1287  			return
  1288  			;;
  1289  	esac
  1290  
  1291  	case "$cur" in
  1292  		-*)
  1293  			COMPREPLY=( $( compgen -W "--aux-address --driver -d --gateway --help --internal --ip-range --ipam-driver --ipam-opt --opt -o --subnet" -- "$cur" ) )
  1294  			;;
  1295  	esac
  1296  }
  1297  
  1298  _docker_network_disconnect() {
  1299  	case "$cur" in
  1300  		-*)
  1301  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1302  			;;
  1303  		*)
  1304  			local counter=$(__docker_pos_first_nonflag)
  1305  			if [ $cword -eq $counter ]; then
  1306  				__docker_complete_networks
  1307  			elif [ $cword -eq $(($counter + 1)) ]; then
  1308  				__docker_complete_containers_in_network "$prev"
  1309  			fi
  1310  			;;
  1311  	esac
  1312  }
  1313  
  1314  _docker_network_inspect() {
  1315  	case "$prev" in
  1316  		--format|-f)
  1317  			return
  1318  			;;
  1319  	esac
  1320  
  1321  	case "$cur" in
  1322  		-*)
  1323  			COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
  1324  			;;
  1325  		*)
  1326  			__docker_complete_networks
  1327  	esac
  1328  }
  1329  
  1330  _docker_network_ls() {
  1331  	case "$prev" in
  1332  		--filter|-f)
  1333  			COMPREPLY=( $( compgen -S = -W "id name type" -- "$cur" ) )
  1334  			__docker_nospace
  1335  			return
  1336  			;;
  1337  	esac
  1338  
  1339  	case "${words[$cword-2]}$prev=" in
  1340  		*id=*)
  1341  			cur="${cur#=}"
  1342  			__docker_complete_network_ids
  1343  			return
  1344  			;;
  1345  		*name=*)
  1346  			cur="${cur#=}"
  1347  			__docker_complete_network_names
  1348  			return
  1349  			;;
  1350  		*type=*)
  1351  			COMPREPLY=( $( compgen -W "builtin custom" -- "${cur#=}" ) )
  1352  			return
  1353  			;;
  1354  	esac
  1355  
  1356  	case "$cur" in
  1357  		-*)
  1358  			COMPREPLY=( $( compgen -W "--filter -f --help --no-trunc --quiet -q" -- "$cur" ) )
  1359  			;;
  1360  	esac
  1361  }
  1362  
  1363  _docker_network_rm() {
  1364  	case "$cur" in
  1365  		-*)
  1366  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1367  			;;
  1368  		*)
  1369  			__docker_complete_networks type=custom
  1370  	esac
  1371  }
  1372  
  1373  _docker_network() {
  1374  	local subcommands="
  1375  		connect
  1376  		create
  1377  		disconnect
  1378  		inspect
  1379  		ls
  1380  		rm
  1381  	"
  1382  	__docker_subcommands "$subcommands" && return
  1383  
  1384  	case "$cur" in
  1385  		-*)
  1386  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1387  			;;
  1388  		*)
  1389  			COMPREPLY=( $( compgen -W "$subcommands" -- "$cur" ) )
  1390  			;;
  1391  	esac
  1392  }
  1393  
  1394  _docker_pause() {
  1395  	case "$cur" in
  1396  		-*)
  1397  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1398  			;;
  1399  		*)
  1400  			local counter=$(__docker_pos_first_nonflag)
  1401  			if [ $cword -eq $counter ]; then
  1402  				__docker_complete_containers_pauseable
  1403  			fi
  1404  			;;
  1405  	esac
  1406  }
  1407  
  1408  _docker_port() {
  1409  	case "$cur" in
  1410  		-*)
  1411  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1412  			;;
  1413  		*)
  1414  			local counter=$(__docker_pos_first_nonflag)
  1415  			if [ $cword -eq $counter ]; then
  1416  				__docker_complete_containers_all
  1417  			fi
  1418  			;;
  1419  	esac
  1420  }
  1421  
  1422  _docker_ps() {
  1423  	case "$prev" in
  1424  		--before|--since)
  1425  			__docker_complete_containers_all
  1426  			;;
  1427  		--filter|-f)
  1428  			COMPREPLY=( $( compgen -S = -W "ancestor exited id label name status" -- "$cur" ) )
  1429  			__docker_nospace
  1430  			return
  1431  			;;
  1432  		--format|-n)
  1433  			return
  1434  			;;
  1435  	esac
  1436  
  1437  	case "${words[$cword-2]}$prev=" in
  1438  		*ancestor=*)
  1439  			cur="${cur#=}"
  1440  			__docker_complete_images
  1441  			return
  1442  			;;
  1443  		*id=*)
  1444  			cur="${cur#=}"
  1445  			__docker_complete_container_ids
  1446  			return
  1447  			;;
  1448  		*name=*)
  1449  			cur="${cur#=}"
  1450  			__docker_complete_container_names
  1451  			return
  1452  			;;
  1453  		*status=*)
  1454  			COMPREPLY=( $( compgen -W "created dead exited paused restarting running" -- "${cur#=}" ) )
  1455  			return
  1456  			;;
  1457  	esac
  1458  
  1459  	case "$cur" in
  1460  		-*)
  1461  			COMPREPLY=( $( compgen -W "--all -a --before --filter -f --format --help --latest -l -n --no-trunc --quiet -q --size -s --since" -- "$cur" ) )
  1462  			;;
  1463  	esac
  1464  }
  1465  
  1466  _docker_pull() {
  1467  	case "$cur" in
  1468  		-*)
  1469  			COMPREPLY=( $( compgen -W "--all-tags -a --help" -- "$cur" ) )
  1470  			;;
  1471  		*)
  1472  			local counter=$(__docker_pos_first_nonflag)
  1473  			if [ $cword -eq $counter ]; then
  1474  				for arg in "${COMP_WORDS[@]}"; do
  1475  					case "$arg" in
  1476  						--all-tags|-a)
  1477  							__docker_complete_image_repos
  1478  							return
  1479  							;;
  1480  					esac
  1481  				done
  1482  				__docker_complete_image_repos_and_tags
  1483  			fi
  1484  			;;
  1485  	esac
  1486  }
  1487  
  1488  _docker_push() {
  1489  	case "$cur" in
  1490  		-*)
  1491  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1492  			;;
  1493  		*)
  1494  			local counter=$(__docker_pos_first_nonflag)
  1495  			if [ $cword -eq $counter ]; then
  1496  				__docker_complete_image_repos_and_tags
  1497  			fi
  1498  			;;
  1499  	esac
  1500  }
  1501  
  1502  _docker_rename() {
  1503  	case "$cur" in
  1504  		-*)
  1505  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1506  			;;
  1507  		*)
  1508  			local counter=$(__docker_pos_first_nonflag)
  1509  			if [ $cword -eq $counter ]; then
  1510  				__docker_complete_containers_all
  1511  			fi
  1512  			;;
  1513  	esac
  1514  }
  1515  
  1516  _docker_restart() {
  1517  	case "$prev" in
  1518  		--time|-t)
  1519  			return
  1520  			;;
  1521  	esac
  1522  
  1523  	case "$cur" in
  1524  		-*)
  1525  			COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  1526  			;;
  1527  		*)
  1528  			__docker_complete_containers_all
  1529  			;;
  1530  	esac
  1531  }
  1532  
  1533  _docker_rm() {
  1534  	case "$cur" in
  1535  		-*)
  1536  			COMPREPLY=( $( compgen -W "--force -f --help --link -l --volumes -v" -- "$cur" ) )
  1537  			;;
  1538  		*)
  1539  			for arg in "${COMP_WORDS[@]}"; do
  1540  				case "$arg" in
  1541  					--force|-f)
  1542  						__docker_complete_containers_all
  1543  						return
  1544  						;;
  1545  				esac
  1546  			done
  1547  			__docker_complete_containers_stopped
  1548  			;;
  1549  	esac
  1550  }
  1551  
  1552  _docker_rmi() {
  1553  	case "$cur" in
  1554  		-*)
  1555  			COMPREPLY=( $( compgen -W "--force -f --help --no-prune" -- "$cur" ) )
  1556  			;;
  1557  		*)
  1558  			__docker_complete_images
  1559  			;;
  1560  	esac
  1561  }
  1562  
  1563  _docker_run() {
  1564  	local options_with_args="
  1565  		--add-host
  1566  		--attach -a
  1567  		--blkio-weight
  1568  		--blkio-weight-device
  1569  		--cap-add
  1570  		--cap-drop
  1571  		--cgroup-parent
  1572  		--cidfile
  1573  		--cpu-period
  1574  		--cpu-quota
  1575  		--cpuset-cpus
  1576  		--cpuset-mems
  1577  		--cpu-shares
  1578  		--device
  1579  		--device-read-bps
  1580  		--device-read-iops
  1581  		--device-write-bps
  1582  		--device-write-iops
  1583  		--dns
  1584  		--dns-opt
  1585  		--dns-search
  1586  		--entrypoint
  1587  		--env -e
  1588  		--env-file
  1589  		--expose
  1590  		--group-add
  1591  		--hostname -h
  1592  		--ip
  1593  		--ip6
  1594  		--ipc
  1595  		--isolation
  1596  		--kernel-memory
  1597  		--label-file
  1598  		--label -l
  1599  		--link
  1600  		--log-driver
  1601  		--log-opt
  1602  		--mac-address
  1603  		--memory -m
  1604  		--memory-swap
  1605  		--memory-swappiness
  1606  		--memory-reservation
  1607  		--name
  1608  		--net
  1609  		--net-alias
  1610  		--oom-score-adj
  1611  		--pid
  1612  		--publish -p
  1613  		--restart
  1614  		--security-opt
  1615  		--shm-size
  1616  		--stop-signal
  1617  		--tmpfs
  1618  		--ulimit
  1619  		--user -u
  1620  		--uts
  1621  		--volume-driver
  1622  		--volumes-from
  1623  		--volume -v
  1624  		--workdir -w
  1625  	"
  1626  
  1627  	local boolean_options="
  1628  		--disable-content-trust=false
  1629  		--help
  1630  		--interactive -i
  1631  		--oom-kill-disable
  1632  		--privileged
  1633  		--publish-all -P
  1634  		--read-only
  1635  		--tty -t
  1636  	"
  1637  
  1638  	if [ "$command" = "run" ] ; then
  1639  		options_with_args="$options_with_args
  1640  			--detach-keys
  1641  		"
  1642  		boolean_options="$boolean_options
  1643  			--detach -d
  1644  			--rm
  1645  			--sig-proxy=false
  1646  		"
  1647  		__docker_complete_detach-keys && return
  1648  	fi
  1649  
  1650  	local all_options="$options_with_args $boolean_options"
  1651  
  1652  
  1653  	case "$prev" in
  1654  		--add-host)
  1655  			case "$cur" in
  1656  				*:)
  1657  					__docker_complete_resolved_hostname
  1658  					return
  1659  					;;
  1660  			esac
  1661  			;;
  1662  		--attach|-a)
  1663  			COMPREPLY=( $( compgen -W 'stdin stdout stderr' -- "$cur" ) )
  1664  			return
  1665  			;;
  1666  		--cap-add|--cap-drop)
  1667  			__docker_complete_capabilities
  1668  			return
  1669  			;;
  1670  		--cidfile|--env-file|--label-file)
  1671  			_filedir
  1672  			return
  1673  			;;
  1674  		--device|--tmpfs|--volume|-v)
  1675  			case "$cur" in
  1676  				*:*)
  1677  					# TODO somehow do _filedir for stuff inside the image, if it's already specified (which is also somewhat difficult to determine)
  1678  					;;
  1679  				'')
  1680  					COMPREPLY=( $( compgen -W '/' -- "$cur" ) )
  1681  					__docker_nospace
  1682  					;;
  1683  				/*)
  1684  					_filedir
  1685  					__docker_nospace
  1686  					;;
  1687  			esac
  1688  			return
  1689  			;;
  1690  		--env|-e)
  1691  			COMPREPLY=( $( compgen -e -- "$cur" ) )
  1692  			__docker_nospace
  1693  			return
  1694  			;;
  1695  		--ipc)
  1696  			case "$cur" in
  1697  				*:*)
  1698  					cur="${cur#*:}"
  1699  					__docker_complete_containers_running
  1700  					;;
  1701  				*)
  1702  					COMPREPLY=( $( compgen -W 'host container:' -- "$cur" ) )
  1703  					if [ "$COMPREPLY" = "container:" ]; then
  1704  						__docker_nospace
  1705  					fi
  1706  					;;
  1707  			esac
  1708  			return
  1709  			;;
  1710  		--isolation)
  1711  			__docker_complete_isolation
  1712  			return
  1713  			;;
  1714  		--link)
  1715  			case "$cur" in
  1716  				*:*)
  1717  					;;
  1718  				*)
  1719  					__docker_complete_containers_running
  1720  					COMPREPLY=( $( compgen -W "${COMPREPLY[*]}" -S ':' ) )
  1721  					__docker_nospace
  1722  					;;
  1723  			esac
  1724  			return
  1725  			;;
  1726  		--log-driver)
  1727  			__docker_complete_log_drivers
  1728  			return
  1729  			;;
  1730  		--log-opt)
  1731  			__docker_complete_log_options
  1732  			return
  1733  			;;
  1734  		--net)
  1735  			case "$cur" in
  1736  				container:*)
  1737  					local cur=${cur#*:}
  1738  					__docker_complete_containers_all
  1739  					;;
  1740  				*)
  1741  					COMPREPLY=( $( compgen -W "$(__docker_plugins Network) $(__docker_networks) container:" -- "$cur") )
  1742  					if [ "${COMPREPLY[*]}" = "container:" ] ; then
  1743  						__docker_nospace
  1744  					fi
  1745  					;;
  1746  			esac
  1747  			return
  1748  			;;
  1749  		--restart)
  1750  			case "$cur" in
  1751  				on-failure:*)
  1752  					;;
  1753  				*)
  1754  					COMPREPLY=( $( compgen -W "always no on-failure on-failure: unless-stopped" -- "$cur") )
  1755  					;;
  1756  			esac
  1757  			return
  1758  			;;
  1759  		--security-opt)
  1760  			case "$cur" in
  1761  				label:*:*)
  1762  					;;
  1763  				label:*)
  1764  					local cur=${cur##*:}
  1765  					COMPREPLY=( $( compgen -W "user: role: type: level: disable" -- "$cur") )
  1766  					if [ "${COMPREPLY[*]}" != "disable" ] ; then
  1767  						__docker_nospace
  1768  					fi
  1769  					;;
  1770  				*)
  1771  					COMPREPLY=( $( compgen -W "label apparmor seccomp" -S ":" -- "$cur") )
  1772  					__docker_nospace
  1773  					;;
  1774  			esac
  1775  			return
  1776  			;;
  1777  		--user|-u)
  1778  			__docker_complete_user_group
  1779  			return
  1780  			;;
  1781  		--volume-driver)
  1782  			__docker_complete_plugins Volume
  1783  			return
  1784  			;;
  1785  		--volumes-from)
  1786  			__docker_complete_containers_all
  1787  			return
  1788  			;;
  1789  		$(__docker_to_extglob "$options_with_args") )
  1790  			return
  1791  			;;
  1792  	esac
  1793  
  1794  	__docker_complete_log_driver_options && return
  1795  
  1796  	case "$cur" in
  1797  		-*)
  1798  			COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  1799  			;;
  1800  		*)
  1801  			local counter=$( __docker_pos_first_nonflag $( __docker_to_alternatives "$options_with_args" ) )
  1802  			if [ $cword -eq $counter ]; then
  1803  				__docker_complete_images
  1804  			fi
  1805  			;;
  1806  	esac
  1807  }
  1808  
  1809  _docker_save() {
  1810  	case "$prev" in
  1811  		--output|-o)
  1812  			_filedir
  1813  			return
  1814  			;;
  1815  	esac
  1816  
  1817  	case "$cur" in
  1818  		-*)
  1819  			COMPREPLY=( $( compgen -W "--help --output -o" -- "$cur" ) )
  1820  			;;
  1821  		*)
  1822  			__docker_complete_images
  1823  			;;
  1824  	esac
  1825  }
  1826  
  1827  _docker_search() {
  1828  	case "$prev" in
  1829  		--stars|-s)
  1830  			return
  1831  			;;
  1832  	esac
  1833  
  1834  	case "$cur" in
  1835  		-*)
  1836  			COMPREPLY=( $( compgen -W "--automated --help --no-trunc --stars -s" -- "$cur" ) )
  1837  			;;
  1838  	esac
  1839  }
  1840  
  1841  _docker_start() {
  1842  	__docker_complete_detach-keys && return
  1843  
  1844  	case "$cur" in
  1845  		-*)
  1846  			COMPREPLY=( $( compgen -W "--attach -a --detach-keys --help --interactive -i" -- "$cur" ) )
  1847  			;;
  1848  		*)
  1849  			__docker_complete_containers_stopped
  1850  			;;
  1851  	esac
  1852  }
  1853  
  1854  _docker_stats() {
  1855  	case "$cur" in
  1856  		-*)
  1857  			COMPREPLY=( $( compgen -W "--all -a --help --no-stream" -- "$cur" ) )
  1858  			;;
  1859  		*)
  1860  			__docker_complete_containers_running
  1861  			;;
  1862  	esac
  1863  }
  1864  
  1865  _docker_stop() {
  1866  	case "$prev" in
  1867  		--time|-t)
  1868  			return
  1869  			;;
  1870  	esac
  1871  
  1872  	case "$cur" in
  1873  		-*)
  1874  			COMPREPLY=( $( compgen -W "--help --time -t" -- "$cur" ) )
  1875  			;;
  1876  		*)
  1877  			__docker_complete_containers_running
  1878  			;;
  1879  	esac
  1880  }
  1881  
  1882  _docker_tag() {
  1883  	case "$cur" in
  1884  		-*)
  1885  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1886  			;;
  1887  		*)
  1888  			local counter=$(__docker_pos_first_nonflag)
  1889  
  1890  			if [ $cword -eq $counter ]; then
  1891  				__docker_complete_image_repos_and_tags
  1892  				return
  1893  			fi
  1894  			(( counter++ ))
  1895  
  1896  			if [ $cword -eq $counter ]; then
  1897  				__docker_complete_image_repos_and_tags
  1898  				return
  1899  			fi
  1900  			;;
  1901  	esac
  1902  }
  1903  
  1904  _docker_unpause() {
  1905  	case "$cur" in
  1906  		-*)
  1907  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1908  			;;
  1909  		*)
  1910  			local counter=$(__docker_pos_first_nonflag)
  1911  			if [ $cword -eq $counter ]; then
  1912  				__docker_complete_containers_unpauseable
  1913  			fi
  1914  			;;
  1915  	esac
  1916  }
  1917  
  1918  _docker_update() {
  1919  	local options_with_args="
  1920  		--blkio-weight
  1921  		--cpu-period
  1922  		--cpu-quota
  1923  		--cpuset-cpus
  1924  		--cpuset-mems
  1925  		--cpu-shares
  1926  		--kernel-memory
  1927  		--memory -m
  1928  		--memory-reservation
  1929  		--memory-swap
  1930  	"
  1931  
  1932  	local boolean_options="
  1933  		--help
  1934  	"
  1935  
  1936  	local all_options="$options_with_args $boolean_options"
  1937  
  1938  	case "$prev" in
  1939  		$(__docker_to_extglob "$options_with_args") )
  1940  			return
  1941  			;;
  1942  	esac
  1943  
  1944  	case "$cur" in
  1945  		-*)
  1946  			COMPREPLY=( $( compgen -W "$all_options" -- "$cur" ) )
  1947  			;;
  1948  		*)
  1949  			__docker_complete_containers_all
  1950  			;;
  1951  	esac
  1952  }
  1953  
  1954  _docker_top() {
  1955  	case "$cur" in
  1956  		-*)
  1957  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1958  			;;
  1959  		*)
  1960  			local counter=$(__docker_pos_first_nonflag)
  1961  			if [ $cword -eq $counter ]; then
  1962  				__docker_complete_containers_running
  1963  			fi
  1964  			;;
  1965  	esac
  1966  }
  1967  
  1968  _docker_version() {
  1969  	case "$cur" in
  1970  		-*)
  1971  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  1972  			;;
  1973  	esac
  1974  }
  1975  
  1976  _docker_volume_create() {
  1977  	case "$prev" in
  1978  		--driver|-d)
  1979  			__docker_complete_plugins Volume
  1980  			return
  1981  			;;
  1982  		--name|--opt|-o)
  1983  			return
  1984  			;;
  1985  	esac
  1986  
  1987  	case "$cur" in
  1988  		-*)
  1989  			COMPREPLY=( $( compgen -W "--driver -d --help --name --opt -o" -- "$cur" ) )
  1990  			;;
  1991  	esac
  1992  }
  1993  
  1994  _docker_volume_inspect() {
  1995  	case "$prev" in
  1996  		--format|-f)
  1997  			return
  1998  			;;
  1999  	esac
  2000  
  2001  	case "$cur" in
  2002  		-*)
  2003  			COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
  2004  			;;
  2005  		*)
  2006  			__docker_complete_volumes
  2007  			;;
  2008  	esac
  2009  }
  2010  
  2011  _docker_volume_ls() {
  2012  	case "$prev" in
  2013  		--filter|-f)
  2014  			COMPREPLY=( $( compgen -S = -W "dangling" -- "$cur" ) )
  2015  			__docker_nospace
  2016  			return
  2017  			;;
  2018  	esac
  2019  
  2020  	case "${words[$cword-2]}$prev=" in
  2021  		*dangling=*)
  2022  			COMPREPLY=( $( compgen -W "true false" -- "${cur#=}" ) )
  2023  			return
  2024  			;;
  2025  	esac
  2026  
  2027  	case "$cur" in
  2028  		-*)
  2029  			COMPREPLY=( $( compgen -W "--filter -f --help --quiet -q" -- "$cur" ) )
  2030  			;;
  2031  	esac
  2032  }
  2033  
  2034  _docker_volume_rm() {
  2035  	case "$cur" in
  2036  		-*)
  2037  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  2038  			;;
  2039  		*)
  2040  			__docker_complete_volumes
  2041  			;;
  2042  	esac
  2043  }
  2044  
  2045  _docker_volume() {
  2046  	local subcommands="
  2047  		create
  2048  		inspect
  2049  		ls
  2050  		rm
  2051  	"
  2052  	__docker_subcommands "$subcommands" && return
  2053  
  2054  	case "$cur" in
  2055  		-*)
  2056  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  2057  			;;
  2058  		*)
  2059  			COMPREPLY=( $( compgen -W "$subcommands" -- "$cur" ) )
  2060  			;;
  2061  	esac
  2062  }
  2063  
  2064  _docker_wait() {
  2065  	case "$cur" in
  2066  		-*)
  2067  			COMPREPLY=( $( compgen -W "--help" -- "$cur" ) )
  2068  			;;
  2069  		*)
  2070  			__docker_complete_containers_all
  2071  			;;
  2072  	esac
  2073  }
  2074  
  2075  _docker() {
  2076  	local previous_extglob_setting=$(shopt -p extglob)
  2077  	shopt -s extglob
  2078  
  2079  	local commands=(
  2080  		attach
  2081  		build
  2082  		commit
  2083  		cp
  2084  		create
  2085  		daemon
  2086  		diff
  2087  		events
  2088  		exec
  2089  		export
  2090  		history
  2091  		images
  2092  		import
  2093  		info
  2094  		inspect
  2095  		kill
  2096  		load
  2097  		login
  2098  		logout
  2099  		logs
  2100  		network
  2101  		pause
  2102  		port
  2103  		ps
  2104  		pull
  2105  		push
  2106  		rename
  2107  		restart
  2108  		rm
  2109  		rmi
  2110  		run
  2111  		save
  2112  		search
  2113  		start
  2114  		stats
  2115  		stop
  2116  		tag
  2117  		top
  2118  		unpause
  2119  		update
  2120  		version
  2121  		volume
  2122  		wait
  2123  	)
  2124  
  2125  	# These options are valid as global options for all client commands
  2126  	# and valid as command options for `docker daemon`
  2127  	local global_boolean_options="
  2128  		--debug -D
  2129  		--tls
  2130  		--tlsverify
  2131  	"
  2132  	local global_options_with_args="
  2133  		--config
  2134  		--host -H
  2135  		--log-level -l
  2136  		--tlscacert
  2137  		--tlscert
  2138  		--tlskey
  2139  	"
  2140  
  2141  	local host config
  2142  
  2143  	COMPREPLY=()
  2144  	local cur prev words cword
  2145  	_get_comp_words_by_ref -n : cur prev words cword
  2146  
  2147  	local command='docker' command_pos=0 subcommand_pos
  2148  	local counter=1
  2149  	while [ $counter -lt $cword ]; do
  2150  		case "${words[$counter]}" in
  2151  			# save host so that completion can use custom daemon
  2152  			--host|-H)
  2153  				(( counter++ ))
  2154  				host="${words[$counter]}"
  2155  				;;
  2156  			# save config so that completion can use custom configuration directories
  2157  			--config)
  2158  				(( counter++ ))
  2159  				config="${words[$counter]}"
  2160  				;;
  2161  			$(__docker_to_extglob "$global_options_with_args") )
  2162  				(( counter++ ))
  2163  				;;
  2164  			-*)
  2165  				;;
  2166  			=)
  2167  				(( counter++ ))
  2168  				;;
  2169  			*)
  2170  				command="${words[$counter]}"
  2171  				command_pos=$counter
  2172  				break
  2173  				;;
  2174  		esac
  2175  		(( counter++ ))
  2176  	done
  2177  
  2178  	local completions_func=_docker_${command}
  2179  	declare -F $completions_func >/dev/null && $completions_func
  2180  
  2181  	eval "$previous_extglob_setting"
  2182  	return 0
  2183  }
  2184  
  2185  eval "$__docker_previous_extglob_setting"
  2186  unset __docker_previous_extglob_setting
  2187  
  2188  complete -F _docker docker