简体   繁体   English

Python CMD自动完成

[英]Python CMD autocomplete

I am currently working on creating a command line interface using the Python's CMD module.This command line takes multiple arguments for various functions in the form:" command parametre1=value1 parametre2=value2 " and so on .I want to setup an TAB autocomplete feature for the parametre's names along with the command name. 我目前正在使用Python的CMD模块创建命令行界面。此命令行采用以下形式表示各种函数的多个参数:“ command parametre1 = value1 parametre2 = value2”,依此类推。我想设置TAB自动完成功能参数的名称以及命令名称。 The commmand name Autocomplete is done but struggling with the Parametre's autcomplete. 命令名称“自动完成”已完成,但与参数的自动完成不尽相同。 Help 救命

I think this does what you want it to do: 我认为这可以满足您的要求:

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?")

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM