import cmd
import string, sys
import os
import readline
class CLI(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self, "tab")
self.prompt = 'input the path:'
self.path = None
readline.set_completer_delims('/\\')
def onecmd(self, path):
if os.path.exists(path):
self.path = path
return path
else:
print "%s not exists"%(path)
return None
def complete(self, text, state):
if state == 0:
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
self.completion_matches = self.completepaths(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def completepaths(self, text, line, *ignored):
(head, tail) = os.path.split(line)
matches = []
if head == "":
p = "."
else:
p = head
if os.path.exists(p):
filelist = os.listdir(p)
matches = [f if os.path.isfile(os.path.join(head, f))\
else f+os.sep for f in filelist if f.startswith(tail)]
return matches
def get_path(self):
self.cmdloop()
return self.path
def get_path():
cli = CLI()
return cli.get_path()
这里我们继承了Cmd类。但是重载了complete这个函数。这个函数是在用户按下tab键呼叫补全的时候被调用。
使用这个小程序只要import这个文件以后直接调用get_path就可以了.
补充:后来发现了一个小bug已经做了修改。cmd module里面在cmdloop()这个函数里就设定了completer_delims (分隔符)。问题在于默认的分隔符里面有很多合法的linux文件名字符比如 “-”这样会导致tab补全的时候出一些问题。 所以我们需要在这之前设置好。这里我们就在__init__初始化的时候设置成/以及\。
No comments:
Post a Comment