I've written a command line utility that uses getopt for parsing arguments given on the command line. I would also like to have a filename be an optional argument, such as it is in other utilities like grep, cut etc. So, I would like it to have the following usage
tool -d character -f integer [filename]
How can I implement the following?
The fileinput module may do what you want - assuming the non-option arguments are in args
then:
import fileinput
for line in fileinput.input(args):
print line
If args
is empty then fileinput.input()
will read from stdin; otherwise it reads from each file in turn, in a similar manner to Perl's while(<>)
.