Karteek Bhat
Karteek Bhat

Reputation: 5

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

Upvotes: 0

Views: 1719

Answers (1)

Robᵩ
Robᵩ

Reputation: 168836

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

Upvotes: 1

Related Questions