|
|
|
@ -7,6 +7,7 @@ import (
|
|
|
|
|
"strings"
|
|
|
|
|
"text/tabwriter"
|
|
|
|
|
"text/template"
|
|
|
|
|
"unicode/utf8"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// AppHelpTemplate is the text template for the Default help topic.
|
|
|
|
@ -157,19 +158,88 @@ func ShowAppHelp(c *Context) (err error) {
|
|
|
|
|
|
|
|
|
|
// DefaultAppComplete prints the list of subcommands as the default app completion method
|
|
|
|
|
func DefaultAppComplete(c *Context) {
|
|
|
|
|
for _, command := range c.App.Commands {
|
|
|
|
|
DefaultCompleteWithFlags(nil)(c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printCommandSuggestions(commands []Command, writer io.Writer) {
|
|
|
|
|
for _, command := range commands {
|
|
|
|
|
if command.Hidden {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if os.Getenv("_CLI_ZSH_AUTOCOMPLETE_HACK") == "1" {
|
|
|
|
|
for _, name := range command.Names() {
|
|
|
|
|
fmt.Fprintf(c.App.Writer, "%s:%s\n", name, command.Usage)
|
|
|
|
|
fmt.Fprintf(writer, "%s:%s\n", name, command.Usage)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for _, name := range command.Names() {
|
|
|
|
|
fmt.Fprintf(c.App.Writer, "%s\n", name)
|
|
|
|
|
fmt.Fprintf(writer, "%s\n", name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cliArgContains(flagName string) bool {
|
|
|
|
|
for _, name := range strings.Split(flagName, ",") {
|
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
|
count := utf8.RuneCountInString(name)
|
|
|
|
|
if count > 2 {
|
|
|
|
|
count = 2
|
|
|
|
|
}
|
|
|
|
|
flag := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
|
|
|
|
|
for _, a := range os.Args {
|
|
|
|
|
if a == flag {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) {
|
|
|
|
|
cur := strings.TrimPrefix(lastArg, "-")
|
|
|
|
|
cur = strings.TrimPrefix(cur, "-")
|
|
|
|
|
for _, flag := range flags {
|
|
|
|
|
if bflag, ok := flag.(BoolFlag); ok && bflag.Hidden {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, name := range strings.Split(flag.GetName(), ",") {
|
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
|
// this will get total count utf8 letters in flag name
|
|
|
|
|
count := utf8.RuneCountInString(name)
|
|
|
|
|
if count > 2 {
|
|
|
|
|
count = 2 // resuse this count to generate single - or -- in flag completion
|
|
|
|
|
}
|
|
|
|
|
// if flag name has more than one utf8 letter and last argument in cli has -- prefix then
|
|
|
|
|
// skip flag completion for short flags example -v or -x
|
|
|
|
|
if strings.HasPrefix(lastArg, "--") && count == 1 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// match if last argument matches this flag and it is not repeated
|
|
|
|
|
if strings.HasPrefix(name, cur) && cur != name && !cliArgContains(flag.GetName()) {
|
|
|
|
|
flagCompletion := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
|
|
|
|
|
fmt.Fprintln(writer, flagCompletion)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DefaultCompleteWithFlags(cmd *Command) func(c *Context) {
|
|
|
|
|
return func(c *Context) {
|
|
|
|
|
if len(os.Args) > 2 {
|
|
|
|
|
lastArg := os.Args[len(os.Args)-2]
|
|
|
|
|
if strings.HasPrefix(lastArg, "-") {
|
|
|
|
|
printFlagSuggestions(lastArg, c.App.Flags, c.App.Writer)
|
|
|
|
|
if cmd != nil {
|
|
|
|
|
printFlagSuggestions(lastArg, cmd.Flags, c.App.Writer)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if cmd != nil {
|
|
|
|
|
printCommandSuggestions(cmd.Subcommands, c.App.Writer)
|
|
|
|
|
} else {
|
|
|
|
|
printCommandSuggestions(c.App.Commands, c.App.Writer)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -231,9 +301,14 @@ func ShowCompletions(c *Context) {
|
|
|
|
|
// ShowCommandCompletions prints the custom completions for a given command
|
|
|
|
|
func ShowCommandCompletions(ctx *Context, command string) {
|
|
|
|
|
c := ctx.App.Command(command)
|
|
|
|
|
if c != nil && c.BashComplete != nil {
|
|
|
|
|
c.BashComplete(ctx)
|
|
|
|
|
if c != nil {
|
|
|
|
|
if c.BashComplete != nil {
|
|
|
|
|
c.BashComplete(ctx)
|
|
|
|
|
} else {
|
|
|
|
|
DefaultCompleteWithFlags(c)(ctx)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printHelpCustom(out io.Writer, templ string, data interface{}, customFunc map[string]interface{}) {
|
|
|
|
|