cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Include --help and --version flag in completion

Open marckhouzam opened this issue 2 years ago • 0 comments

Fixes #1786

Thanks to @fnickels's nice work on #1707, I realized why the --help/-h and --version/-v flags where included in shell completions.

The --help, -h, --version and -v flags are normally added when the execute() function is called on a command. When doing completion we don't call execute() so we need to add these flags explicitly to the command being completed.

Also, when doing shell completion following those flags, we disable file completion.

Note that we keep track if the --help and --version flags were added by Cobra using a new flag annotation.

To test:

./prog -<TAB>
# Notice that --help and -h are listed with this PR but not without it

./prog --help <TAB>
# There are no files suggested with this PR but there are without it

marckhouzam avatar Sep 21 '22 03:09 marckhouzam