In order to offer options to my python script, I want to introduce some parameters. I found that the better way to do this in python is with getopt, but once I run my script it doesn’t do anything. Please help me!!!. This is my code:
def main(argv):
try:
opts, args = getopt.getopt(argv, 'hi:o:t', ['help', 'input=', 'output='])
except getopt.GetoptError:
usage()
sys.exit(2)
file = None
outfile = None
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
elif opt in ('-i', '--input'):
file = arg
elif opt in ('-o', '--output'):
outfile = arg
elif opt == '-t':
maininfo(file,outfile)
else:
usage()
sys.exit(2)
if __name__ =='__main__':
main(sys.argv[1:])
I suggest adding more logging. Not only will this help you out now, it’ll help out whoever uses your script in future.
I also moved the call to
maininfo()out of the loop as you could supply-tbefore the filenames!