# ip(8) completion -*- shell-script -*- proc _iproute2_etc { setglobal COMPREPLY = '( '$( compgen -W \ "$( awk '!/#/ { print $2 }' /etc/iproute2/$1 2>/dev/null )" \ -- "$cur" ) ) } proc _ip { var cur = '', prev = '', words = '', cword = '' _init_completion || return match $prev { with -V|-Version|-rc|-rcvbuf return 0 with -f|-family setglobal COMPREPLY = '( '$( compgen -W 'inet inet6 ipx dnet link' -- "$cur" ) ) return 0 with -b|-batch _filedir return 0 with -force setglobal COMPREPLY = '( '$( compgen -W '-batch' -- "$cur" ) ) return 0 } var subcword = '', cmd = '', subcmd = '' for (( subcword=1; subcword < ${#words[@]}-1; subcword++ )); do [[ ${words[subcword]} == -b?(atch) ]] && return 0 [[ -n $cmd ]] && subcmd=${words[subcword]} && break [[ ${words[subcword]} != -* && \ ${words[subcword-1]} != -@(f?(amily)|rc?(vbuf)) ]] && \ cmd=${words[subcword]} done if [[ -z $cmd ]] { match $cur { with -* var c = '"-Version -statistics -details -resolve -family -oneline -timestamp -batch -rcvbuf'" [[ $cword -eq 1 ]] && set c = '" -force'" setglobal COMPREPLY = '( '$( compgen -W "$c" -- "$cur" ) ) return 0 with * setglobal COMPREPLY = '( '$( compgen -W "help $( ip help 2>&1 | \ sed -e '/OBJECT := /,/}/!d' \ -e 's/.*{//' -e 's/}.*//' -e 's/|//g' )" -- "$cur" ) ) return 0 } } [[ $subcmd == help ]] && return 0 match $cmd { with link match $subcmd { with add # TODO with delete match $shExpr('$cword-$subcword') { with 1 _available_interfaces with 2 setglobal COMPREPLY = '( '$( compgen -W 'type' -- "$cur" ) ) with 3 [[ $prev == type ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'vlan veth vcan dummy ifb macvlan can' -- "$cur" ) ) } with set if [[ $cword-$subcword -eq 1 ]] { _available_interfaces } else { match $prev { with arp|dynamic|multicast|allmulticast|promisc|\ trailers setglobal COMPREPLY = '( '$( compgen -W 'on off' \ -- "$cur" ) ) with txqueuelen|name|address|broadcast|mtu|netns|alias with * var c = '"arp dynamic multicast allmulticast promisc trailers txqueuelen name address broadcast mtu netns alias'" [[ $prev != @(up|down) ]] && set c = '" up down'" setglobal COMPREPLY = '( '$( compgen -W "$c" -- "$cur" ) ) } } with show if [[ $cword -eq $subcword+1 ]] { _available_interfaces setglobal COMPREPLY = '( '$( compgen -W 'dev group up' -- "$cur" ) ) } elif [[ $prev == dev ]] { _available_interfaces } elif [[ $prev == group ]] { _iproute2_etc group } with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help add delete set show' \ -- "$cur" ) ) } with addr match $subcmd { with add|change|replace if [[ $prev == dev ]] { _available_interfaces } elif [[ $prev == scope ]] { _iproute2_etc rt_scopes } else { : # TODO } with del if [[ $prev == dev ]] { _available_interfaces } elif [[ $prev == scope ]] { _iproute2_etc rt_scopes } else { : # TODO } with show|flush if [[ $cword -eq $subcword+1 ]] { _available_interfaces setglobal COMPREPLY = '( '$( compgen -W 'dev scope to label dynamic permanent tentative deprecated dadfailed temporary primary secondary up' -- "$cur" ) ) } elif [[ $prev == dev ]] { _available_interfaces } elif [[ $prev == scope ]] { _iproute2_etc rt_scopes } with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help add change replace del show flush' -- "$cur" ) ) } with addrlabel match $subcmd { with list|add|del|flush if [[ $prev == dev ]] { _available_interfaces } else { : # TODO } with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help list add del flush' \ -- "$cur" ) ) } with route match $subcmd { with list|flush if [[ $prev == proto ]] { _iproute2_etc rt_protos } else { : # TODO } with get # TODO with add|del|change|append|replace|monitor # TODO with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help list flush get add del change append replace monitor' -- "$cur" ) ) } with rule match $subcmd { with add|del # TODO with flush|show|list|lst with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help list add del flush' \ -- "$cur" ) ) } with neigh match $subcmd { with add|del|change|replace # TODO with show|flush # TODO with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help add del change replace show flush' -- "$cur" ) ) } with ntable match $subcmd { with change # TODO with show # TODO with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help change show' \ -- "$cur" ) ) } with tunnel match $subcmd { with show with add|change|del|prl|6rd # TODO with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help add change del show prl 6rd' -- "$cur" ) ) } with maddr match $subcmd { with add|del # TODO with show if [[ $cword -eq $subcword+1 || $prev == dev ]] { _available_interfaces [[ $prev != dev ]] && \ setglobal COMPREPLY = '( '$( compgen -W '${COMPREPLY[@]} dev' \ -- "$cur" ) ) } with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help add del show' \ -- "$cur" ) ) } with mroute match $subcmd { with show # TODO with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help show' -- "$cur" ) ) } with monitor match $subcmd { with all with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'help all' -- "$cur" ) ) } with xfrm match $subcmd { with state|policy|monitor # TODO with * [[ $cword -eq $subcword ]] && \ setglobal COMPREPLY = '( '$( compgen -W 'state policy monitor' \ -- "$cur" ) ) } } } && complete -F _ip ip # ex: ts=4 sw=4 et filetype=sh