如何用readline获取完整函数的调用密钥?

2024-04-27 01:08:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的编码,并一直在工作的思科操作系统风格的命令行界面与自动完成功能。似乎python的内置模块readline是我可行的选择。 我打算按一下“TAB”、“space”和“?”要实现完全性,行为略有不同,但只支持绑定到一个完整的函数,我不确定是否有方法获取哪个键刚刚调用了这个函数。或者我应该为此寻找其他选择? 任何提示都将不胜感激!你知道吗


Tags: 模块方法函数命令行功能编码readline界面
1条回答
网友
1楼 · 发布于 2024-04-27 01:08:43

在readline 6.2.4.1的基础上,我在readline.c中添加了一个新函数,将变量rl\u completion\u invoking\u key的值传递给python,并生成了自己的函数读线.so. 然后我可以根据complete()函数中的调用键来决定不同的行为。你知道吗

readline.c:
static PyObject *
get_completion_invoking_key(PyObject *self, PyObject *noarg)
{
    return PyInt_FromLong(rl_completion_invoking_key);
}

PyDoc_STRVAR(doc_get_completion_invoking_key,
"get_completion_invoking_key() -> int\n\
Get the invoking key of completion being attempted.");

static struct PyMethodDef readline_methods[] =
{
...
{"get_completion_invoking_key", get_completion_invoking_key,
METH_NOARGS, doc_get_completion_invoking_key},
...
}

in my own code:
readline.parse_and_bind("tab: complete")    # invoking_key = 9
readline.parse_and_bind("space: complete")  # invoking_key = 32
readline.parse_and_bind("?: complete")      # invoking_key = 63

def complete(self, text, state):
    invoking_key = readline.get_completion_invoking_key()

相关问题 更多 >