Commit graph

31 commits

Author SHA1 Message Date
Marc Khouzam
5a793296fb Redirect bash completion v1 to v2 when possible
We are no longer actively maintaining bash completion v1 in favor of its
more rich v2 version.  Previously, using bash completion v2 required
projects to be aware of its existence and to explicitly call
GenBashCompletionV2().

With this commit, any projects calling GenBashCompletion() will
automatically be redirected to using the v2 version.

One exception is if the project uses the legacy custom completion logic
which is not supported in v2.  We can detect that by looking for the
use of the field `BashCompletionFunction` on the root command.

Note that descriptions are kept off when calling GenBashCompletion().
This means that to enable completion descriptions for bash, a project
must still explicitly call GenBashCompletionV2().

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
2022-11-25 15:27:50 -05:00
Unai Martinez-Corral
6d978a911e
add missing license headers (#1809) 2022-09-16 07:55:56 -04:00
Marc Khouzam
f464d6c82e
Add Active Help support (#1482) 2022-06-15 20:08:16 -04: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
Daisuke Taniwaki
ba1052d4cb Fix two word flags (#807) 2019-03-11 08:55:09 -04:00
Michael
7547e83b2d Run tests against go 1.11 and drop 1.9 (#737)
* Update the Travis and CircleCI Go versions

* Adapt to new gofmt formatting

The formatting of gofmt changed slightly in go 1.11.  The release
notes recommend to use a specific binary of gofmt.  See
https://golang.org/doc/go1.11#gofmt

This commit adapts to the new formatting applied by gofmt and changes
the configs for travis and circleci to run gofmt only with go 1.11.
2019-01-08 19:34:09 -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
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
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
Albert Nigmatzianov
65c8acb228 Improve tests 2017-11-09 07:56:43 +01:00
Edward Betts
3c0b56b677 correct spelling mistake (#524) 2017-09-01 11:16:37 -04:00
Eric Paris
d89c499964 Allow commands to explicitly state if they do, or do not take arbitrary arguments
Check that arguments are in ValidArgs

If a command defined cmd.ValidArgs check that the argument is actually
in ValidArgs and fail if it is not.
2017-07-23 09:57:27 +02:00
Albert Nigmatzianov
84f471618b Make BenchmarkBashCompletion more honest
name              old time/op    new time/op    delta
BashCompletion-4    67.4µs ± 1%    50.8µs ± 3%  -24.63%  (p=0.000 n=17+19)

name              old alloc/op   new alloc/op   delta
BashCompletion-4    29.5kB ± 0%    29.5kB ± 0%   +0.00%  (p=0.020 n=20+20)

name              old allocs/op  new allocs/op  delta
BashCompletion-4       253 ± 0%       253 ± 0%     ~     (all equal)
2017-06-05 19:32:33 +02:00
Albert Nigmatzianov
fb1f39915d Add benchmark for GenBashCompletion 2017-05-18 15:03:00 +02:00
Albert Nigmatzianov
de6b168d98 Simplify bash_completions.go
Improve test coverage from 80% to 85%.
2017-05-18 15:03:00 +02:00
Chris Broadfoot
d1371ecdb4 gofmt, govet, run those in Travis, add 1.7 2016-08-23 13:30:43 -07: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
Dr. Stefan Schimanski
defeccc04d Run shellcheck on bash completion 2016-04-02 23:16:12 +02:00
Eric Paris
60267ae24d Merge pull request #257 from sttts/sttts-noun-aliases
Add noun aliases
2016-04-02 09:29:34 -05:00
Dr. Stefan Schimanski
1a0a490d76 Remove plural handling and add aliases for nouns in completion 2016-04-02 12:39:19 +02:00
Ian Walter
a0bd6c17b3 Fixing golint warnings
* Moving final return outside of if-else
* Removing type declarations that Go can infer from values
* Cleaning up some existing comments
* Changing snake_case variables to camelCase
2016-03-31 09:53:34 -04:00
Dr. Stefan Schimanski
8092588fb8 Add custom bash flag completion 2016-03-21 08:05:54 +01:00
Adam Bozanich
fe79245d1a Bash completion for names with ':' character. 2015-12-09 20:57:45 -08:00
Fabiano Franz
7290a944e4 Allow marking persistent flags as required or filename 2015-11-06 08:43:09 -05: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
c3e48f996d Deprecated subcommands
They should still work, but shouldn't show up in help or usage output
2015-04-29 13:08:20 -04:00
Eric Paris
9b2e6822e5 Add bash autocompletion generator
Given a (potentially annotated) cobra command you can generate a bash
completion script.
2015-04-07 20:13:49 -04:00