# bash completion for update-alternatives -*- shell-script -*- proc _installed_alternatives { local admindir # find the admin dir for i in alternatives dpkg/alternatives rpm/alternatives { [[ -d /var/lib/$i ]] && setvar admindir = "/var/lib/$i" && break } for (( i=1; i < cword; i++ )); do if [[ "${words[i]}" == --admindir ]]; then admindir=${words[i+1]} break fi done setvar COMPREPLY = ''( $( compgen -W '$( command ls $admindir )' -- "$cur" ) ) } proc _update_alternatives { local cur prev words cword _init_completion || return case (prev) { --altdir|--admindir { _filedir -d return 0 } --help|--usage|--version { return 0 } } local mode args i # find which mode to use and how many real args used so far for (( i=1; i < cword; i++ )); do if [[ "${words[i]}" == --@(install|remove|auto|display|config|remove-all|set) ]]; then mode=${words[i]} args=$(($cword - i)) break fi done case (mode) { --install { case (args) { 1|3 { _filedir } 2 { _installed_alternatives } 4 { # priority - no completions } * { case{ 0|2 { _filedir } 1 { setvar COMPREPLY = ''( $( compgen -W '--slave' -- "$cur" ) ) } 3 { _installed_alternatives } } } } } --remove|--set { case (args) { 1 { _installed_alternatives } 2 { _filedir } } } --auto|--remove-all|--display|--config { _installed_alternatives } * { setvar COMPREPLY = ''( $( compgen -W '--verbose --quiet --help --version --altdir --admindir --install --remove --auto --display --config --set' -- "$cur" ) ) } } } && complete -F _update_alternatives update-alternatives alternatives # ex: ts=4 sw=4 et filetype=sh