修正する:

修正する:

私のカスタムスクリプトにはzsh完了ルールがありますh

#compdef h

_arguments -S : \
    '-A+:' \
    '-B+:' \
    '-C+:' \
    '-i' \
    '-w'

私のカスタムスクリプトは、上記のオプションと1つ以上のランダムな文字列を使用します。文字列が指定されていない場合は、完成が機能します。

h <TAB>
-A  -B  -C  -i  -w  

ただし、文字列を指定すると、完成はもう機能しません。

h foo -<TAB>

何もありません。

1つ以上の文字列を提供しても完成をどのように使用しますか?

修正する:

'*:string:'@Gillesが提案したように追加しました。

#compdef h

_arguments -S : \
    '*:string:' \
    '-A+:' \
    '-B+:' \
    '-C+:' \
    '-i' \
    '--color' \
    '-w'

さて完了動作がおかしいですね。私がするとき:

h some text -<TAB>

実際に使用できるオプションが一覧表示されていますが、完了するわけではありません。

私がするとき:

h some text --co<TAB>

使用可能な--colorオプションを表示しますが、オプションがあいまいでない場合でも、残りは完了しません。

ベストアンサー1

人の心の奥深いところで選択のzshcompsys(1)余地があるかもしれません。-A_arguments

-A pat Do not complete options after the first non-option argument
       on the line. pat is a pattern matching all strings which are
       not to be taken as arguments. For example, to make _arguments
       stop completing options after the first normal argument, but
       ignoring all strings starting with a hyphen even if they are
       not described by one of the optspecs, the form is `-A "-*"'.

拒否は役に立ちません。とにかく、パターンは*「何もパラメータとは見なされません」を意味するように見えるため、オプションはオプションではなくパラメータの後に完了する必要があります。

#compdef h
local ret=1
_arguments -A "*" -S : \
    '-A+:' \
    '-B+:' \
    '-C+:' \
    '-i' \
    '-w' \
    && ret=0
return $ret

使用法(_h完成スクリプトが次の場所にあると仮定)~/.zsh/functions

% zsh -f
lion% print $fpath
lion% fpath=( ~/.zsh/functions $fpath )
lion% autoload -U compinit
lion% compinit
lion% h -w foo -
-A  -B  -C  -i

-ZSHは利用可能なオプションを完了した直後にカーソルを使用します。完了スクリプトが開始されたことを確認します(後ろに次のコマンドで、Tab キーを押します。

lion% which _h
_h () {
        local ret=1
        _arguments -A "*" -S : '-A+:' '-B+:' '-C+:' '-i' '-w' && ret=0
        return $ret
}

おすすめ記事