Monday, June 16, 2008

Python read command line options

Here is an example of how to read command line arguments with Python.


import getopt, sys

def main():
try:
opts, args = getopt.getopt(sys.argv[1:], ":i:r:")
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
output = None
verbose = False
for opt in opts:
if opt[0] == "-i":
id = opt[1]
if opt[0] == "-r":
root = opt[1]
print id
print root
if __name__ == "__main__":
main()

No comments: