completion.bash 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. sf_cmd_type=$(type -t $sf_cmd)
  13. if [[ $sf_cmd_type == "alias" ]]; then
  14. sf_cmd=$(alias $sf_cmd | sed -E "s/alias $sf_cmd='(.*)'/\1/")
  15. elif [[ $sf_cmd_type == "file" ]]; then
  16. sf_cmd=$(type -p $sf_cmd)
  17. fi
  18. if [[ $sf_cmd_type != "function" && ! -x $sf_cmd ]]; then
  19. return 1
  20. fi
  21. local cur prev words cword
  22. _get_comp_words_by_ref -n := cur prev words cword
  23. local completecmd=("$sf_cmd" "_complete" "--no-interaction" "-sbash" "-c$cword" "-S{{ VERSION }}")
  24. for w in ${words[@]}; do
  25. w=$(printf -- '%b' "$w")
  26. # remove quotes from typed values
  27. quote="${w:0:1}"
  28. if [ "$quote" == \' ]; then
  29. w="${w%\'}"
  30. w="${w#\'}"
  31. elif [ "$quote" == \" ]; then
  32. w="${w%\"}"
  33. w="${w#\"}"
  34. fi
  35. # empty values are ignored
  36. if [ ! -z "$w" ]; then
  37. completecmd+=("-i$w")
  38. fi
  39. done
  40. local sfcomplete
  41. if sfcomplete=$(${completecmd[@]} 2>&1); then
  42. local quote suggestions
  43. quote=${cur:0:1}
  44. # Use single quotes by default if suggestions contains backslash (FQCN)
  45. if [ "$quote" == '' ] && [[ "$sfcomplete" =~ \\ ]]; then
  46. quote=\'
  47. fi
  48. if [ "$quote" == \' ]; then
  49. # single quotes: no additional escaping (does not accept ' in values)
  50. suggestions=$(for s in $sfcomplete; do printf $'%q%q%q\n' "$quote" "$s" "$quote"; done)
  51. elif [ "$quote" == \" ]; then
  52. # double quotes: double escaping for \ $ ` "
  53. suggestions=$(for s in $sfcomplete; do
  54. s=${s//\\/\\\\}
  55. s=${s//\$/\\\$}
  56. s=${s//\`/\\\`}
  57. s=${s//\"/\\\"}
  58. printf $'%q%q%q\n' "$quote" "$s" "$quote";
  59. done)
  60. else
  61. # no quotes: double escaping
  62. suggestions=$(for s in $sfcomplete; do printf $'%q\n' $(printf '%q' "$s"); done)
  63. fi
  64. COMPREPLY=($(IFS=$'\n' compgen -W "$suggestions" -- $(printf -- "%q" "$cur")))
  65. __ltrim_colon_completions "$cur"
  66. else
  67. if [[ "$sfcomplete" != *"Command \"_complete\" is not defined."* ]]; then
  68. >&2 echo
  69. >&2 echo $sfcomplete
  70. fi
  71. return 1
  72. fi
  73. }
  74. complete -F _sf_{{ COMMAND_NAME }} {{ COMMAND_NAME }}