A question about Cmd Class

Dave Angel davea at ieee.org
Tue Mar 8 07:26:57 EST 2011


On 01/-10/-28163 02:59 PM, 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.
>
> If my way is correct when encoutering commands with "-" ?
> If it's correct, how can I implement completion to work correctly?
>
>
>
> thanks,
> yuanzheng.
>

The problem with the replace() is that if there are any other dashes in 
the command, you'll replace them as well.  Further, there are lots of 
other characters that are legal in program names that are not legal in 
variable names.

It isn't clear why you care that the do_show_info function has a name 
that is transformable from the show-info command string.  A shell has a 
few builtin commands, but most of them are simply names of external 
files, and it's those which would have dashes or other awkward 
characters.  Are you planning to have a separate function for every one 
of those possible external programs?

As for tab-completion, you haven't given us any clue about your 
environment (Linux, OS/MFT, ...), nor how the tab completion is caused 
currently.  Ideally, on a shell, the tab completion algorithm would be 
much different than it would for normal programmatic access.  In 
particular, when completing the first name on the line, tab completion 
should use the PATH, and when completing other names, it should escape 
characters like space, tab and backslash.

DaveA



More information about the Python-list mailing list