prometheus / alertmanager

Prometheus Alertmanager
https://prometheus.io
Apache License 2.0
6.64k stars 2.15k forks source link

`amtool` ZSH completion is broken #3849

Open mdschmitt opened 5 months ago

mdschmitt commented 5 months ago

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.

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
simonpasquier commented 3 months ago

hmm does it relate to https://github.com/alecthomas/kingpin/pull/339?