read input for cmd.Cmd from file

Discussion in 'Python' started by Achim Domma (Procoders), Jun 3, 2005.

  1. 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
    Achim Domma (Procoders), Jun 3, 2005
    #1
    1. Advertising

  2. Achim Domma (Procoders)

    Peter Otten Guest

    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
    Peter Otten, Jun 3, 2005
    #2
    1. Advertising

  3. Achim Domma (Procoders)

    Peter Otten Guest

    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
    Peter Otten, Jun 3, 2005
    #3
    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. Sarir Khamsi

    Interpreter-like help in cmd.Cmd

    Sarir Khamsi, Jun 9, 2005, in forum: Python
    Replies:
    4
    Views:
    355
    Bengt Richter
    Jun 26, 2005
  2. =?ISO-8859-1?Q?Sch=FCle_Daniel?=

    [exec cmd for cmd in cmds]

    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Mar 8, 2006, in forum: Python
    Replies:
    3
    Views:
    370
    Scott David Daniels
    Mar 8, 2006
  3. Diez B. Roggisch

    pydb remote debugging/cmd.Cmd over socket?

    Diez B. Roggisch, May 28, 2008, in forum: Python
    Replies:
    2
    Views:
    537
    Diez B. Roggisch
    May 29, 2008
  4. Diez B. Roggisch

    cmd.Cmd bug or at least docu-bug

    Diez B. Roggisch, May 29, 2008, in forum: Python
    Replies:
    1
    Views:
    330
    Michele Simionato
    May 29, 2008
  5. Jean-Michel Pichavant

    python cmd.Cmd auto complete feature

    Jean-Michel Pichavant, Mar 8, 2011, in forum: Python
    Replies:
    0
    Views:
    294
    Jean-Michel Pichavant
    Mar 8, 2011
Loading...

Share This Page