Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] Disable flags suggestion after -- argument #11

Open
sagan opened this issue Aug 3, 2023 · 0 comments
Open

[Improvement] Disable flags suggestion after -- argument #11

sagan opened this issue Aug 3, 2023 · 0 comments

Comments

@sagan
Copy link

sagan commented Aug 3, 2023

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)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant