Hide
Know what you're getting – Unlike many sites, all our code is clearly licensed. Join Siafoo Now or Learn More

Parsing command line options with getopt Atom Feed 0

In Brief This is an example of using the Python getopt module to parse command line options.
# 's
 1import getopt, sys
2
3def main():
4 try:
5 opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
6 except getopt.GetoptError, err:
7 # print help information and exit:
8 print str(err) # will print something like "option -a not recognized"
9 usage()
10 sys.exit(2)
11 output = None
12 verbose = False
13 for o, a in opts:
14 if o == "-v":
15 verbose = True
16 elif o in ("-h", "--help"):
17 usage()
18 sys.exit()
19 elif o in ("-o", "--output"):
20 output = a
21 else:
22 assert False, "unhandled option"
23 # ...
24
25if __name__ == "__main__":
26 main()

This is an example of using the Python getopt module to parse command line options.