我正在使用
Python的Cmd.cmd创建一个命令行工具,我想添加一个带有filename参数的“load”命令,它支持tab-completion.
import os,cmd,sys,yaml import os.path as op import glob as gb def _complete_path(path): if op.isdir(path): return gb.glob(op.join(path,'*')) else: return gb.glob(path+'*') class CmdHandler(cmd.Cmd): def do_load(self,filename): try: with open(filename,'r') as f: self.cfg = yaml.load(f) except: print 'fail to load the file "{:}"'.format(filename) def complete_load(self,text,line,start_idx,end_idx): return _complete_path(text)
这适用于cwd,但是,当我想进入subdir时,在subdir /之后,complete_load函数的“text”变为空白,所以_complete_path func再次返回cwd.