Re: python 写命令行程序
"憨狗" <[email protected]>
| Newsgroups | gmane.comp.python.chinese |
|---|---|
| Message-ID | <[email protected]> |
copy一段我的,
try:
opts, args = getopt.getopt(sys.argv[1:], "hs:t:b:", ["help",
"site=","build=","type="])
#支持长选项和短选项两个方式,之所短的: "hs:t:b:"表示-h是开关类型的选项,-s,-t,-t是需要提供额外的值。
#比如: cmd.py -s www.google.com -t homepage -b other_args 或者
#cmd.py -h 或者 cmd.py --help
#opts包含(opt,value)这样的选项和值
#args包含参数列表
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-o", "--output"):
output = a
elif o in ("-s","--site") :
site_alias = a
elif o in ("-t","--type"):
build_type = a
elif o in ("-b","--build"):
build_number = a
else:
assert False, "unhandled option"
# ...
_______________________________________________
python-chinese
Post: send [email protected]
Subscribe: send subscribe to [email protected]
Unsubscribe: send unsubscribe to [email protected]
Detail Info: http://python.cn/mailman/listinfo/python-chinese