A question about Cmd Class

Dave Angel davea at ieee.org
Thu Mar 10 03:10:52 EST 2011


On 03/10/2011 01:38 AM, yuan zheng wrote:
> 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.
>

So what are these commands, and how is the shell used?  Could you give 
us more than a trivial fragment of working code?  Since you say you're 
running Linux, are you saying users of your shell wouldn't be able to 
use a command like
     ls -l

in your shell, because it has a dash in the argument string?

BTW, when adding your own comments to quoted text, always use a new 
line, so your own comments do not have a leading ">" character. 
Otherwise it gets quite confusing.

Maybe you're not writing a Linux shell at all, but a shell just to run 
other python "commands" that you've written.  If that's the case, why 
not select names that python will like, and run them directly?  For that 
matter, why have a do_ prefix at all?  If the command is called 
list_modules, let them type list_modules, and call the function 
list_modules().

If that's just not feasible, you could just make a map converting the 
names they type to the actual function objects.  If you like your 
present names, the map might look like:

convert_table = { "start": do_start, "stop": do_stop, "quit", do_quit, 
"show-info": do_show_info }

Then your dispatch would be something like
   cmd = lines.split()[0]
   convert_table[cmd] (arg1, arg2)

DaveA




More information about the Python-list mailing list