From 26bee58adc331ea30fb288f6a70169bae4d08e59 Mon Sep 17 00:00:00 2001 From: Evan Fiordeliso Date: Mon, 13 Nov 2023 19:06:59 -0500 Subject: [PATCH] Implement global options in zsh implementation --- completions_zsh.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/completions_zsh.go b/completions_zsh.go index 6c12903..3f0d286 100644 --- a/completions_zsh.go +++ b/completions_zsh.go @@ -15,7 +15,6 @@ func WriteZshCompletions(out io.Writer, rootCmd *Command) error { } // TODO: Fix indentation and other spacing -// TODO: Implement global options var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(` {{- $rootCmd := .RootCmd -}} {{- $progName := $rootCmd.Name -}} @@ -39,8 +38,12 @@ var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(` _arguments {{- end -}} {{- range .Cmd.Opts -}} - {{ " " }}\ + {{- if ne .Name ""}} \ + '--{{ .Name }}[{{ .Description }}]' + {{- end }} + {{- if ne .ShortName ""}} \ '-{{ .ShortName }}[{{ .Description }}]' + {{- end }} {{- end }} {{- if gt (len .Cmd.Subcommands) 0 -}} {{ " " }}\ @@ -64,6 +67,18 @@ var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(` function _{{ $varName }} { {{ template "cmd" (map "Cmd" .RootCmd) -}} + + {{- if gt (len .GlobalOpts) 0 }} + _arguments + {{- range .GlobalOpts -}} + {{- if ne .Name ""}} \ + '--{{ .Name }}[{{ .Description }}]' + {{- end }} + {{- if ne .ShortName ""}} \ + '-{{ .ShortName }}[{{ .Description }}]' + {{- end }} + {{- end }} + {{- end }} } compdef _{{ $varName }} {{ $progName }}