首页 > 解决方案 > 如何在命令行中编写选项卡功能?

问题描述

我正在尝试在键入时在 python 中构建此功能,shell> listeners然后它将更改为shell>listeners>,但我不知道如何或从哪里开始执行此操作。我试过这个,但我想这样输出shell (hello)(aa)(aaa) >>

sections = ["shell"]

def render_sections():
    return ">".join([*sections, ''])

def get_command():
    return input(render_sections()).strip()

while 1:
    command = get_command()
    if command == 'exit':
        break
    elif command == '':
        continue
    else:
        sections.append(command)

标签: python

解决方案


所以你想要这样的东西:

def render_sections():
    first, rest = sections[:1], sections[1:]
    return ' '.join(first + [''.join(['(%s)' % s for s in rest])] + [">>"])

产生:

shell  >>hello
shell (hello) >>aa
shell (hello)(aa) >>aaa
shell (hello)(aa)(aaa) >>

推荐阅读