Python:自制制表符补全帮助

发布于 2024-12-01 00:17:21 字数 578 浏览 0 评论 0原文

我需要一些帮助。我完全被难住了。我正在尝试为自定义 shell 样式程序编写自己的制表符完成模块(也完成参数)我可以使用制表符完成命令,但参数给我带来了麻烦。有人可以写一个完成模块的示例选项卡供我查看吗?
可用命令: ['显示','退出','清除','船舶'] 对于命令 show,第一个参数: ['ip','mac','选项'] 对于子参数 IP: ['external,'internal','local']

最后我希望能够去:

My Prompt > sh<tab>
show ship
My Prompt > sho<tab>
My Prompt > show <tab>
ip mac options
My Prompt > show ip <tab>
external local internal
My Prompt > show ip e<tab>
My Prompt > show ip external

等等。如果我有一个好的工作示例,我想我可以解决这个问题。我已经完全重写了制表符补全代码 5 次,但仍然无法得到它。有人可以帮忙吗?

I need some help. I am completely stumped. I'm trying to write my own tab completion module for a custom shell style program (also completing arguments) I can tab complete the commands, but the arguments are giving me trouble. Can some one write a sample tab completing module for me to look at?
Available Commands:
['show','exit','clear', 'ship']
for command show, first arg:
['ip','mac','options']
and for sub arg IP:
['external,'internal','local']

In the end I want to be able to go:

My Prompt > sh<tab>
show ship
My Prompt > sho<tab>
My Prompt > show <tab>
ip mac options
My Prompt > show ip <tab>
external local internal
My Prompt > show ip e<tab>
My Prompt > show ip external

and so on an so forth. If I just had one good working example I think I could figure this out. I've completely rewritten my tab completion code 5 times, but still I can't get it. Could some one please help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-12-08 00:17:21

最后我在第七次重写后弄清楚了。比我想要的有点混乱,但可行,可能我稍后会清理它。无论如何,这是代码:

"""defined earlier in the script:"""
COMMANDS=['show', 'exit', 'ship']
def complete(text, state, list = COMMANDS):
    for option in list:
        if option.startswith(text):
            if not state:
                return option
            else:
                state -= 1
def precomplete(text, state):
    BUFFER=readline.get_line_buffer()
    args=[None,None,None,None,None,None,None,None,None,None]
    argtemp=[]
    if BUFFER != "":
        i=-1
        while i != BUFFER.count(" "):
            if BUFFER.count(" ") >= 0:
                if BUFFER.count(" ") == 0: #1 because len() starts couting at 1
                    return complete(text, state)
                else:
                    print "Else triggered"
                    o=0
                    verb=[]
                    while complete(BUFFER.split()[0],o):
                        verb.append(complete(BUFFER.split()[0],o))
                        o=o+1
                    if len(verb) == 1:
                        verb=verb[0]
                        print verb
            if BUFFER.count(" ") >= 1:
                if  BUFFER.count(" ") == 1:
                    if verb == 'show':
                        return complete(text, state, ['mac', 'ip', 'arp'])
                else:
                    o=0
                    while complete(BUFFER[1],o,['mac', 'ip', 'arp']):
                        argtemp.append(complete(BUFFER[1],o,['mac', 'ip', 'arp']))
                        o=o+1
                    if len(argtemp) == 1:
                       argtemp==argtemp[0]
            i=i+1
    else:
        return complete(text,state)
readline.parse_and_bind("tab: complete")
readline.set_completer(precomplete)

编辑:完整代码:http://codepad.org/W3VHURUx

In the end I figured it out after the 7th rewrite. A little messier than I would like, but workable, likely I will clean this up later. Anyways, here's the code:

"""defined earlier in the script:"""
COMMANDS=['show', 'exit', 'ship']
def complete(text, state, list = COMMANDS):
    for option in list:
        if option.startswith(text):
            if not state:
                return option
            else:
                state -= 1
def precomplete(text, state):
    BUFFER=readline.get_line_buffer()
    args=[None,None,None,None,None,None,None,None,None,None]
    argtemp=[]
    if BUFFER != "":
        i=-1
        while i != BUFFER.count(" "):
            if BUFFER.count(" ") >= 0:
                if BUFFER.count(" ") == 0: #1 because len() starts couting at 1
                    return complete(text, state)
                else:
                    print "Else triggered"
                    o=0
                    verb=[]
                    while complete(BUFFER.split()[0],o):
                        verb.append(complete(BUFFER.split()[0],o))
                        o=o+1
                    if len(verb) == 1:
                        verb=verb[0]
                        print verb
            if BUFFER.count(" ") >= 1:
                if  BUFFER.count(" ") == 1:
                    if verb == 'show':
                        return complete(text, state, ['mac', 'ip', 'arp'])
                else:
                    o=0
                    while complete(BUFFER[1],o,['mac', 'ip', 'arp']):
                        argtemp.append(complete(BUFFER[1],o,['mac', 'ip', 'arp']))
                        o=o+1
                    if len(argtemp) == 1:
                       argtemp==argtemp[0]
            i=i+1
    else:
        return complete(text,state)
readline.parse_and_bind("tab: complete")
readline.set_completer(precomplete)

EDIT: Full code:http://codepad.org/W3VHURUx

作业与我同在 2024-12-08 00:17:21

您可能对以通用方式执行此操作的现有实现感兴趣。

CLI 工具包执行命令和一些参数完成情况。然而,事实证明,用 Python 很好地做到这一点并不完全可能。这主要是因为 readline 库(在编译代码中运行)当时控制着用户输入,因此您必须在调用输入方法之前预先知道所有完成情况。

You might be interested in an existing implemention that does that in a general way.

The CLI toolkit does command and some parameter completions. However, doing this well from Python turns out to be not entirely possible. This is mainly due to the readline library (running in compiled code) having control of user input at that time so you have to have all the completions known beforehand before calling into the input method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文