470,643 Members | 1,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,643 developers. It's quick & easy.

Re: How to make a "command line basd" interactive program?

On Apr 11, 2:32 am, Evan <xdi...@gmail.comwrote:
Hope this hasn't been posted hundreds of times. I'm new for this.

Before using python for this kind of script, I was using TCL to write
down a "command line based" interactive program. it likes a "tclsh",
or "python" command, after that, you can work under a prompt, for
example, " - >", and then you can execute any commands what you
defined in script.

Now, in python, are there any common way(class) to finish this work?
or does anybody has a example to do that?

Thanks,
Evan
Do you want a custom shell that does whatever you want? Or do you want
an interactive python shell that has some custom commands?

For the first check out the cmd module
http://docs.python.org/lib/module-cmd.html

example:
>>import cmd
class MyCmd(cmd.Cmd):
.... def do_echo(self, params):
.... print params
....
>>MyCmd().cmdloop()
(Cmd) echo Hello World
Hello World
(Cmd) help

Undocumented commands:
======================
echo help
For the second, check out the code module
http://docs.python.org/lib/module-code.html

example:
>>import code
def foo():
.... print "hello, this is foo"
....
>>code.interact("Welcome to my python shell!", local={'bar':foo})
Welcome to my python shell!
>>bar()
hello, this is foo
>>>
Hope this helps,

Matt
Jun 27 '08 #1
2 1758
that's great, a custom shell is what I need.

Thanks all
Evan
Jun 27 '08 #2
On Apr 15, 6:35 am, Evan <xdi...@gmail.comwrote:
that's great, a custom shell is what I need.

Thanks all
Evan
And for the quick-n-dirty there is:
python -i yourscript.py

Which runs your script then drops you into the interpreter.

- Paddy.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Phil Powell | last post: by
2 posts views Thread by Knepper, Michelle | last post: by
5 posts views Thread by msigwald | last post: by
1 post views Thread by Rune Jacobsen | last post: by
3 posts views Thread by aki | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.