A question about Cmd Class

yuan zheng tsinghuayuan86 at gmail.com
Thu Mar 10 01:38:15 EST 2011


Thanks for your help.


thanks,
yuanzheng.

2011/3/8 Dave Angel <davea at ieee.org>

> 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.
>
> My commands have just one '-', so I don't worry this problem.


> 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?
>
> The reason I use this ways is that, some commands often use this feature
and they are all use python to implement. So I think there is sth.
wrong with my program or sth I don't know about python.


> 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.
>
> Enviroment: Linux  2.6.35-23-generic x86_64 GNU/Linux , ubuntu 10.10.


> DaveA
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110310/c07d4fbd/attachment-0001.html>


More information about the Python-list mailing list