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