Advertisement

学习Optparse模块

阅读量:

Python 提供了两个内置模块,用于处理命令行参数:

其一是 getopt,在《Deep in python》一书中有所提及,仅适用于对命令行参数进行基础处理;

另一则是 optparse,该模块功能更为全面,操作简便,能够高效生成符合 Unix/Posix 标准的命令行参数说明。

复制代码
    from optparse import OptionParser
    parse = OptionParser()
    parse.add_option("-f", dest="filename",
                 help="write report to file")
    (options, args) = parse.parse_args()
    print(options, args)
    # python optparse模块.py ff df 输出: {"fileanme: None"} ["ff", "df"]
    ## python optparse模块.py -f test 输出:{'filename': 'test'} []
    print(options.filename)
    # None
    ## test
    
    
      
      
      
      
      

全部评论 (0)

还没有任何评论哟~