# 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 ]] && setglobal admindir = "/var/lib/$i" && break } for (( i=1; i < cword; i++ )); do if [[ "${words[i]}" == --admindir ]]; then admindir=${words[i+1]} break fi done setglobal COMPREPLY = ''( $( compgen -W '$( command ls $admindir )' -- "$cur" ) ) } proc _update_alternatives { local cur prev words cword _init_completion || return match $prev { with --altdir|--admindir _filedir -d return 0 with --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 match $mode { with --install match $args { with 1|3 _filedir with 2 _installed_alternatives with 4 # priority - no completions with * match $shExpr('args % 4') { with 0|2 _filedir with 1 setglobal COMPREPLY = ''( $( compgen -W '--slave' -- "$cur" ) ) with 3 _installed_alternatives } } with --remove|--set match $args { with 1 _installed_alternatives with 2 _filedir } with --auto|--remove-all|--display|--config _installed_alternatives with * setglobal 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