Characters such as -, +etc., are not parsed in the same way as ASCII alphanumeric characters with the Python readline-based cmd module. This seems like a specific problem only for Linux, as it is expected to work on Mac OS.
Code example
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
(Cmd) abc <TAB>
abc
(Cmd) abc -<TAB>
-xxx -yyy -zzz
(Cmd) abc -x<TAB>
(Cmd) abc -xxx
Incorrect Linux behavior
(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 that didn't help.
cmd.Cmd.identchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'
Why is there a difference in readline analysis between Mac OS and Linux, although both use GNL readline:
Mac OS:
>>> readline.__doc__
'Importing this module enables command line editing using GNU readline.'
Linux:
>>> readline.__doc__
'Importing this module enables command line editing using GNU readline.'
Thank!
source
share