add bash completion for the "apt" command
[ntk/apt.git] / share / bash-completions / apt
1 # Debian apt(8) completion -*- shell-script -*-
2
3 _apt()
4 {
5 local sourcesdir="/etc/apt/sources.list.d"
6 local cur prev words cword
7 _init_completion || return
8
9 # see if the user selected a command already
10 local COMMANDS=("install" "remove" "purge" "show" "list"
11 "update" "upgrade" "full-upgrade" "dist-upgrade"
12 "edit-sources" "help")
13
14 local command i
15 for (( i=0; i < ${#words[@]}-1; i++ )); do
16 if [[ ${COMMANDS[@]} =~ ${words[i]} ]]; then
17 command=${words[i]}
18 break
19 fi
20 done
21
22 # supported options per command
23 if [[ "$cur" == -* ]]; then
24 case $command in
25 install|remove|purge|upgrade|full-upgrade)
26 COMPREPLY=( $( compgen -W '--show-progress
27 --fix-broken --purge --verbose-versions --auto-remove
28 --simulate --dry-run
29 --download
30 --fix-missing
31 --fix-policy
32 --ignore-hold
33 --force-yes
34 --trivial-only
35 --reinstall --solver' -- "$cur" ) )
36 return 0
37 ;;
38 update)
39 COMPREPLY=( $( compgen -W '--list-cleanup
40 ' -- "$cur" ) )
41 return 0
42 ;;
43 list)
44 COMPREPLY=( $( compgen -W '--installed --upgradable
45 --manual-installed
46 -v --verbose
47 -a --all-versions
48 ' -- "$cur" ) )
49 return 0
50 ;;
51 show)
52 COMPREPLY=( $( compgen -W '-a --all-versions
53 ' -- "$cur" ) )
54 return 0
55 ;;
56 esac
57 fi
58
59 # specific command arguments
60 if [[ -n $command ]]; then
61 case $command in
62 remove|purge)
63 if [[ -f /etc/debian_version ]]; then
64 # Debian system
65 COMPREPLY=( $( \
66 _xfunc dpkg _comp_dpkg_installed_packages $cur ) )
67 else
68 # assume RPM based
69 _xfunc rpm _rpm_installed_packages
70 fi
71 return 0
72 ;;
73 install|show|list)
74 COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
75 2> /dev/null ) )
76 return 0
77 ;;
78 edit-sources)
79 COMPREPLY=( $( compgen -W '$( command ls $sourcesdir )' \
80 -- "$cur" ) )
81 return 0
82 ;;
83 esac
84 fi
85
86 # no command yet, show what commands we have
87 if [ "$command" = "" ]; then
88 COMPREPLY=( $( compgen -W '${COMMANDS[@]}' -- "$cur" ) )
89 fi
90
91 return 0
92 } &&
93 complete -F _apt apt
94
95 # ex: ts=4 sw=4 et filetype=sh