python optparse 入门练习

来源:互联网 发布:qt编程入门windows 编辑:程序博客网 时间:2024/05/16 18:04

更多内容参考:https://docs.python.org/2/library/optparse.html


from optparse import OptionParser

parser = OptionParser()
#parser.add_option("-f", "--file", dest="filename",
parser.add_option("-f",
                  help="write report to FILE", metavar="FILE")
"""
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")
"""
parser.add_option("-q", "--quiet",
                  action="store_const", const=0, dest="verbose")
parser.add_option("-v", "--verbose",
                  action="store_const", const=1, dest="verbose")
parser.add_option("--noisy",
                  action="store_const", const=2, dest="verbose")
(options, args) = parser.parse_args()


print options, args
print options.__class__
parser.error("options -a and -b are mutually exclusive")


lhg@lhg-VirtualBox:~/pythondemo/optparse-demo$ python optparse-demo.py -f xx  -v -qasdf adf
{'verbose': 0, 'f': 'xx'} ['asdf', 'adf']
optparse.Values
Usage: optparse-demo.py [options]

optparse-demo.py: error: options -a and -b are mutually exclusive


0 0