Last active
May 19, 2016 08:18
-
-
Save ikautak/5354054 to your computer and use it in GitHub Desktop.
auto complete decorator for cmd module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import cmd | |
def file_auto_complete(text, line, begidx, endidx): | |
""" auto complete of file name. | |
""" | |
line = line.split() | |
if len(line) < 2: | |
filename = '' | |
path = './' | |
else: | |
path = line[-1] | |
if '/' in path: | |
i = path.rfind('/') | |
filename = path[i+1:] | |
path = path[:i] | |
else: | |
filename = path | |
path = './' | |
annotate = lambda x : x+'/' if os.path.isdir(os.path.join(path, x)) else x | |
ls = map(annotate, sorted(os.listdir(path))) | |
if filename == '': | |
return ls | |
else: | |
return [f for f in ls if f.startswith(filename)] | |
def auto_complete(func): | |
""" auto_complete decorator. | |
func is not changed, only add 'complete_' method. | |
""" | |
def _complete(self, *args): | |
return file_auto_complete(*args) | |
# Add 'complete_' method to super class. | |
setattr(cmd.Cmd, 'complete_' + func.__name__[3:], _complete) | |
return func | |
class MyCmd(cmd.Cmd): | |
def __init__(self): | |
cmd.Cmd.__init__(self) | |
self.prompt = '(MyCmd)' | |
@auto_complete | |
def do_foo(self, line): | |
print('foo ' + line) | |
@auto_complete | |
def do_bar(self, line): | |
print('bar' + line) | |
if __name__ == '__main__': | |
mycmd = MyCmd() | |
mycmd.cmdloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment