Add bash completion support for flags
This commit is contained in:
parent
693af58b4d
commit
58a072d573
@ -6,7 +6,14 @@ _cli_bash_autocomplete() {
|
|||||||
local cur opts base
|
local cur opts base
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||||
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
|
if [[ $cur == -* ]]; then
|
||||||
|
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
|
||||||
|
else
|
||||||
|
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
|
||||||
|
fi
|
||||||
|
if [[ "$opts1" == "$cur1" ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
79
help.go
79
help.go
@ -4,9 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AppHelpTemplate is the text template for the Default help topic.
|
// AppHelpTemplate is the text template for the Default help topic.
|
||||||
@ -152,19 +154,80 @@ func ShowAppHelp(c *Context) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var shortFlagRegex = regexp.MustCompile(`^-`)
|
||||||
|
|
||||||
// DefaultAppComplete prints the list of subcommands as the default app completion method
|
// DefaultAppComplete prints the list of subcommands as the default app completion method
|
||||||
func DefaultAppComplete(c *Context) {
|
func DefaultAppComplete(c *Context) {
|
||||||
for _, command := range c.App.Commands {
|
DefaultAppCompleteWithFlags(nil)(c)
|
||||||
if command.Hidden {
|
}
|
||||||
continue
|
|
||||||
|
func DefaultAppCompleteWithFlags(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, "-") {
|
||||||
|
lastArg = shortFlagRegex.ReplaceAllString(lastArg, "")
|
||||||
|
lastArg = shortFlagRegex.ReplaceAllString(lastArg, "")
|
||||||
|
for _, flag := range c.App.Flags {
|
||||||
|
for _, name := range strings.Split(flag.GetName(), ",") {
|
||||||
|
name = strings.Trim(name, " ")
|
||||||
|
if strings.HasPrefix(name, lastArg) && lastArg != name {
|
||||||
|
count := utf8.RuneCountInString(name)
|
||||||
|
if count > 2 {
|
||||||
|
count = 2
|
||||||
|
}
|
||||||
|
fmt.Fprintf(c.App.Writer, "%s%s\n", strings.Repeat("-", count), name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if cmd != nil {
|
||||||
|
for _, flag := range cmd.Flags {
|
||||||
|
for _, name := range strings.Split(flag.GetName(), ",") {
|
||||||
|
name = strings.Trim(name, " ")
|
||||||
|
if strings.HasPrefix(name, lastArg) && lastArg != name {
|
||||||
|
count := utf8.RuneCountInString(name)
|
||||||
|
if count > 2 {
|
||||||
|
count = 2
|
||||||
|
}
|
||||||
|
fmt.Fprintf(c.App.Writer, "%s%s\n", strings.Repeat("-", count), name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if os.Getenv("_CLI_ZSH_AUTOCOMPLETE_HACK") == "1" {
|
if cmd != nil {
|
||||||
for _, name := range command.Names() {
|
for _, command := range cmd.Subcommands {
|
||||||
fmt.Fprintf(c.App.Writer, "%s:%s\n", name, command.Usage)
|
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)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, name := range command.Names() {
|
||||||
|
if name != "h" {
|
||||||
|
fmt.Fprintf(c.App.Writer, "%s\n", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, name := range command.Names() {
|
for _, command := range c.App.Commands {
|
||||||
fmt.Fprintf(c.App.Writer, "%s\n", name)
|
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)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, name := range command.Names() {
|
||||||
|
fmt.Fprintf(c.App.Writer, "%s\n", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user