-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
The default ShellCompDirective can be configured #2221
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,6 +304,29 @@ $ helm status --output [tab][tab] | |
json table yaml | ||
``` | ||
|
||
#### Change the default ShellCompDirective | ||
|
||
If Cobra cannot determine a special `ShellCompDirective` during flag parsing, | ||
it will return `ShellCompDirectiveDefault`, which will invoke the shell's filename completion. | ||
This is handy for flags that accept filenames, as they do not require a `FlagCompletionFunc`. | ||
|
||
For other flags where no meaningful completion can be provided, this requires extra work: | ||
You have to register a `FlagCompletionFunc` just to get rid of file completion. | ||
|
||
If you find yourself registering lots of handlers like | ||
|
||
```go | ||
cmd.RegisterFlagCompletionFunc("flag-name", cobra.NoFileCompletions) | ||
``` | ||
|
||
you can change the default `ShellCompDirective` to `ShellCompDirectiveNoFileComp`: | ||
|
||
```go | ||
cmd.CompletionOptions.DefaultShellCompDirective = ShellCompDirectiveNoFileComp | ||
``` | ||
|
||
Keep in mind that from now on you have to register handlers for every filename flag. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do think of the slightly reworked: #### Change the default ShellCompDirective When no completion function is registered for a leaf command or for a flag, Cobra will automatically use `ShellCompDirectiveDefault`, which will invoke the shell's filename completion. This implies that when file completion does not apply to a leaf command or to a flag (the command or flag does not operate on a filename), turning off file completion requires you to register a completion function for that command/flag. For example: ```go cmd.RegisterFlagCompletionFunc("flag-name", cobra.NoFileCompletions) ``` If you find that there are more situations where file completion should be turned off than when it is applicable, you can change the default `ShellCompDirective` to `ShellCompDirectiveNoFileComp`: ```go rootCmd.CompletionOptions.DefaultShellCompDirective = ShellCompDirectiveNoFileComp ``` If doing so, keep in mind that you should instead register a completion function for leaf commands or flags where file completion is applicable. For example: ```go cmd.RegisterFlagCompletionFunc("flag-name", cobra.FixedCompletions(nil, ShellCompDirectiveDefault)) ``` There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is much better than my version, thank you very much. |
||
#### Debugging | ||
|
||
You can also easily debug your Go completion code for flags: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code then rely on the fact the default value of this variable is 0 and then matches ShellCompDirectiveDefault
It's a bit tricky and invisible, I'm not fan of this.
Could you make it more explicit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, PTAL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed but the explicit code just feels "wrong" to me. I mean:
when I read the if statement, I immediately wonder why we have this effectively non-useful if statement.
Furthermore, if we were to change the first line and use a different directive, it would require to change the if statement, which seems unnecessarily brittle.
Finally, looking at the if statement, one still wonders what happens when the new option is not set by the program and will need to realize it is set by the compiler.
How going back to the original one-line but adding a comment above it:
// If not explicitly set by the program, the default directive will be [ShellCompDirectiveDefault]