Add bash completion for `docker build --target`
`--file` and `PATH` are honored if the are specified before `--target`.
Note that according to the command's syntax, PATH should be specified
after the options.
@@ -2439,6 +2439,7 @@ _docker_image_build() {
--network
--shm-size
--tag -t
+ --target
--ulimit
"
__docker_daemon_os_is windows && options_with_args+="
@@ -2505,6 +2506,19 @@ _docker_image_build() {
__docker_complete_image_repos_and_tags
return
;;
+ --target)
+ local context_pos=$( __docker_pos_first_nonflag "$( __docker_to_alternatives "$options_with_args" )" )
+ local context="${words[$context_pos]}"
+ context="${context:-.}"
+
+ local file="$( __docker_value_of_option '--file|f' )"
+ local default_file="${context%/}/Dockerfile"
+ local dockerfile="${file:-$default_file}"
+
+ local targets="$( sed -n 's/^FROM .\+ AS \(.\+\)/\1/p' "$dockerfile" 2>/dev/null )"
+ COMPREPLY=( $( compgen -W "$targets" -- "$cur" ) )
+ return
+ ;;
$(__docker_to_extglob "$options_with_args") )
return
;;
