/bash_completion.d/postgresql

http://github.com/brinkman83/bashrc · #! · 171 lines · 150 code · 21 blank · 0 comment · 0 complexity · 7705b24b6fa63ce573c485d7cd1e13ff MD5 · raw file

  1. # bash completion for Postgresql
  2. have psql && {
  3. _pg_databases()
  4. {
  5. return # See https://launchpad.net/bugs/164772
  6. COMPREPLY=( $( compgen -W "$( psql -l 2>/dev/null | \
  7. sed -e '1,/^-/d' -e '/^(/,$d' | \
  8. awk '{print $1}' )" -- "$cur" ) )
  9. }
  10. _pg_users()
  11. {
  12. # See https://launchpad.net/bugs/164772
  13. #COMPREPLY=( $( psql -qtc 'select usename from pg_user' template1 2>/dev/null | \
  14. # grep "^ $cur" ) )
  15. #[ ${#COMPREPLY[@]} -eq 0 ] && COMPREPLY=( $( compgen -u -- $cur ) )
  16. COMPREPLY=( $( compgen -u -- "$cur" ) )
  17. }
  18. # createdb(1) completion
  19. #
  20. _createdb()
  21. {
  22. local cur prev split=false
  23. COMPREPLY=()
  24. cur=`_get_cword`
  25. prev=${COMP_WORDS[COMP_CWORD-1]}
  26. _split_longopt && split=true
  27. case "$prev" in
  28. -h|--host)
  29. _known_hosts_real "$cur"
  30. return 0
  31. ;;
  32. -U|--username|-O|--owner)
  33. _pg_users
  34. return 0
  35. ;;
  36. -p|--port|-D|--tablespace|-E|--encoding|-T|--template)
  37. # argument required but no completions available
  38. return 0
  39. ;;
  40. --help|--version)
  41. # all other arguments are noop with these
  42. return 0
  43. ;;
  44. esac
  45. $split && return 0
  46. if [[ "$cur" == -* ]]; then
  47. COMPREPLY=( $( compgen -W '-D -T -E -h -p -U -W -e -q \
  48. --tablespace --template --encoding --host --port \
  49. --username --password --echo --quiet --help --version' -- "$cur" ))
  50. else
  51. _pg_databases
  52. fi
  53. }
  54. complete -F _createdb $default createdb
  55. # dropdb(1) completion
  56. #
  57. _dropdb()
  58. {
  59. local cur prev split=false
  60. COMPREPLY=()
  61. cur=`_get_cword`
  62. prev=${COMP_WORDS[COMP_CWORD-1]}
  63. _split_longopt && split=true
  64. case "$prev" in
  65. -h|--host)
  66. _known_hosts_real "$cur"
  67. return 0
  68. ;;
  69. -U|--username)
  70. _pg_users
  71. return 0
  72. ;;
  73. --help|--version)
  74. # all other arguments are noop with these
  75. return 0
  76. ;;
  77. esac
  78. $split && return 0
  79. if [[ "$cur" == -* ]]; then
  80. COMPREPLY=( $( compgen -W '-h -p -U -W -i -e -q \
  81. --host --port --username --password --interactive \
  82. --echo --quiet --help --version' -- "$cur" ) )
  83. else
  84. _pg_databases
  85. fi
  86. }
  87. complete -F _dropdb $default dropdb
  88. # psql(1) completion
  89. #
  90. _psql()
  91. {
  92. local cur prev split=false
  93. COMPREPLY=()
  94. cur=`_get_cword`
  95. prev=${COMP_WORDS[COMP_CWORD-1]}
  96. _split_longopt && split=true
  97. case "$prev" in
  98. -h|--host)
  99. _known_hosts_real "$cur"
  100. return 0
  101. ;;
  102. -U|--username)
  103. _pg_users
  104. return 0
  105. ;;
  106. -d|--dbname)
  107. _pg_databases
  108. return 0
  109. ;;
  110. -o|--output|-f|--file|-L|--log-file)
  111. _filedir
  112. return 0
  113. ;;
  114. -c|--command|-F|--field-separator|-p|--port|-P|--pset|\
  115. -R|--record-separator|-T|--table-attr|-v|--set|--variable)
  116. # argument required but no completions available
  117. return 0
  118. ;;
  119. -\?|--help|-V|--version)
  120. # all other arguments are noop with these
  121. return 0
  122. ;;
  123. esac
  124. $split && return 0
  125. if [[ "$cur" == -* ]]; then
  126. # return list of available options
  127. COMPREPLY=( $( compgen -W '-a --echo-all -A --no-align \
  128. -c --command -d --dbname -e --echo-queries \
  129. -E --echo-hidden -f --file -F --field-separator \
  130. -h --host -H --html -l --list -L --log-file -n \
  131. -o --output -p --port -P --pset -q --quiet \
  132. -R --record-separator -s --single-step \
  133. -S --single-line -t --tuples-only -T --table-attr \
  134. -U --username -v --set --variable -V --version \
  135. -W --password -x --expanded -X --no-psqlrc \
  136. -1 --single-transaction -? --help' -- "$cur" ) )
  137. else
  138. # return list of available databases
  139. _pg_databases
  140. fi
  141. }
  142. complete -F _psql $filenames psql
  143. }
  144. # Local variables:
  145. # mode: shell-script
  146. # sh-basic-offset: 4
  147. # sh-indent-comment: t
  148. # indent-tabs-mode: nil
  149. # End:
  150. # ex: ts=4 sw=4 et filetype=sh