django_bash_completion 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # #########################################################################
  2. # This bash script adds tab-completion feature to django-admin.py and
  3. # manage.py.
  4. #
  5. # Testing it out without installing
  6. # =================================
  7. #
  8. # To test out the completion without "installing" this, just run this file
  9. # directly, like so:
  10. #
  11. # . ~/path/to/django_bash_completion
  12. #
  13. # Note: There's a dot ('.') at the beginning of that command.
  14. #
  15. # After you do that, tab completion will immediately be made available in your
  16. # current Bash shell. But it won't be available next time you log in.
  17. #
  18. # Installing
  19. # ==========
  20. #
  21. # To install this, point to this file from your .bash_profile, like so:
  22. #
  23. # . ~/path/to/django_bash_completion
  24. #
  25. # Do the same in your .bashrc if .bashrc doesn't invoke .bash_profile.
  26. #
  27. # Settings will take effect the next time you log in.
  28. #
  29. # Uninstalling
  30. # ============
  31. #
  32. # To uninstall, just remove the line from your .bash_profile and .bashrc.
  33. _django_completion()
  34. {
  35. local cur prev opts actions action_shell_opts action_runfcgi_opts
  36. COMPREPLY=()
  37. cur="${COMP_WORDS[COMP_CWORD]}"
  38. prev="${COMP_WORDS[COMP_CWORD-1]}"
  39. # Standalone options
  40. opts="--help --settings --pythonpath --version"
  41. # Actions
  42. actions="adminindex createcachetable dbshell diffsettings \
  43. inspectdb install reset runfcgi runserver \
  44. shell sql sqlall sqlclear sqlindexes sqlinitialdata \
  45. sqlreset sqlsequencereset startapp startproject \
  46. syncdb validate"
  47. # Action's options
  48. action_shell_opts="--plain"
  49. action_runfcgi_opts="host port socket method maxspare minspare maxchildren daemonize pidfile workdir"
  50. if [[ # django-admin.py, ./manage, manage.py
  51. ( ${COMP_CWORD} -eq 1 &&
  52. ( ${COMP_WORDS[0]} == django-admin.py ||
  53. ${COMP_WORDS[0]} == ./manage.py ||
  54. ${COMP_WORDS[0]} == manage.py ) )
  55. ||
  56. # python manage.py, /some/path/python manage.py (if manage.py exists)
  57. ( ${COMP_CWORD} -eq 2 &&
  58. ( $( basename ${COMP_WORDS[0]} ) == python ) &&
  59. ( $( basename ${COMP_WORDS[1]} ) == manage.py) &&
  60. ( -r ${COMP_WORDS[1]} ) ) ]] ; then
  61. case ${cur} in
  62. -*)
  63. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  64. action=$COMPREPLY
  65. return 0
  66. ;;
  67. *)
  68. COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
  69. action=$COMPREPLY
  70. return 0
  71. ;;
  72. esac
  73. else
  74. case ${prev} in
  75. adminindex|install|reset| \
  76. sql|sqlall|sqlclear|sqlindexes| \
  77. sqlinitialdata|sqlreset|sqlsequencereset)
  78. # App completion isn't yet implemented, but here's where that
  79. # would go.
  80. # COMPREPLY=( $(compgen -W "auth core" -- ${cur}) )
  81. COMPREPLY=()
  82. return 0
  83. ;;
  84. createcachetable|dbshell|diffsettings| \
  85. inspectdb|runserver|startapp|startproject|syncdb| \
  86. validate)
  87. COMPREPLY=()
  88. return 0
  89. ;;
  90. shell)
  91. COMPREPLY=( $(compgen -W "$action_shell_opts" -- ${cur}) )
  92. return 0
  93. ;;
  94. runfcgi)
  95. COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
  96. return 0
  97. ;;
  98. host*|port*|socket*|method*|maxspare*|minspare*|maxchildren*|daemonize*|pidfile*|workdir*)
  99. if [ "$action" == "runfcgi" ] ; then
  100. COMPREPLY=( $(compgen -W "$action_runfcgi_opts" -- ${cur}) )
  101. return 0
  102. fi
  103. return 0
  104. ;;
  105. *)
  106. #COMPREPLY=( $(compgen -W "auth core" -- ${cur}) )
  107. COMPREPLY=()
  108. return 0
  109. ;;
  110. esac
  111. fi
  112. }
  113. complete -F _django_completion django-admin.py manage.py