As you said, I am not writting a linux shell. <span id="result_box" class="short_text" lang="en"><span title="点击可显示其他翻译" class="hps">Our requirement</span></span>s <br>need this kind of commands.  <br><br>Certernly,  the convert_table can sovle the command problem.<br>
But it cannot complete correctly when using "TAB". It will complete<br>"show_info".<br><br>My first email expains my problem: I can implement commands with '-',<br>but I can't let completion works well.<br>
<br><br>thanks,<br>yuanzheng.<br><br><div class="gmail_quote">2011/3/10 Dave Angel <span dir="ltr"><<a href="mailto:davea@ieee.org">davea@ieee.org</a>></span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5">On 03/10/2011 01:38 AM, yuan zheng wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Thanks for your help.<br>
<br>
<br>
thanks,<br>
yuanzheng.<br>
<br>
2011/3/8 Dave Angel<<a href="mailto:davea@ieee.org" target="_blank">davea@ieee.org</a>><br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
On 01/-10/-28163 02:59 PM, yuan zheng wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hello, everyone:<br>
<br>
     I encouter a question when implementing a commmand line(shell).<br>
I have implemented some commands, such as "start", "stop", "quit",<br>
they are easily implemented by "do_start", "do_stop" and "do_quit".<br>
there are no troubles.<br>
      But I want to implement some commands like these "list-modules",<br>
"show-info". There is a character "-" among the string. So I can't easily<br>
use "do_list-modules", because the name is invalid. I attempt another<br>
ways, add a sentense in function "cmd.onecmd":<br>
-------------------------------------------------------<br>
    def onecmd(self, line):<br>
         line = line.replace("-", "_")         # I add<br>
     ...<br>
-------------------------------------------------------<br>
Then, I can use "do_list_modules" to mach "list-modules" command. But in<br>
this way, completion cannot work correctly. If I input "list-", and then<br>
"tab",<br>
it would not complete.<br>
<br>
If my way is correct when encoutering commands with "-" ?<br>
If it's correct, how can I implement completion to work correctly?<br>
<br>
<br>
<br>
thanks,<br>
yuanzheng.<br>
<br>
<br>
</blockquote>
The problem with the replace() is that if there are any other dashes in the<br>
command, you'll replace them as well.  Further, there are lots of other<br>
characters that are legal in program names that are not legal in variable<br>
names.<br>
<br>
My commands have just one '-', so I don't worry this problem.<br>
</blockquote>
<br>
</blockquote>
<br></div></div>
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<br>

    ls -l<br>
<br>
in your shell, because it has a dash in the argument string?<br>
<br>
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.<br>
<br>
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().<br>

<br>
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:<br>
<br>
convert_table = { "start": do_start, "stop": do_stop, "quit", do_quit, "show-info": do_show_info }<br>
<br>
Then your dispatch would be something like<br>
  cmd = lines.split()[0]<br>
  convert_table[cmd] (arg1, arg2)<br>
<br>
DaveA<br>
<br>
</blockquote></div><br>