diff options
| author | Eyal Posener <[email protected]> | 2019-11-21 00:16:33 +0200 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2019-11-21 00:16:33 +0200 |
| commit | cf002b9a50d777b10beaf228f316c5da34f8178a (patch) | |
| tree | 0e86858f20fecaab1b6d1c3d8e2e00628cb9c7b0 /complete.go | |
| parent | b05895fa6c478784bdf66547fd6b0bf613d06f0e (diff) | |
Fix completion of help when completing flags
Diffstat (limited to 'complete.go')
| -rw-r--r-- | complete.go | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/complete.go b/complete.go index b649ee4..9ddfee2 100644 --- a/complete.go +++ b/complete.go @@ -143,7 +143,8 @@ reset: func (c completer) suggestSubCommands(prefix string) []string { if len(prefix) > 0 && prefix[0] == '-' { - return []string{helpFlag(prefix)} + help, _ := helpFlag(prefix) + return []string{help} } subs := c.SubCmdList() return suggest("", prefix, func(prefix string) []string { @@ -256,19 +257,23 @@ func (c completer) iterateStack(f func(Completer)) { func suggest(dashes, prefix string, collect func(prefix string) []string) []string { options := collect(prefix) - // If nothing was suggested, suggest all flags. - if len(options) == 0 { - prefix = "" - options = collect(prefix) + help, helpMatched := helpFlag(dashes + prefix) + // In case that something matched: + if len(options) > 0 { + if strings.HasPrefix(help, dashes+prefix) { + options = append(options, help) + } + return options } - // Add help flag if needed. - help := helpFlag(dashes + prefix) - if len(options) == 0 || strings.HasPrefix(help, dashes+prefix) { - options = append(options, help) + if helpMatched { + return []string{help} } - return options + // Nothing matched. + options = collect("") + help, _ = helpFlag(dashes) + return append(options, help) } func filterByPrefix(prefix string, options ...string) []string { @@ -321,12 +326,15 @@ func hasPrefix(s, prefix string) (string, bool) { } // helpFlag returns either "-h", "-help" or "--help". -func helpFlag(prefix string) string { +func helpFlag(prefix string) (string, bool) { if prefix == "" || prefix == "-" || prefix == "-h" { - return "-h" + return "-h", true + } + if strings.HasPrefix("--help", prefix) { + return "--help", true } if strings.HasPrefix(prefix, "--") { - return "--help" + return "--help", false } - return "-help" + return "-help", false } |
