Commit Graph

68 Commits

Author SHA1 Message Date
Ville Skyttä eb3b6397b1
Bash completion variable leak fixes (#1352)
Fixes bash variables leaking into the parent shell without `local`
2021-02-18 08:26:03 -07:00
Unai Martinez-Corral 652c755d37
Use golangci-lint (#1044)
Use golangci-lint. Repair warnings and errors resulting from linting.
2021-02-07 17:08:50 -07:00
Luap99 50258f15eb
Complete subcommands when TraverseChildren is set (#1171)
* Complete subcommands when TraverseChildren is true in custom completion

The current custom completion logic does not complete
subcommands when a local flag is set. This is good unless
TraverseChildren is set to true where local flags
can be set on parent commands.

This commit allows subcommands to be completed
if TraverseChildren is set to true on the root cmd.

Closes #1170

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>

* Complete subcommands when TraverseChildren is true in bash completion

The current bash completion logic does not complete
subcommands when a local flag is set. There is also a bug
where subcommands are sometimes still getting completed. see: #1172

If TraverseChildren is true we should allow subcommands
to be completed even if a local flag is set.

Closes #1172

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
2020-09-09 09:34:51 -06:00
Marc Khouzam 2c5a0d300f
Extend Go completions and revamp zsh comp (#1070) (#1070)
Replace the current Zsh completion with a Zsh completion solution based
on Go completions. This allows to support custom completions (based
on Go completions), but also to standardize the behavior of completion
across all shells.

Also, add support to Go completions for the bash completion annotations:
  BashCompFilenameExt (including Command.MarkFlagFilename() family)
                       - still supported by zsh
  BashCompSubdirsInDir - now supported by zsh
  BashCompOneRequiredFlag (including Command.MarkFlagRequired() family)
                       - now supported by zsh and fish

Finally, remove the suggestin of the = form of flag completion.
The = form is supported, but it will not be suggested to avoid having
duplicated suggestions.
2020-06-29 13:52:14 -06:00
Marc Khouzam 04318720db
Add completion for help command (#1136)
* Don't exclude 'help' from bash completions

Fixes #1000.

* Provide completion for the help command

1- Show 'help' as a possible completion
2- Provide completions for the help command itself

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

Co-authored-by: Zaven Muradyan <voithos@google.com>
2020-06-16 14:49:26 -06:00
Marc Khouzam f8fdd17383
Complete command names even if ValidArgs present (#1088)
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2020-05-07 15:04:14 -06:00
Marc Khouzam a684a6d7f5
Fish completion using Go completion (#1048)
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2020-04-10 13:56:28 -06:00
Marc Khouzam b84ef40338
Rename BashCompDirectives to ShellCompDirectives (#1082)
Since the completion directives will be used for all shells, and that
these names will be consumed by users, this is a more appropriate name.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2020-04-06 11:28:44 -06:00
Marc Khouzam b80aeb17fc
Add support for custom completions in Go (#1035)
This commit allows programs using Cobra to code their custom completions
in Go instead of Bash.

The new ValidArgsFunction field is added for commands, similarly to
ValidArgs.  For flags, the new function
Command.RegisterFlagCompletionFunc() is added.

When either of the above functions is used, the bash completion script
will call the new hidden command '__complete', passing it all
command-line arguments. The '__complete' command will call
the function specified by Command.ValidArgsFunction or by
Command.RegisterFlagCompletionFunc to obtain completions from the
program itself.

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
2020-04-03 13:43:43 -06:00
Marc Khouzam bf26895664 Fix regression when calling *_custom_func (#1001)
PR #889 introduced a regression where the global variable $c is no
longer set when *custom_func is called.  This is because $c is re-used
by mistake in the read loop.

This PR simply changes the name of the variable used in the loop.

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2019-12-26 10:55:42 -07:00
rsteube 21f39ca07e bash: fix shellcheck errors (#889)
https://github.com/koalaman/shellcheck/wiki/SC2207
https://github.com/koalaman/shellcheck/wiki/SC2164
2019-07-11 12:18:47 -06:00
Haim Ashkenazi e2c45ac9eb Started working on Unified API for the various shell completions:
- Moved some general function to a more generic shell_completions file.
- Added functions to mark flag as directory completion.
- Started making the global functions docs more generic (not bash
  specific) and added compatibility matrix.
2019-06-07 10:09:50 -04:00
Daisuke Taniwaki ba1052d4cb Fix two word flags (#807) 2019-03-11 08:55:09 -04:00
Daisuke Taniwaki d2d81d9a96 Fix too many underscore for __custom_func (#794)
* Fix too many underscore for __custom_func

* Fix typo of too many leading underscores in docs
2018-11-27 08:31:06 -05:00
Paul 6fd8e29b07 Qualify custom bash func name (#730)
* Qualify custom bash func name

 - fixes issue where multiple cobra apps using custom bash completion
 would have their __custom_func collide
 - support fallback to plain __custom_func to maintain compatibility

#694

* Improve tests for bash completion __custom_func

 - check for the correct number of occurrences of function name

#694
2018-08-21 12:12:02 -04:00
Rajat Jindal 7ee208b09f support completions for command aliases (#669)
* support completions for command aliases

* try newer version of shellcheck

* initialize aliashash only when BASH_VERSION > 3
2018-04-23 08:47:20 -04:00
John McCabe 6644d46b81 Prefix bash functions with root command name (#643)
* Prefix bash functions with root command name

Prior to this commit the autocomplete bash functions were being prefixed
with the root command name, but references to those functions from
subcommands were having the subcommands prefixed instead - causing the
function lookups to fail and error out.

For example (as observed in kubernetes/kubernetes#60517):

kubectl create -f [Tab] failed with the following message: kubectl
create -f __create_handle_filename_extension_flag: command not found

in this case the function being invoked should be __kubectl_handle_filename_extension_flag

Signed-off-by: John McCabe <john@johnmccabe.net>

* Test filename extension and subdirs_in_dir for subcommands

This commit adds two regex based tests to ensure that the handle
filename extension and handle subdirs in dir functions are prefixed by
the root command when present in subcommands.

Previously they had been prefixed incorrectly with the subcommand name.

Signed-off-by: John McCabe <john@johnmccabe.net>
2018-02-27 21:38:38 -08:00
Rajat Jindal a1e4933ab7 Bash completion aliases (#638)
* alias support with bash completions

* add cmdname to rootcmdname

* remove print statement

* add documentation for bash alias
2018-02-21 09:51:53 -08:00
Eric Paris 1a618fb24b
Do not add a space after a single flag completion (#625) 2018-02-21 07:50:56 -08:00
John McCabe fd32f09af1 Fix generated bash completion for Bash 3 (OSX) (#520)
* Make preamble functions unique to command

Prior to this commit the functions in the preamble had names that didn't
vary based on the command for which the bash completion was generated.

This meant that if you had two bash completions with differences in the
preamble functions then only the last loaded function would be
available.

This commit prefixes all of these functions with the name of the command
so that multiple cobra generated completion files won't clash.

Signed-off-by: John McCabe <john@johnmccabe.net>

* Fix function names in writeFlagHandler

The references to the `__handle_filename_extension_flag` and
`__handle_subdirs_in_dir_flag` functions in `writeFlagHandler` hadn't
been updated correctly in the previous commits.

Signed-off-by: John McCabe <john@johnmccabe.net>

* Pass cmd into writeFlagHandler

This commit passes the cmd pointer into the writeFlagHandler so that the
`__handle_filename_extension_flag` and `__handle_subdirs_in_dir_flag`
functions can have the `cmd.Name()` prefixed.

* Update Bash completion tests

Prefixes the tested `__handle_filename_extension_flag` and
`__handle_subdirs_in_dir_flag` functions with the command name.
2018-02-08 13:34:46 -08:00
Jake Dodd c2bbfaa12d Add usage example for required flags (#627)
* Add usage example for required flags

* Explain new behavior in MarkFlagRequired godocs
2018-02-05 11:53:53 -08:00
Kazuki Suda 9979838ec4 Fix attemptd assignment to non-variable in bash 3 (#628)
* Fix attemptd assignment to non-variable in bash 3

flaghash variable is an associative array which is only supported in
bash > 3.

* Use -gt instead of >
2018-02-04 08:42:17 -08:00
Kazuki Suda 845c905010 Fix typo 2017-10-11 06:52:18 +02:00
Eric Paris b787445794 Use func (c *Command) consistently (#530)
It makes the docs looks better. The idea was suggested by @SamWhited
2017-09-05 13:20:51 -04:00
Lucy Davies 66da711334 __ltrim_colon_completions is not always available on macOS (#459)
* __ltrim_colon_completions is not always available on macOS, so bash-completion should check first
2017-06-05 11:18:07 -04:00
Albert Nigmatzianov de6b168d98 Simplify bash_completions.go
Improve test coverage from 80% to 85%.
2017-05-18 15:03:00 +02:00
tomerf 5deb57bbca Fixed completion of dash parameters arguments (#415) 2017-04-08 10:45:37 -04:00
Tamir Duberstein 7aeaa2cce6 Avoid storing pointer to nil (#411)
* Fix shellcheck

Before this change:

	In - line 204:
	    declare -F $next_command >/dev/null && $next_command
	               ^-- SC2086: Double quote to prevent globbing and word splitting.

	--- FAIL: TestBashCompletions (0.34s)
		bash_completions_test.go:138: shellcheck failed: exit status 1

* Avoid storing pointer to nil

Before this change, the new test fails with:

	--- FAIL: TestSetOutput (0.00s)
		command_test.go:198: expected setting output to nil to revert back to stdout, got <nil>
2017-04-02 10:14:34 -04:00
Vincent Batts c29ece4386 Go vet (#345)
* bash_completions: cleanup for go vet

The gnarly block of string in the Fprint tripped up go vet and was not
easy to read.

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>

* test: cleanup for go vet

Looks like copy'pasta and an unused variable

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2017-01-26 14:32:19 -05:00
Oleg Atamanenko 0f056af21f Added godocs to public methods. (#386)
* Added godocs to public methods.

* Fix gofmt formatting.
2017-01-24 11:30:45 -05:00
bogem 71a9c0834b Simplify condition with err 2016-08-30 21:57:12 +05:00
bogem 6e17f4e2c5 Fix typos 2016-08-20 12:04:53 +05:00
Euan Kemp 40e19b3f3b Don't display deprecated flags in bash completions 2016-08-02 15:01:33 -07:00
Euan Kemp 75daccd5b8 Don't display hidden flags in bash completions 2016-08-02 14:49:33 -07:00
Eric Paris 7bf964e5b6 Do not show subcommands in bash completion if a local flag was specified
If a user specifies a flag to a command which doesn't make sense to a
subcommand do not show subcommands as a suggestion.

This also changes things to show both 'required flags' and 'commands'
instead of only 'required flags'
2016-06-03 12:44:58 -04:00
Eric Paris ecc7ea6969 Show both commands and 'required flags' instead of only 'required flags' 2016-06-03 12:44:54 -04:00
Dr. Stefan Schimanski defeccc04d Run shellcheck on bash completion 2016-04-02 23:16:12 +02:00
Dr. Stefan Schimanski 1a0a490d76 Remove plural handling and add aliases for nouns in completion 2016-04-02 12:39:19 +02:00
Dr. Stefan Schimanski a23d903a12 Fix escaping of % in bash code strings 2016-03-22 17:44:59 +01:00
Dr. Stefan Schimanski ea9a77d312 Add support for plural nouns in bash completion 2016-03-21 08:05:54 +01:00
Dr. Stefan Schimanski 8092588fb8 Add custom bash flag completion 2016-03-21 08:05:54 +01:00
Dr. Stefan Schimanski 7d556a0974 Enable bash completion after --flag= or --flag=abc 2016-03-21 08:05:49 +01:00
Anthony Fok 65a708cee0 Allow bash-completion to work with full-path executable 2016-02-08 17:06:27 -05:00
Anthony Fok 801364aa15 Skip "bash: declare: -A: invalid option" error gracefully
The use of "declare -A flaghash" (associative array) was introduced
in PR #205, which works perfectly for Bash 4.x, but OS X insists on
shipping a very outdated Bash 3.2.x.

This patch hides the "bash: declare: -A: invalid option" error message
and allows the bash completion script to continue gracefully on
OS X, albeit without the benefit of the new feature in PR #205.

Fixes #240
2016-02-05 13:04:36 +08:00
Anthony Fok 97f831a4fd Change first line of generated bash-completion file
to conform with existing bash-completion snippets and
to suppress Lintian "script-not-executable".
2016-01-11 15:37:32 -07:00
Daniel Martí e0479ff7a7 Add error handling to GenBashCompletion
Since the switch from *bytes.Buffer to io.Writer, errors can no longer
be ignored. Also makes the reuse of GenBashCompletion in
GenBashCompletionFile without a buffer treat errors properly again.
2016-01-05 21:31:19 +01:00
Daniel Martí a5ff2f28ab Reuse GenBashCompletion in GenBashCompletionFile 2016-01-05 21:30:42 +01:00
Daniel Martí 463be1428d Replace *bytes.Buffer with io.Writer
Also adds support for generating bash completions on writers other than
just buffers.

Found via github.com/mvdan/interfacer.
2016-01-05 21:30:42 +01:00
Tamer TAS c6399cd398 Use readline's default filename completion if completion fails
If the `compspec` generates no matches it uses bash filename completion instead.
2016-01-02 08:11:35 +02:00
Kenjiro Nakayama 60b8213eba Add flaghash to bash_completion to keep flag value 2015-12-24 08:53:30 -05:00