简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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