---
title: "Example of Using getopt in Python"
author: "standalone"
date: "2009-07-24T12:31:05Z"
tags: []
slug: "example-of-using-getopt-in-python"
draft: false
lastmod: "2009-07-24T12:31:05Z"
id: 432202
url: "https://standalone.iteye.com/blog/432202"
---

```
import getopt, sys

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    output = None
    verbose = False
    for o, a in opts:
        if o == "-v":
            verbose = True
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("-o", "--output"):
            output = a
```

 中午详细解释看[这里](http://hi.baidu.com/pavelliu/blog/item/668416d8c23df53033fa1cf3.html)。