Commit Graph

54 Commits

Author SHA1 Message Date
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
Adam Bozanich fe79245d1a Bash completion for names with ':' character. 2015-12-09 20:57:45 -08:00
Anthony Fok 0e4c02d9cb Restore bash completion behaviour for bash 4.x
In Pull Request #178, the use of `builtin compopt` as a test condition
is inappropriate.  Use `[[ $(type -t compopt) = "builtin" ]]` instead.

Also clean up formatting of the resulting bash completion script.
2015-11-07 04:39:28 -07:00
Fabiano Franz 7290a944e4 Allow marking persistent flags as required or filename 2015-11-06 08:43:09 -05:00
Fabiano Franz dab963a797 Persistent flags should also be used in completions 2015-11-06 08:43:09 -05:00
Eric Paris 2e6a428921 Merge pull request #179 from eparis/init_comp
Check for _init_completion not_init_completionS
2015-11-03 15:41:40 -05:00
Matt McCoy 428babeb31 Modifying bash completions so they can run on OSX 2015-11-03 15:21:24 -05:00
Eric Paris 58e38f95da Check for _init_completion not_init_completionS 2015-11-03 15:09:37 -05:00
Eric Paris 3eac1434ef Update IsAvailableCommand checks
This slightly changes IsAvailableCommand in that a non-runnable command
with a runnable subcommand is now 'Available'

We also use IsAvailableCommand in the rest of the codebase instead of
half kinda sorta doing it incorrectly other places.
2015-09-08 22:48:12 -04:00
Eric Paris 32e87e3c8e Enable bash completions on Macs
If they installed bash-completions from homebrew, this should make our
completions work.
2015-09-03 17:00:18 -04:00
Eric Paris 9e7788657f Auto generation of a man page 2015-08-21 01:10:13 -07:00
Anthony Fok 1e6fdf608f Add new BashCompSubdirsInDir annotation
This first `cd` to a specified directory, then
lists the subdirectories therein with `_filedir -d`.

This can be used by e.g. `hugo --theme=[Tab][Tab]`, which would
give a list of subdirectories under the `themes` directory.
2015-08-09 13:30:58 -06:00
Jordan Liggitt 6119fc993e Simplify setting file/extension annotations on a flag 2015-06-22 15:16:00 -04:00
Eric Paris dff410ab56 make filename extension handling a bash function
We were trying to call a bash function with bash stuff like @ () from a
variable.  Stop that.  Just call a function with an arg from a variable
instead of trying to pass around the bash.

Should fix https://github.com/spf13/cobra/pull/103
2015-05-04 18:41:53 -04:00
Eric Paris f4b3401f9e Clean up minor bash complaints from shellcheck.net 2015-05-04 14:40:27 -04:00