I'm trying to write a custom auto-complete script for zsh (or bash) for a command that has options starting with a slash.
For instance: MyCommand /foo=bar.txt /yolo=test /final=4
I've been trying to use the zsh helper _arguments
but it did not work:
#compdef MyCommand
_MyCommand()
{
local curcontext="$curcontext" state line
typeset -A opt_args
_arguments \
'/foo=:foo:_files'
}
_MyCommand "$@"
But when I replace the /
with --
it works well.
How can I achieve this?
You can do that using _regex_arguments
like this:
matchany=/$'[^\0]##\0'/
_regex_arguments _mycommand "$matchany" \( /$'/foo='/ ':option:option:(/foo=)' "$matchany" ':file:filename:_files' \| /$'/yolo='/ ':option:option:(/yolo=)' "$matchany" ':optarg:optarg:(test this)' \| /$'/final='/ ':option:option:(/final=)' /$'[0-9]##\0'/ ':number:number:' \) \#
_mycommand "$@"
You can read more about _regex_arguments
here
http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions
or here: https://github.com/vapniks/zsh-completions/blob/master/zsh-completions-howto.org
The important thing to note here is that there is no null char (\0) at the end of the pattern matches for the option names.