mirror of
https://github.com/spf13/cobra
synced 2024-11-25 15:17:17 +00:00
Commands but with the short descriptions now in zsh completion
This commit is contained in:
parent
1e58aa3361
commit
14efddf125
1 changed files with 26 additions and 9 deletions
|
@ -1,10 +1,12 @@
|
||||||
package cobra
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"io"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"github.com/spf13/pflag"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -83,8 +85,10 @@ func writeLevel(w io.Writer, root *Command, i int) {
|
||||||
|
|
||||||
for p, c := range byParent {
|
for p, c := range byParent {
|
||||||
names := names(c)
|
names := names(c)
|
||||||
fmt.Fprintf(w, " %s)\n", p)
|
//flags := flags(p)
|
||||||
fmt.Fprintf(w, " _arguments '%d: :(%s)'\n", i, strings.Join(names, " "))
|
fmt.Fprintf(w, " %s)\n", p.Name())
|
||||||
|
fmt.Fprintf(w, " _values '%s command' ", p.Name())
|
||||||
|
fmt.Fprintf(w, "'%s'\n", strings.Join(names, "' '"))
|
||||||
fmt.Fprintln(w, " ;;")
|
fmt.Fprintln(w, " ;;")
|
||||||
}
|
}
|
||||||
fmt.Fprintln(w, " *)")
|
fmt.Fprintln(w, " *)")
|
||||||
|
@ -105,14 +109,14 @@ func filterByLevel(c *Command, l int) []*Command {
|
||||||
return cs
|
return cs
|
||||||
}
|
}
|
||||||
|
|
||||||
func groupByParent(commands []*Command) map[string][]*Command {
|
func groupByParent(commands []*Command) map[*Command][]*Command {
|
||||||
m := make(map[string][]*Command)
|
m := make(map[*Command][]*Command)
|
||||||
for _, c := range commands {
|
for _, c := range commands {
|
||||||
parent := c.Parent()
|
parent := c.Parent()
|
||||||
if parent == nil {
|
if parent == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
m[parent.Name()] = append(m[parent.Name()], c)
|
m[parent] = append(m[parent], c)
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
@ -120,7 +124,20 @@ func groupByParent(commands []*Command) map[string][]*Command {
|
||||||
func names(commands []*Command) []string {
|
func names(commands []*Command) []string {
|
||||||
ns := make([]string, len(commands))
|
ns := make([]string, len(commands))
|
||||||
for i, c := range commands {
|
for i, c := range commands {
|
||||||
ns[i] = c.Name()
|
ns[i] = fmt.Sprintf("%s[%s]", c.Name(), c.Short)
|
||||||
|
}
|
||||||
|
return ns
|
||||||
|
}
|
||||||
|
|
||||||
|
func flags(command *Command) []string {
|
||||||
|
flags := command.Flags()
|
||||||
|
ns := make([]string, flags.NArg())
|
||||||
|
if flags.NArg() > 0 {
|
||||||
|
i := 0
|
||||||
|
flags.VisitAll(func(f *pflag.Flag) {
|
||||||
|
ns[i] = fmt.Sprintf("%s[%s]", f.Name, f.Usage)
|
||||||
|
i += 1
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return ns
|
return ns
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue