command - Tab autocomplete and simple implementation of subcommands in python cmd module? -



command - Tab autocomplete and simple implementation of subcommands in python cmd module? -

is possible add together tab autocomplete subcommands in python cmd class in cmd module? running command loop, , wanted have command called add, can have selection of animal names, add horse, or add elephant. how can tab autocomplete sub commands, if @ possible?

one thing i'm doing actual project i'm working on using different classes different modes. if type whitelist, runs command loop in class , in "whitelist" mode. can type exit go main command loop. seems more heavyweight modes, creating whole new class inherits cmd seems bit much simple adding different types of things illustration above. so, best way add together simple (in terms of code) sub commands cmd class can tab completed? thanks.

the next works:

#!/usr/bin/env python __future__ import print_function cmd import cmd import readline # noqa class zoo(cmd): def __init__(self, animals): cmd.__init__(self) self.animals = animals def do_add(self, animal): print("animal {0:s} added".format(animal)) def completedefault(self, text, line, begidx, endidx): tokens = line.split() if tokens[0].strip() == "add": homecoming self.animal_matches(text) homecoming [] def animal_matches(self, text): matches = [] n = len(text) word in self.animals: if word[:n] == text: matches.append(word) homecoming matches animals = ["bear", "cat", "cheetah", "lion", "zebra"] zoo = zoo(animals) zoo.cmdloop()

python command subcommand

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -