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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matimus

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

    On Apr 11, 2:32 am, Evan <xdi...@gmail.c omwrote:
    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
    Source code: Lib/cmd.py The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes tha...


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

    Undocumented commands:
    =============== =======
    echo help


    For the second, check out the code module
    Source code: Lib/code.py The code module provides facilities to implement read-eval-print loops in Python. Two classes and convenience functions are included which can be used to build applications...


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

    Matt
  • Evan

    #2
    Re: How to make a &quot;comman d line basd&quot; interactive program?

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

    Thanks all
    Evan

    Comment

    • Paddy

      #3
      Re: How to make a &quot;comman d line basd&quot; interactive program?

      On Apr 15, 6:35 am, Evan <xdi...@gmail.c omwrote:
      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.

      Comment

      Working...