As raised by @barnabasJ in Bash completion not working in ubuntu 24.04, the command
exercism completion bash
is not working properly.
I noticed that the name of the command is not present in the generated output. This is autogenerated by cobra
.
There’s a manually written script in shell/exercism_completion.bash
that works, but that can (and did) get out of sync with the actual commands and options. And I expect users won’t know about it.
I’m not a go programmer, but I think I found the issue, and I have a diff with a fix.
Before:
exercism completion bash | tail -n 8
if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start_
else
complete -o default -o nospace -F __start_
fi
# ex: ts=4 sw=4 et filetype=sh
after my fix:
./testercism completion bash | tail -n 8
./testercism
if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start_./testercism ./testercism
else
complete -o default -o nospace -F __start_./testercism ./testercism
fi
# ex: ts=4 sw=4 et filetype=sh
In the latter I’m using the custom built binary. Sourcing the output seems to work fine.
I see that it is not encouraged to open issues on github, but OTOH I noticed that commit messages tend to include the issue id so I refrained from doing that yet. In any case, the diff looks like this:
git diff --staged
diff --git a/cmd/root.go b/cmd/root.go
index 35d2663..d94d5c8 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -14,7 +14,7 @@ import (
// RootCmd represents the base command when called without any subcommands.
var RootCmd = &cobra.Command{
- Use: BinaryName,
+ Use: getCommandName(),
Short: "A friendly command-line interface to Exercism.",
Long: `A command-line interface for Exercism.
@@ -41,8 +41,12 @@ func Execute() {
}
}
+func getCommandName() string {
+ return os.Args[0]
+}
+
func init() {
- BinaryName = os.Args[0]
+ BinaryName = getCommandName()
config.SetDefaultDirName(BinaryName)
Out = os.Stdout
Err = os.Stderr
Could people with more knowledge on the topic weigh in?