"""Utilities for people writing Unix-style tools in Python.
Adam Sampson <ats@offog.org>"""

# FIXME: this could also support -i for sed-like tools

import sys

def map_input_files(args, function):
	"""Run function with an open file as its only argument, either with
	each of args opened read-only, or with sys.stdin if args is empty.

	(If you're planning to read line-by-line, the fileinput module from
	the standard library may be a better choice.)"""

	if args == []:
		function(sys.stdin)
	else:
		for fn in args:
			f = open(fn)
			function(f)
			f.close()
