Implement global options in zsh implementation

This commit is contained in:
Evan Fiordeliso 2023-11-13 19:06:59 -05:00
parent ba90879ff0
commit 26bee58adc
1 changed files with 17 additions and 2 deletions

View File

@ -15,7 +15,6 @@ func WriteZshCompletions(out io.Writer, rootCmd *Command) error {
} }
// TODO: Fix indentation and other spacing // TODO: Fix indentation and other spacing
// TODO: Implement global options
var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(` var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(`
{{- $rootCmd := .RootCmd -}} {{- $rootCmd := .RootCmd -}}
{{- $progName := $rootCmd.Name -}} {{- $progName := $rootCmd.Name -}}
@ -39,9 +38,13 @@ var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(`
_arguments _arguments
{{- end -}} {{- end -}}
{{- range .Cmd.Opts -}} {{- range .Cmd.Opts -}}
{{ " " }}\ {{- if ne .Name ""}} \
'--{{ .Name }}[{{ .Description }}]'
{{- end }}
{{- if ne .ShortName ""}} \
'-{{ .ShortName }}[{{ .Description }}]' '-{{ .ShortName }}[{{ .Description }}]'
{{- end }} {{- end }}
{{- end }}
{{- if gt (len .Cmd.Subcommands) 0 -}} {{- if gt (len .Cmd.Subcommands) 0 -}}
{{ " " }}\ {{ " " }}\
'1: :{_describe 'command' {{ $varPrefix }}commands}' \ '1: :{_describe 'command' {{ $varPrefix }}commands}' \
@ -64,6 +67,18 @@ var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(`
function _{{ $varName }} { function _{{ $varName }} {
{{ template "cmd" (map "Cmd" .RootCmd) -}} {{ 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 }} compdef _{{ $varName }} {{ $progName }}