complete.bash.skel 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/bash
  2. __SATURNIN_COMPLETE_CMDNAME=__MKIT_PROJ_PKGNAME__
  3. __SATURNIN_COMPLETE_M_OPTS="-D -V -d -h -v --full-debug --help --verbose --debug --version --version-semver"
  4. #shellcheck disable=SC2016
  5. __SATURNIN_COMPLETE_CODE='#!/bin/bash
  6. __'"$__SATURNIN_COMPLETE_CMDNAME"'() {
  7. local Meta=$1 # current metacommand
  8. local Partial=$2 # current unfinished word
  9. local Previous=$3 # word preceding the unfinished one
  10. local ScmdWords # words from subcommand on, if already present
  11. local Scmds=() # all subcommands
  12. local parser=__${Meta}__scparse # parse util function
  13. local asker=__${Meta}__scask # util function to ask Saturnin
  14. local comptext # compgen result text
  15. mapfile -t Scmds <<<"$("$Meta" --saturnin-get-subcommands)"
  16. COMPREPLY=()
  17. if $parser HAS; then
  18. COMP_WORDBREAKS=" "
  19. mapfile -t ScmdWords <<<"$($parser WORDS)"
  20. comptext=$(compgen -W "$($asker)" -- "$Partial")
  21. else
  22. case $Partial in
  23. -*)
  24. comptext=$(
  25. compgen -W "'"$__SATURNIN_COMPLETE_M_OPTS"'" -- "$Partial"
  26. )
  27. ;;
  28. *)
  29. comptext=$(compgen -W "${Scmds[*]}" "$Partial")
  30. esac
  31. fi
  32. case $comptext in
  33. "") COMPREPLY=() ;;
  34. *) mapfile -t COMPREPLY <<<"$comptext" ;;
  35. esac
  36. {
  37. declare -p Meta
  38. declare -p Partial
  39. declare -p Previous
  40. declare -p ScmdWords
  41. declare -p comptext
  42. declare -p COMPREPLY
  43. } | "__${Meta}__debug"
  44. }
  45. __'"$__SATURNIN_COMPLETE_CMDNAME"'__debug() {
  46. #
  47. # Send stdin to debug file, or /dev/null if debug is off
  48. #
  49. local dfile=/dev/null
  50. if test "$__SATURNIN_COMPLETE_DEBUG" == true; then
  51. dfile=/tmp/__saturnin_complete-'"$__SATURNIN_COMPLETE_CMDNAME"'.debug
  52. fi
  53. cat >"$dfile"
  54. }
  55. __'"$__SATURNIN_COMPLETE_CMDNAME"'__scask() {
  56. #
  57. # Ask and pass args from function plus all the filtered words
  58. #
  59. "$Meta" --saturnin-sccompgen "$Meta" "$Partial" "$Previous" "${ScmdWords[@]}"
  60. }
  61. __'"$__SATURNIN_COMPLETE_CMDNAME"'__scparse() {
  62. local what=$1
  63. set -- "${COMP_WORDS[@]}"
  64. test "$1" == "$Meta" || echo "__${Meta}__scparse(): completion error" >&2
  65. shift
  66. local scswgaps=" ${Scmds[*]} "
  67. while true; do
  68. test -n "$1" || return 1
  69. test "${1#-}" == "$1" || { shift; continue; }
  70. test "${scswgaps/ $1 /}" == "$scswgaps" && { shift; continue; }
  71. break
  72. done
  73. case $what in
  74. HAS) : ;;
  75. WORDS) echo "$1"
  76. shift
  77. while test $# -gt 0; do
  78. echo "$1"
  79. shift
  80. done
  81. ;;
  82. esac
  83. return 0
  84. }
  85. '
  86. eval "$__SATURNIN_COMPLETE_CODE"
  87. complete -F "__$__SATURNIN_COMPLETE_CMDNAME" "$__SATURNIN_COMPLETE_CMDNAME"