如何為包裝器指令編寫 zsh 補全函數

如何為包裝器指令編寫 zsh 補全函數

我正在嘗試為名為 myssh 的 SSH 自訂包裝器編寫完成函數。 myssh 採用以下任一形式的命令列參數:

myssh [myssh options] [ssh args]

或者

myssh [myssh options] -- [ssh options] [ssh args]

如何為 myssh 特定選項提供補全,同時重複使用 ssh 的現有補全?

編輯:我也想使用該_gnu_generic功能這裡提到用於完成 myssh 選項。

答案1

在更一般的情況下,@Franklin\ Yu 的評論不足以滿足您的需要,您可以製定相應的完成命令。以命令為例flux。這個指令以及許多類似的指令相當挑剔,並且期望完成指令的第一個參數是原始指令的名稱,因此會失敗:

$ compdef myflux=flux
$ myflux<tab>
l2advertisement.yaml  pool.yaml # <--- not expected

myflux引入一個輔助命令來替換變數中的第一個命令$words可以解決此問題:

_myflux() {
  words="flux ${words[@]:1}"     # replace myflux with flux, in `words` array
  _flux                           # call original completion command which expects a words array beginning with `flux`
}

$ compdef _myflux myflux

$ myflux<tab> 
bootstrap   -- Deploy Flux on a cluster the GitOps way.
build       -- Build a flux resource
check ...
...
# the above *is* expected.

有時您會用子命令包裝原始命令,例如flux get source

數組發生突變的行將$words變為:

  words="flux get source ${words[@]:1}"

相關內容