在完成函数中查看变量的值

问题描述

我有found以下zsh完成函数

#compdef _h h

_h() { 
    local curcontext="$curcontext" state line
 
    _arguments \
        '1: :->country'\
        '*: :->city'
 
    case $state in
    country)
        _arguments '1:Countries:(France Germany Italy)'
    ;;
    *)
        case $words[2] in
        France)
            compadd "$@" Paris Lyon Marseille
        ;;
        Germany)
            compadd "$@" Berlin Munich Dresden
        ;;
        Italy)
            compadd "$@" Rome Napoli Palermo
        ;;
        *)
            _files 
        esac
    esac
}

我想查看(echo变量($curcontext$state$line$words的值,但不确定在哪里或如何将显示输出(因为仅当我按 tab 时才会触发完成功能)。

如何查看上述变量的值。

解决方法

在这里OP。我还在zsh-users GitHub page中问了这个问题。下面给出了一些附加功能的答案。

将以下行放入完成文件中。

echo "curcontext=$curcontext state=$state line=$line words=$words[@]"

如果要在另一个终端上打印调试输出,请使用:

echo "curcontext=$curcontext state=$state line=$line words=$words[@]" > device_file_path # like /dev/pts/3

在要查看调试输出的终端中输入tty,以获取其device_file_path