I am trying to use optparse (to parse command line options to my script) and fileinput (to have the flexibility to provide data input via pipe or file).
import optparse, fileinput
parser = OptionParser()
parser.add_option("-v", action="store_true", dest="verbose")
(options, args) = parser.parse_args()
for line in fileinput.input:
process(line)
However fileinput tries to use the '-v' option as well as file name resulting in 'No such file or directory error'. So either I need to make fileinput args or remove the parsed options from sys.argv, however I don't know a elegant way of doing it. Any pointers?
From the documentation:
To specify an alternative list of filenames, pass it as the first argument to
input()
. A single file name is also allowed.
So you can just pass in the leftover args
you get from optparse
.