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

Feature request: Set a default completion function #2209

Open
albers opened this issue Dec 27, 2024 · 2 comments
Open

Feature request: Set a default completion function #2209

albers opened this issue Dec 27, 2024 · 2 comments

Comments

@albers
Copy link

albers commented Dec 27, 2024

In my experience, not many flags actually require completion of files.

Some have dynamic completions, but a great deal of them take free-form string parameters where no completion can be offered.
Unfortunately, the default completion for a flag with no completion function in Cobra is file completion.
This forces me to register a cobra.NoFileCompletions for each flag of that category.

As an example, completion for docker run has 56 flags where no meaningful FlagCompletionFunc exists.
In a first attempt, I registered 56 handlers with cmd.RegisterFlagCompletionFunc("flag-name", cobra.NoFileCompletions) just to silence their file completion.
Following a hint of @thaJeztah, I replaced them with a programmatically installed default handler, see here.

I would very much welcome a solution in Cobra, where I can set a default handler on command level, something like

cmd.CompletionOptions.DefaultCompHandler = cobra.NoFileCompletions

With this feature, I'd only have to define the completion functions for flags that actually have meaningful completions.

/cc @thaJeztah

@marckhouzam
Copy link
Collaborator

I agree. I regret making file completion the default behaviour; in retrospect, no completion should have been the default. But now we are stuck with this default behaviour. Adding an option to change the default completion sounds like a good idea.

@thaJeztah
Copy link
Contributor

Thanks! And thanks for opening @albers - were you planning to work on this?

I just recalled I had that other issue with custom flags that can be set multiple times, but were not detected for completion (multiple times); I just pushed a PR with some changes In had stashed locally;

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

3 participants