0

我正在尝试为 zsh(或 bash)编写一个自定义自动完成脚本,该脚本具有以斜杠开头的选项。

例如:MyCommand /foo=bar.txt /yolo=test /final=4

我一直在尝试使用 zsh 帮助_arguments程序,但它没有用:

#compdef MyCommand 

_MyCommand()
{
  local curcontext="$curcontext" state line
  typeset -A opt_args

  _arguments \
    '/foo=:foo:_files'
}

_MyCommand "$@"

但是当我用它替换/--时效果很好。

我怎样才能做到这一点?

4

1 回答 1

1

你可以这样做_regex_arguments

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 "$@"

_regex_arguments你可以在这里 阅读更多关于http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions 或在这里:https://github.com/vapniks/zsh-completions/blob/master/ zsh-completions-howto.org

这里要注意的重要一点是,选项名称的模式匹配末尾没有空字符 (\0)。

于 2016-10-26T17:51:16.343 回答