completion.bash 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # This file is part of the Symfony package.
  2. #
  3. # (c) Fabien Potencier <fabien@symfony.com>
  4. #
  5. # For the full copyright and license information, please view
  6. # https://symfony.com/doc/current/contributing/code/license.html
  7. _sf_{{ COMMAND_NAME }}() {
  8. # Use newline as only separator to allow space in completion values
  9. IFS=$'\n'
  10. local sf_cmd="${COMP_WORDS[0]}"
  11. # for an alias, get the real script behind it
  12. if [[ $(type -t $sf_cmd) == "alias" ]]; then
  13. sf_cmd=$(alias $sf_cmd | sed -E "s/alias $sf_cmd='(.*)'/\1/")
  14. else
  15. sf_cmd=$(type -p $sf_cmd)
  16. fi
  17. if [ ! -x "$sf_cmd" ]; then
  18. return 1
  19. fi
  20. local cur prev words cword
  21. _get_comp_words_by_ref -n := cur prev words cword
  22. local completecmd=("$sf_cmd" "_complete" "-sbash" "-c$cword" "-S{{ VERSION }}")
  23. for w in ${words[@]}; do
  24. w=$(printf -- '%b' "$w")
  25. # remove quotes from typed values
  26. quote="${w:0:1}"
  27. if [ "$quote" == \' ]; then
  28. w="${w%\'}"
  29. w="${w#\'}"
  30. elif [ "$quote" == \" ]; then
  31. w="${w%\"}"
  32. w="${w#\"}"
  33. fi
  34. # empty values are ignored
  35. if [ ! -z "$w" ]; then
  36. completecmd+=("-i$w")
  37. fi
  38. done
  39. local sfcomplete
  40. if sfcomplete=$(${completecmd[@]} 2>&1); then
  41. local quote suggestions
  42. quote=${cur:0:1}
  43. # Use single quotes by default if suggestions contains backslash (FQCN)
  44. if [ "$quote" == '' ] && [[ "$sfcomplete" =~ \\ ]]; then
  45. quote=\'
  46. fi
  47. if [ "$quote" == \' ]; then
  48. # single quotes: no additional escaping (does not accept ' in values)
  49. suggestions=$(for s in $sfcomplete; do printf $'%q%q%q\n' "$quote" "$s" "$quote"; done)
  50. elif [ "$quote" == \" ]; then
  51. # double quotes: double escaping for \ $ ` "
  52. suggestions=$(for s in $sfcomplete; do
  53. s=${s//\\/\\\\}
  54. s=${s//\$/\\\$}
  55. s=${s//\`/\\\`}
  56. s=${s//\"/\\\"}
  57. printf $'%q%q%q\n' "$quote" "$s" "$quote";
  58. done)
  59. else
  60. # no quotes: double escaping
  61. suggestions=$(for s in $sfcomplete; do printf $'%q\n' $(printf '%q' "$s"); done)
  62. fi
  63. COMPREPLY=($(IFS=$'\n' compgen -W "$suggestions" -- $(printf -- "%q" "$cur")))
  64. __ltrim_colon_completions "$cur"
  65. else
  66. if [[ "$sfcomplete" != *"Command \"_complete\" is not defined."* ]]; then
  67. >&2 echo
  68. >&2 echo $sfcomplete
  69. fi
  70. return 1
  71. fi
  72. }
  73. complete -F _sf_{{ COMMAND_NAME }} {{ COMMAND_NAME }}