• 给Python 脚本用optparse传参及用法


    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    import optparse
    
    if __name__=="__main__":
        parse=optparse.OptionParser(usage='"Usage:python %prog [options] arg1,arg2,arg3"',version="%prog 1.2")
        parse.add_option('-u','--user',dest='user',action='store',type=str,help='Enter User Name!!')  
        parse.add_option('--s','--graph_start',dest='graph_start',action='store',type=str,help='Enter start time,For example "2019-10-10"')
        parse.add_option('-e','--graph_end',dest='graph_end',action='store',type=str,help='Enter end time,For example "2019-10-10"')
        parse.add_option('--ms','--miss',dest='miss',action='store',type=str,help='Write 95 billing value to the miss database,value can only be "yes"')
    
        options,args=parse.parse_args()
        print 'OPTIONS:',options
        print 'ARGS:',args
        print "user:",options.user
        print "graph_start:",options.graph_start
        print "graph_end:",options.graph_end
        print "miss:",options.miss
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    python 文件test.py 传参的用法说明:
    1、-u、–user 的用法
    python test.py -uzhangsan
    python test.py -u zhangsan
    python test.py --user=zhangsan
    python test.py --u=zhangsan
    上面四种用法都对,user接收的值都为:zhangsan
    其中接收参数的脚本中

    2、–ms、–miss 的用法
    python test.py --ms yes
    python test.py --ms=yes
    python test.py --miss=yes
    上面三种用法都对,miss接收的值都为:yes

    注意:
    当参数名简写为一个字母时可以在简写前有一个或两个短横杠,比如 -u ,–s ,其中–s的用法与–ms的用法相同;
    当接收参数使用一个短横杠时参数名简写只能是一个字母,比如-u是正确的写法,-us 是错误的写法

  • 相关阅读:
    把 ”中台“ 的思想迁移到代码中去
    typeScript--[interface接口实现类的定义,函数定义,可索引定义]
    字符设备驱动总结
    详解js跨页面传参以及API的解释
    JS 模块化- 04 CMD 规范与 Sea JS
    【Hive-小文件合并】Hive外部分区表利用Insert overwrite的暴力方式进行小文件合并
    微服务可用性之隔离限流降级
    Spring相关知识点
    Vue整合
    Win10 屏蔽键盘按键
  • 原文地址:https://blog.csdn.net/longe20111104/article/details/134060435