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

Discussion in 'Python' started by Evan, Apr 11, 2008.

  1. Evan

    Evan Guest

    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
    Evan, Apr 11, 2008
    #1
    1. Advertising

  2. Evan

    Matimus Guest

    On Apr 11, 2:32 am, Evan <> wrote:
    > 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
    Matimus, Apr 11, 2008
    #2
    1. Advertising

  3. Evan

    Evan Guest

    that's great, a custom shell is what I need.

    Thanks all
    Evan
    Evan, Apr 15, 2008
    #3
  4. Evan

    Paddy Guest

    On Apr 15, 6:35 am, Evan <> wrote:
    > 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.
    Paddy, Apr 15, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WJ
    Replies:
    2
    Views:
    484
  2. Dave Reed
    Replies:
    2
    Views:
    310
    Dave Kuhlman
    Oct 12, 2003
  3. B.G.R.

    program in interactive mode

    B.G.R., Dec 26, 2004, in forum: Python
    Replies:
    7
    Views:
    305
    Mike Meyer
    Dec 27, 2004
  4. masternige

    interactive program,pyTTS & Tk

    masternige, Sep 24, 2006, in forum: Python
    Replies:
    0
    Views:
    287
    masternige
    Sep 24, 2006
  5. lzlu123
    Replies:
    4
    Views:
    337
    lzlu123
    Aug 22, 2011
Loading...

Share This Page