#! /usr/bin/env python
#
# A template for building unix style command line utilities like head(1),
# nl(1), or cat(1).
#
import fileinput
import optparse

VERSION = '0.1.0'


# Set up the command line parser. Help (-h, --help) and version (--version)
# switches are already enabled. See http://docs.python.org/library/optparse.html
#
op = optparse.OptionParser(
	usage='%prog [options] filenames...',
	version='%prog ' + VERSION)

op.add_option('-l', '--list-filenames', action='store_true',
	help="List filenames")
op.add_option('-n', '--number', action='store_true',
	help="Print line numbers")

opts, args = op.parse_args()


# Iterate over all input files: If no file names are given on the command line,
# read from stdin. Otherwise iterate over all given files, with '-' being
# stdin. See http://docs.python.org/library/fileinput.html
#
for line in fileinput.input(args):
    if opts.list_filenames and fileinput.isfirstline():
        print "==>", fileinput.filename(), "<=="

    if opts.number:
        print "%d:%d: %s" % (fileinput.lineno(), fileinput.filelineno(), line),
    else:
        print line,

fileinput.close()

# EOF
