cobra-prompt
cobra-prompt copied to clipboard
[Improvement] Disable flags suggestion after -- argument
Hi
Cobra is fully POSIX-compliant, in which the argument -- terminates all options; any following arguments are treated as non-option arguments, even if they begin with a hyphen. (See here for more)
Therefore, the flags suggestions should be disabled if user has already typed a pure "--" token, plus any space, before cursor.
I made a small change to func findSuggestions
in cobra-prompt.go and tested it should work
func findSuggestions(co *CobraPrompt, d *prompt.Document) []prompt.Suggest {
command := co.RootCmd
args := strings.Fields(d.CurrentLineBeforeCursor()) // changed from using d.CurrentLine()
// ... omitted
// replace this 2 lines
// command.LocalFlags().VisitAll(addFlags)
// command.InheritedFlags().VisitAll(addFlags)
flagsTerminated := false
args2 := args
if d.GetWordBeforeCursor() == "--" {
args2 = args[:len(args)-1]
}
for _, arg := range args2 {
if arg == "--" {
flagsTerminated = true
break
}
}
if !flagsTerminated {
command.LocalFlags().VisitAll(addFlags)
command.InheritedFlags().VisitAll(addFlags)
}
}