Python 处理命令行参数
optparse模块用于从命令行直接读取参数,用法基本与 argparse模块 一致,如下:
#!/usr/bin/env python #-*- coding:utf-8 -*- from optparse import OptionParser parser = OptionParser("Usage: xxxx") parser.add_option('-c', '--chars', dest='characters', action='store_true', default=False, help='only count characters') parser.add_option('-w', '--words', dest='words', action='store_true', default=False, help='only count words') parser.add_option('-l', '--lines', dest='lines', action='store_true', default=False, help='only count lines') options.args = parser.parse_args()
if options.characters: xxxxx elif options.words: xxxxx elif options.lines: xxxxx else: xxxxx