optparse命令行参数
import optparse
def testparam(paramA,paramB,paramC='C'):
print('paramA:', paramA)
print('paramB:', paramB)
print('paramC:', paramC)
def main():
parser = optparse.OptionParser('[-]usage %prog ' + '-A <paramA> -B <paramB> [-C <paramC>]')
parser.add_option('-A', dest='paramA', type='string', help='specify target paramA')
parser.add_option('-B', dest='paramB', type='string', help='specify target paramB')
parser.add_option('-C', dest='paramC', type='string', help='specify target paramC')
(options, args) = parser.parse_args()
if (options.paramA == None)|(options.paramB==None):
print(parser.usage)
exit(0)
paramA = options.paramA
paramB = options.paramB
paramC = options.paramC
if options.paramC== None:
paramC = 'paramC'
testparam(paramA,paramB,paramC)
if __name__=='__main__':
main()
python temp.py -A aaa -B bbb
本文来自博客园,作者:OTAKU_nicole,转载请注明原文链接:https://www.cnblogs.com/nicole-zhang/p/13778795.html