raj*_*aja 4 python linux readline tab-completion python-cmd
字符,如-,+等不解析由Python的readline的字母数字ASCII字符基于CMD模块以同样的方式.这似乎只是Linux特定的问题,因为它似乎在Mac OS上按预期工作.
示例代码
import cmd
class Test(cmd.Cmd):
def do_abc(self, line):
print line
def complete_abc(self, text, line, begidx, endidx):
return [i for i in ['-xxx', '-yyy', '-zzz'] if i.startswith(text)]
try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
Test().cmdloop()
Run Code Online (Sandbox Code Playgroud)
Mac OS上的预期行为
(Cmd) abc <TAB>
abc
(Cmd) abc -<TAB>
-xxx -yyy -zzz
(Cmd) abc -x<TAB>
(Cmd) abc -xxx
Run Code Online (Sandbox Code Playgroud)
Linux上的行为不正确
(Cmd) abc <TAB>
abc
(Cmd) abc -x<TAB>
<Nothing>
(Cmd) abc -<TAB>
(Cmd) abc --<TAB>
(Cmd) abc ---<TAB>
(Cmd) abc ----
Run Code Online (Sandbox Code Playgroud)
我尝试添加-到cmd.Cmd.identchars,但它没有帮助.
cmd.Cmd.identchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'
Run Code Online (Sandbox Code Playgroud)
为什么Mac OS和Linux之间的readline解析存在差异,即使它们都使用GNU readline:
苹果系统:
>>> readline.__doc__
'Importing this module enables command line editing using GNU readline.'
Run Code Online (Sandbox Code Playgroud)
Linux的:
>>> readline.__doc__
'Importing this module enables command line editing using GNU readline.'
Run Code Online (Sandbox Code Playgroud)
谢谢!
在linux上,readline模块考虑-用于制表符完成的分隔符.也就是说,在-遇到a之后,将尝试新的完成.
您的问题的解决方案是-从readline使用的字符集中删除作为分隔符.
例如.
old_delims = readline.get_completer_delims()
readline.set_completer_delims(old_delims.replace('-', ''))
Run Code Online (Sandbox Code Playgroud)