Add additional test for log flag completion and comments
This commit is contained in:
parent
c3f51bed6f
commit
2be2bc755e
29
app_test.go
29
app_test.go
@ -274,6 +274,35 @@ func ExampleApp_Run_bashComplete_withLongFlag() {
|
||||
// --some-flag
|
||||
// --similar-flag
|
||||
}
|
||||
func ExampleApp_Run_bashComplete_withMultipleLongFlag() {
|
||||
os.Args = []string{"greet", "--st", "--generate-bash-completion"}
|
||||
|
||||
app := NewApp()
|
||||
app.Name = "greet"
|
||||
app.EnableBashCompletion = true
|
||||
app.Flags = []Flag{
|
||||
IntFlag{
|
||||
Name: "int-flag,i",
|
||||
},
|
||||
StringFlag{
|
||||
Name: "string,s",
|
||||
},
|
||||
StringFlag{
|
||||
Name: "string-flag-2",
|
||||
},
|
||||
StringFlag{
|
||||
Name: "similar-flag",
|
||||
},
|
||||
StringFlag{
|
||||
Name: "some-flag",
|
||||
},
|
||||
}
|
||||
|
||||
app.Run(os.Args)
|
||||
// Output:
|
||||
// --string
|
||||
// --string-flag-2
|
||||
}
|
||||
|
||||
func ExampleApp_Run_bashComplete() {
|
||||
// set args for examples sake
|
||||
|
14
help.go
14
help.go
@ -199,23 +199,27 @@ func cliArgContains(flagName string) bool {
|
||||
}
|
||||
|
||||
func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) {
|
||||
cur := shortFlagRegex.ReplaceAllString(lastArg, "")
|
||||
cur = shortFlagRegex.ReplaceAllString(cur, "")
|
||||
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.Trim(name, " ")
|
||||
name = strings.TrimSpace(name)
|
||||
// this will get total count utf8 letters in flag name
|
||||
count := utf8.RuneCountInString(name)
|
||||
if count > 2 {
|
||||
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
|
||||
}
|
||||
flagCompletion := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user