我是编码新手,并且一直致力于具有自动完成功能的cisco os样式命令行界面。似乎python的内置模块readline将是我可行的选择。 我打算制作“TAB',' space'和'?'以稍微不同的行为实现完成,但是只支持一个完整的函数被绑定,并且我不确定是否有办法获得哪个键刚刚调用了这个函数。或者我应该为此目的寻找其他选择? 任何提示都将不胜感激!
答案 0 :(得分:0)
基于readline 6.2.4.1,我添加了一个新函数,用于将变量rl_completion_invoking_key的值传递给readline.c中的python,并生成我自己的readline.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()