1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
package completion
import (
"fmt"
"io"
"os"
"strings"
"github.com/alecthomas/kong"
)
// Completion command.
type Completion struct {
Bash Bash `cmd:"" help:"Generate the autocompletion script for bash"`
Zsh Zsh `cmd:"" help:"Generate the autocompletion script for zsh"`
Fish Fish `cmd:"" help:"Generate the autocompletion script for fish"`
}
func commandName(cmd *kong.Node) string {
commandName := cmd.FullPath()
commandName = strings.ReplaceAll(commandName, " ", "_")
commandName = strings.ReplaceAll(commandName, ":", "__")
return commandName
}
func hasCommands(cmd *kong.Node) bool {
for _, c := range cmd.Children {
if !c.Hidden {
return true
}
}
return false
}
//nolint:deadcode,unused
func isArgument(cmd *kong.Node) bool {
return cmd.Type == kong.ArgumentNode
}
// writeString writes a string into a buffer, and checks if the error is not nil.
func writeString(b io.StringWriter, s string) {
if _, err := b.WriteString(s); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}
func nonCompletableFlag(flag *kong.Flag) bool {
return flag.Hidden
}
func flagPossibleValues(flag *kong.Flag) []string {
values := make([]string, 0)
for _, enum := range flag.EnumSlice() {
if strings.TrimSpace(enum) != "" {
values = append(values, enum)
}
}
return values
}
|