What did you do?amtool --completion-script-zsh >> ~/.zshrc
What did you expect to see?
Proper tab-completion of subcommands similar to that of Bash
What did you see instead? Under which circumstances?amtool <TAB> results in:
amtool: error: expected command but got ""
Environment
System information:
Linux 4.14.336-256.559.amzn2.x86_64 x86_64, but applies to all OSes afaik.
Alertmanager version:
amtool, version 0.27.0 (branch: HEAD, revision: 0aa3c2aad14cff039931923ab16b26b7481783b5)
build user: root@22cd11f671e9
build date: 20240228-11:51:20
go version: go1.21.7
platform: linux/amd64
tags: netgo
Prometheus version: N/A
Alertmanager configuration file: N/A
Prometheus configuration file: N/A
Logs: N/A
Solution:
The issue seems to be the way ZSH slices arrays which includes the end index. This is contrary to bash where the end index is not included. Therefore, when $CURRENT-1 is the total number of words, the last word (which is an empty string for a command line ending with space) is included. This causes amtool --completion-bash to receive an extra empty argument.
This is fixed by subtracting 2 instead of 1, like so:
_amtool() {
local matches
matches=($( ${words[1]} --completion-bash "${words[@]:1:$CURRENT-2}" ))
compadd -a matches
if [[ $compstate[nmatches] -eq 0 && $words[$CURRENT] != -* ]]; then
_files
fi
}
compdef _amtool amtool
What did you do?
amtool --completion-script-zsh >> ~/.zshrc
What did you expect to see? Proper tab-completion of subcommands similar to that of Bash
What did you see instead? Under which circumstances?
amtool <TAB>
results in:amtool: error: expected command but got ""
Environment
Linux 4.14.336-256.559.amzn2.x86_64 x86_64
, but applies to all OSes afaik.Alertmanager version:
Prometheus version: N/A
Alertmanager configuration file: N/A
Prometheus configuration file: N/A
Logs: N/A
Solution:
The issue seems to be the way ZSH slices arrays which includes the end index. This is contrary to bash where the end index is not included. Therefore, when
$CURRENT-1
is the total number of words, the last word (which is an empty string for a command line ending with space) is included. This causesamtool --completion-bash
to receive an extra empty argument.This is fixed by subtracting 2 instead of 1, like so: