[英]Python cmd on linux does not autocomplete special characters or symbols
Characters such as -
, +
etc are not parsed the same way as alphanumeric ASCII characters by Python's readline based cmd module. 诸如
-
, +
等字符的解析方式与Python基于readline的cmd模块的字母数字ASCII字符不同。 This seems to be linux specific issue only, as it seems to work as expected on Mac OS. 这似乎只是Linux特定的问题,因为它似乎在Mac OS上按预期工作。
Sample code 示例代码
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()
Expected behavior on Mac OS Mac OS上的预期行为
(Cmd) abc <TAB>
abc
(Cmd) abc -<TAB>
-xxx -yyy -zzz
(Cmd) abc -x<TAB>
(Cmd) abc -xxx
Incorrect behavior on Linux Linux上的行为不正确
(Cmd) abc <TAB>
abc
(Cmd) abc -x<TAB>
<Nothing>
(Cmd) abc -<TAB>
(Cmd) abc --<TAB>
(Cmd) abc ---<TAB>
(Cmd) abc ----
I tried adding -
to cmd.Cmd.identchars, but it didn't help. 我尝试添加
-
到cmd.Cmd.identchars,但它没有帮助。
cmd.Cmd.identchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'
Why is there a difference in readline parsing between Mac OS and Linux even though both use GNU readline: 为什么Mac OS和Linux之间的readline解析存在差异,即使它们都使用GNU readline:
Mac OS: 苹果系统:
>>> readline.__doc__
'Importing this module enables command line editing using GNU readline.'
Linux: Linux的:
>>> readline.__doc__
'Importing this module enables command line editing using GNU readline.'
Thanks! 谢谢!
On linux, the readline
module considers -
a delimiter for tab completion. 在linux上,
readline
模块考虑-
选项卡完成的分隔符。 That is, after a -
is encountered a fresh completion will be tried. 也就是说,在遇到
-
后,将尝试新的完成。
The solution to your problem is the remove -
from the set of characters used by readline as delimiters. 你的问题的解决方法是删除
-
从一组使用的readline作为分隔符的字符。
eg. 例如。
old_delims = readline.get_completer_delims()
readline.set_completer_delims(old_delims.replace('-', ''))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.