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

posted @ 2020-10-07 20:16  OTAKU_nicole  阅读(106)  评论(0编辑  收藏  举报