* 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.
Trivial addition to the first example in `README.md` that defines the
`cmd.Execute()` function that is referenced further down in the `main.go`
example.
* Add `CalledAs` method to Command (w/ tests)
The `CalledAs` method returns the name of the command or alias that
invoked the command -- as long as the command was actually invoked.
Otherwise, it returns the empty string.
The opens up possibilies for commands to behave differently based on
which alias invoked the command (in the same vein as Linux programs
which adjust their behavior based on the value of argv[0]).
* Fixed formatting
* 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 >
Make it so that, in the case that the root command is not runnable
but has subcommands, specifying a '--version' flag will still
run the "version" behavior.
Update Cobra generator to make rootCmd private rather than
exporting it. Also update examples in README to use the exported
Execute() command rather than referencing unexported rootCmd.
Fixes#556