completion.bash 3.0 KB

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