A question about Cmd Class

Dog Walker thudfoo at gmail.com
Tue Mar 8 17:05:25 EST 2011


On Monday 2011 March 07 18:41, yuan zheng wrote:
> Hello, everyone:
>
>     I encouter a question when implementing a commmand line(shell).
> I have implemented some commands, such as "start", "stop", "quit",
> they are easily implemented by "do_start", "do_stop" and "do_quit".
> there are no troubles.
>      But I want to implement some commands like these "list-modules",
> "show-info". There is a character "-" among the string. So I can't easily
> use "do_list-modules", because the name is invalid. I attempt another
> ways, add a sentense in function "cmd.onecmd":
> -------------------------------------------------------
>    def onecmd(self, line):
>         line = line.replace("-", "_")         # I add
>     ...
> -------------------------------------------------------
> Then, I can use "do_list_modules" to mach "list-modules" command. But in
> this way, completion cannot work correctly. If I input "list-", and then
> "tab",
> it would not complete.
>

That is because the readline module uses '-' as one of its stop characters. 
You can try this code I used:

# PyPI package names can contain hyphens.
# readline interprets a hyphen as a word boundary.
# We need to remove the hyphen from readline's
# word boundary delimiters so that our findpkg
# command can complete on package name.
import readline
delims = readline.get_completer_delims(  )
delims = delims.replace('-', '')
readline.set_completer_delims(delims)
del delims




More information about the Python-list mailing list