Add bash implementation of autocomplete and add names helper for command and option sets
This commit is contained in:
parent
31c4264e4f
commit
2c26eda5e2
15
command.go
15
command.go
|
@ -71,6 +71,13 @@ func (c *Command) Execute(args []string) {
|
|||
if c.isRoot {
|
||||
args = args[1:]
|
||||
}
|
||||
if len(args) > 0 {
|
||||
sc, ok := c.Subcommands.Get(args[0])
|
||||
if ok {
|
||||
sc.Execute(args[1:])
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
parser := opt.NewParser(args, c.Opts, false)
|
||||
restArgs, err := parser.Parse()
|
||||
|
@ -80,14 +87,6 @@ func (c *Command) Execute(args []string) {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(restArgs) > 0 {
|
||||
sc, ok := c.Subcommands.Get(restArgs[0])
|
||||
if ok {
|
||||
sc.Execute(restArgs[1:])
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
helpOpt, ok := opt.Globals().GetBool("help")
|
||||
if ok && helpOpt.Value() {
|
||||
c.ShowHelp()
|
||||
|
|
|
@ -9,11 +9,46 @@ import (
|
|||
|
||||
func WriteBashCompletions(out io.Writer, rootCmd *Command) error {
|
||||
return bashTpl.Execute(out, map[string]any{
|
||||
"rootCmd": rootCmd,
|
||||
"globalOpts": opt.Globals(),
|
||||
"RootCmd": rootCmd,
|
||||
"GlobalOpts": opt.Globals(),
|
||||
})
|
||||
}
|
||||
|
||||
var bashTpl = template.Must(template.New("bash").Parse(`
|
||||
|
||||
var bashTpl = template.Must(template.New("bash").Funcs(tplFuncs).Parse(`
|
||||
{{- $rootCmd := .RootCmd -}}
|
||||
{{- $progName := $rootCmd.Name -}}
|
||||
{{- $varName := under $rootCmd.Name -}}
|
||||
|
||||
{{- define "cmd" }}
|
||||
{{ $currIdx := .Index }}
|
||||
{{ $nextIdx := inc .Index }}
|
||||
"{{ .Cmd.Name }}")
|
||||
case ${COMP_WORDS[{{ $currIdx }}]} in
|
||||
{{ range .Cmd.Subcommands -}}
|
||||
{{ template "cmd" (map "Cmd" . "Index" $nextIdx) -}}
|
||||
{{ end }}
|
||||
*)
|
||||
COMPREPLY=($(compgen -W "{{ join .Cmd.Subcommands.Names " " }} {{ join .Cmd.Opts.Names " " }}" -- $curr))
|
||||
esac
|
||||
;;
|
||||
{{ end -}}
|
||||
|
||||
_{{$varName}}_completions()
|
||||
{
|
||||
local curr prev
|
||||
COMPREPLY=()
|
||||
curr=${COMP_WORDS[COMP_CWORD]}
|
||||
prev=${COMP_WORDS[COMP_CWORD-1]}
|
||||
|
||||
case ${COMP_WORDS[1]} in
|
||||
{{ range .RootCmd.Subcommands -}}
|
||||
{{ template "cmd" (map "Cmd" . "Index" 2) -}}
|
||||
{{ end }}
|
||||
|
||||
*)
|
||||
COMPREPLY=($(compgen -W "{{ join .RootCmd.Subcommands.Names " " }} {{ join .RootCmd.Opts.Names " " }}" -- $curr))
|
||||
esac
|
||||
}
|
||||
|
||||
complete -F _{{$varName}}_completions {{$progName}}
|
||||
`))
|
||||
|
|
|
@ -19,7 +19,7 @@ var fishTpl = template.Must(template.New("fish").Funcs(tplFuncs).Parse(`
|
|||
{{- $rootCmd := .RootCmd -}}
|
||||
{{- $progName := $rootCmd.Name -}}
|
||||
{{- $varName := under $rootCmd.Name -}}
|
||||
set -l commands {{- range $rootCmd.Subcommands }} {{ .Name }}{{ end }}
|
||||
set -l commands {{ join $rootCmd.Subcommands.Names " " }}
|
||||
|
||||
function __fish_{{ $varName }}_needs_command
|
||||
set -l cmd (commandline -opc)
|
||||
|
@ -75,7 +75,7 @@ complete -f -c {{ $progName }} -n "__fish_{{ $varName }}_using_command {{.Cmd.Pa
|
|||
{{ end -}}
|
||||
|
||||
{{ if gt (len .Cmd.Subcommands) 0 }}
|
||||
set -l {{ $varPrefix }}commands {{- range .Cmd.Subcommands }} {{ .Name }}{{ end -}}
|
||||
set -l {{ $varPrefix }}commands {{ join .Cmd.Subcommands.Names " " }}
|
||||
{{ $cmdName := .Cmd.Name }}
|
||||
{{- range .Cmd.Subcommands }}
|
||||
{{- template "cmd" (map "Cmd" . "ProgName" $progName "VarName" $varName) -}}
|
||||
|
|
10
opt/set.go
10
opt/set.go
|
@ -33,6 +33,16 @@ func (s Set) Get(name string) (Option, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
func (s Set) Names() []string {
|
||||
names := []string{}
|
||||
for _, o := range s {
|
||||
names = append(names, "--"+o.Name())
|
||||
names = append(names, "-"+o.ShortName())
|
||||
}
|
||||
return names
|
||||
|
||||
}
|
||||
|
||||
func (s Set) GetByLongName(longName string) (Option, bool) {
|
||||
for _, o := range s {
|
||||
if o.Name() == longName {
|
||||
|
|
9
set.go
9
set.go
|
@ -35,6 +35,15 @@ func (s Set) MaxNameWidth() int {
|
|||
return max
|
||||
}
|
||||
|
||||
func (s Set) Names() []string {
|
||||
names := make([]string, 0, len(s))
|
||||
for _, c := range s {
|
||||
names = append(names, c.Name)
|
||||
names = append(names, c.Aliases...)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func (s Set) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
|
12
tpl_funcs.go
12
tpl_funcs.go
|
@ -8,9 +8,11 @@ import (
|
|||
|
||||
var tplFuncs = template.FuncMap{
|
||||
"map": tplMap,
|
||||
"cat": tplCat,
|
||||
"join": tplJoin,
|
||||
"under": tplUnder,
|
||||
"varPrefix": tplVarPrefix,
|
||||
"inc": tplInc,
|
||||
}
|
||||
|
||||
func tplMap(vals ...any) (map[string]any, error) {
|
||||
|
@ -25,10 +27,14 @@ func tplMap(vals ...any) (map[string]any, error) {
|
|||
return m, nil
|
||||
}
|
||||
|
||||
func tplJoin(strs ...string) string {
|
||||
func tplCat(strs ...string) string {
|
||||
return strings.Join(strs, "")
|
||||
}
|
||||
|
||||
func tplJoin(strs []string, sep string) string {
|
||||
return strings.Join(strs, sep)
|
||||
}
|
||||
|
||||
func tplUnder(s string) string {
|
||||
return strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(s, " ", "_"), "-", "_"))
|
||||
}
|
||||
|
@ -40,3 +46,7 @@ func tplVarPrefix(s string) string {
|
|||
|
||||
return tplUnder(s) + "_"
|
||||
}
|
||||
|
||||
func tplInc(i int) int {
|
||||
return i + 1
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue