470,580 Members | 2,209 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

read input for cmd.Cmd from file

Hi,

I'm writing a simple shell using cmd.Cmd. It would be very usefull if I
could read the commands as batchjob from a file. I've tried the following:

class MyShell(cmd.Cmd):
def __init__(self,stdin):
cmd.Cmd.__init__(self,stdin=stdin)
...
...

if __name__=='__main__':
if len(sys.argv)==2:
shell=MyShell(file(sys.argv[1]))
else:
shell=MyShell(sys.stdin)
shell.cmdloop()

Calling 'myshell.py inputfile' with an invalid inputfile, I get an
error, so it seems that the file is opened. But the shell starts as
usuall, ignoring the content of the file. There is no output and no
errors (if I write nonsens into the inputfile).

Could anybody help?

regards,
Achim
Jul 19 '05 #1
2 2331
Achim Domma (Procoders) wrote:
I'm writing a simple shell using cmd.Cmd. It would be very usefull if I
could read the commands as batchjob from a file. I've tried the following:

class MyShell(cmd.Cmd):
def __init__(self,stdin):
cmd.Cmd.__init__(self,stdin=stdin)
...
...

if __name__=='__main__':
if len(sys.argv)==2:
shell=MyShell(file(sys.argv[1]))
else:
shell=MyShell(sys.stdin)
shell.cmdloop()

Calling 'myshell.py inputfile' with an invalid inputfile, I get an
error, so it seems that the file is opened. But the shell starts as
usuall, ignoring the content of the file. There is no output and no
errors (if I write nonsens into the inputfile).


[While I'm at it, duplicated from de.comp.lang.python]

Interesting idea. The simplest approach I found was to feed the file
directly into the cmdqueue-Attribute:

import cmd

class Cmd(cmd.Cmd):
****def*do_this(self,*arg):
********print*"this>",*arg
****def*do_that(self,*arg):
********print*"*****<that",*arg
****def*do_quit(self,*arg):
********print*"That's*all,*folks"
********return*True

if __name__ == "__main__":
****import*optparse
****parser*=*optparse.OptionParser()
****parser.add_option("-i",*"--interactive",*action="store_true")
****options,*args*=*parser.parse_args()
****
****c*=*Cmd()
****try:
********filename,*=*args
****except*ValueError:
********pass
****else:
********c.cmdqueue.extend(file(filename))
********if*not*options.interactive:
************c.cmdqueue.append("quit\n")
****
****c*=*c.cmdloop()

$ cat batch.txt
this
that
that
oops
that

$ python2.4 batch_cmd.py batch.txt
this>
*****<that
*****<that
*** Unknown syntax: oops
*****<that
That's all, folks

If you want to continue the session in the interaktive mode:

$ python2.4 batch_cmd.py batch.txt -i
this>
*****<that
*****<that
*** Unknown syntax: oops
*****<that
(Cmd)
Peter

Jul 19 '05 #2
Achim Domma (Procoders) wrote:
I'm writing a simple shell using cmd.Cmd. It would be very usefull if I
could read the commands as batchjob from a file. I've tried the following:


[...]

Your original approach should work too if you clear the use_rawinput flag
and provide a do_EOF() method that handles the file end:

import cmd

class Cmd(cmd.Cmd):
def do_this(self, arg):
print "this>", arg
def do_that(self, arg):
print " <that", arg
def do_quit(self, arg):
print "That's all, folks"
return True
do_EOF = do_quit

if __name__ == "__main__":
import sys
filename = sys.argv[1]
c = Cmd(stdin=file(filename))
c.use_rawinput = False
c = c.cmdloop()

Peter

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gunnar | last post: by
18 posts views Thread by jas | last post: by
3 posts views Thread by Bill Cohagan | last post: by
10 posts views Thread by Tibby | last post: by
8 posts views Thread by a | last post: by
1 post views Thread by Jose Reckoner | last post: by
6 posts views Thread by portCo | last post: by
9 posts views Thread by vineeth | last post: by
63 posts views Thread by Bill Cunningham | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.