You could have a look at http://docs.python.org/library/cmd.html.
Example code:
import cmd
import sys
class Prompt(cmd.Cmd):
def __init__(self, stufflist=[]):
cmd.Cmd.__init__(self)
self.prompt = '>>> '
self.stufflist = stufflist
print "Hello, I am your new commandline prompt! 'help' yourself!"
def do_quit(self, arg):
sys.exit(0)
def do_print_stuff(self, arg):
for s in self.stufflist:
print s
p = Prompt(sys.argv[1:])
p.cmdloop()
Example test:
$ python cmdtest.py foo bar
Hello, I am your new commandline prompt! 'help' yourself!
>>> help
Undocumented commands:
======================
help print_stuff quit
>>> print_stuff
foo
bar
>>> quit
In order to save the output to a file, you can write what usually goes to stdout also to a file, using for example this class:
class Tee(object):
def __init__(self, out1, out2):
self.out1 = out1
self.out2 = out2
def write(self, s):
self.out1.write(s)
self.out2.write(s)
def flush(self):
self.out1.flush()
self.out2.flush()
You can use it like this:
with open('cmdtest.out', 'w') as f:
# write stdout to file and stdout
t = Tee(f, sys.stdout)
sys.stdout = t
A problem is that the commands read in via stdin do not appear in this output, but I believe that this can be easily solved.