Python CMD 自动完成

Python CMD autocomplete

我目前正在使用 Python 的 CMD 创建命令行界面 module.This 命令行为各种函数采用多个参数,格式为:" command parametre1=value1 parametre2=value2 “等等。我想为参数名称和命令名称设置 TAB 自动完成功能。命令名称自动完成已完成,但正在努力处理参数的自动完成。 帮助

我认为这符合您的要求:

import cmd


class MyCmd(cmd.Cmd):
    def do_command(self, line):
        'do_command: [parametre[1,2]=xxx]'

    def complete_command(self, text, line, begidx, endidx):
        return [i
                for i in ('parametre1=', 'parametre2=')
                if i.startswith(text)]

    def do_EOF(self, line):
        'exit the program. Use  Ctrl-D (Ctrl-Z in Windows) as a shortcut'
        return True

if __name__ == "__main__":
    myCmd = MyCmd()
    myCmd.cmdloop("Welcome! What is your command?")

参考:https://wiki.python.org/moin/CmdModule#Completion