[Home] Zsh logo

match-taropts

Zsh Wizard

Download match-taropts Return to Examples
  1. #!/usr/local/bin/zsh
  2. #
  3. local option
  4. local -i argpos
  5. local -i counter
  6. read -cn argpos
  7. #echo
  8. #echo $1_$2($argpos)
  9. if [[ "$1" = *[bCfFgKLNTVX]* ]]
  10. then
  11. # option with parameter present, before cursor.
  12. option=$1
  13. # check that the option with parameter is at the end of the string.
  14. if [[ $option[(i)[bCfFgKLNTVX]] = $#option ]]
  15. then
  16. # accept it, put a space after it.
  17. reply=($1$2\ )
  18. else
  19. # Very little we can do - best to disable completion.
  20. reply=( )
  21. fi
  22. return
  23. else
  24. # let $option contain the stripped-off options.
  25. option="$1${2%%[bCfFgKLNTVX]*}"
  26. #echo "{$option}"
  27. if [[ "$option" = *[Acdrtux]*[Acdrtux]* ]]
  28. then
  29. # multiple mandatory options; only one allowed - no completions.
  30. reply=( )
  31. elif [[ $argpos -gt 2 || "$option" = *[Acdrtux]* ]]
  32. then
  33. # single mandatory option already given; don't prompt for others.
  34. if [[ -n "$2" ]]
  35. then
  36. # not at end of string - only permit simple options.
  37. reply=(B G h i k l m M o O p P R s S v w W z Z)
  38. else
  39. # at end of string - permit options taking values.
  40. reply=(b C f F g K L N T V X B G h i k l m M o O p P R s S v w W z Z)
  41. fi
  42. else
  43. # prompt for mandatory option before allowing others.
  44. reply=(A c d r t u x)
  45. fi
  46. fi
  47. reply=($1${^reply}$2)
  48. #echo "($reply)"