github.com/scaleway/scaleway-cli@v1.11.1/contrib/completion/bash/scw (about)

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