* 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
* 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>
* 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.
* 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
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.
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