I have custom script that takes:
- optional arguments in the short/long format
- one required command line argument
the short/long command line options are for example:
-r, --readonly
-m, --mount
for the one required arguments, these are actually specified in the script as case statements, ie foo
and bar
in this example:
case $1 in
foo )
:
;;
bar )
:
;;
How can I create zsh completion
for my script, so that optional arguments are completed when argument starts with -
, and required arguments are completed taken from my script case statement?
UPDATE:
this is what I have so far, inspired by the answer from @Marlon Richert
.
Suppose my custom script is called myscript.sh
and the completion rule I have created is in /usr/share/zsh/functions/Completion/Unix/_myscript.sh
:
#compdef myscript.sh
_myscript () {
local -a args
args+=(
{-r,--readonly}'[description for "readonly"]'
{-m,--mount}'[description for "mount"]'
)
_arguments $args && return
}
_myscript "$@"
My script myscript.sh
itself is located in /usr/local/bin/myscript.sh
.
So now when i have the optional arguments -r
and -m
taken care of, I need to modify my completion rules, so that for the required command line argument of my script, the items from the case statement from /usr/local/bin/myscript.sh
are offered as completions.
Also, I am not sure if the syntax of the block starting on line 6 with args+=(
in my completion script is correct. Where do I have to put the single quotes?