首页 > 解决方案 > 在嵌套完成 python-prompt-ttoolkit 中使用自定义分隔符

问题描述

我正在使用 python-prompt-toolkit 进行自动补全,它对于单次补全工作非常好。但我需要使用嵌套补全。考虑文档中的以下示例:


from prompt_toolkit import prompt
from prompt_toolkit.completion import NestedCompleter


completer = NestedCompleter.from_nested_dict(
    {
        "show": 
            {"version": None,
           
           "clock": None, 
            
            "ip": {"interface": {"brief": None}}
            },
        "exit": None,
    }
)


def main():
    text = prompt("Type a command: ", completer=completer)
    print("You said: %s" % text)


if __name__ == "__main__":
    main()

虽然它确实有效,但它仅在我使用空格作为分隔符(即,在单词之间保留空格)来分隔单词时才有效,例如 。但是show version我想使用'\\' or '/'(取决于操作系统)作为分隔符,所以它会像show/ip/interface/brief(类似壳路径完成如何工作)。我知道你可以做到这一点,但我不知道怎么做?非常感谢您的帮助。

标签: pythonprompt-toolkit

解决方案


推荐阅读