Home >Backend Development >Python Tutorial >python命令行参数解析OptionParser类用法实例

python命令行参数解析OptionParser类用法实例

WBOY
WBOYOriginal
2016-06-06 11:20:101469browse

本文实例讲述了python命令行参数解析OptionParser类的用法,分享给大家供大家参考。

具体代码如下:

from optparse import OptionParser 
 
parser = OptionParser(usage="usage:%prog [optinos] filepath") 
parser.add_option("-t", "--timeout", 
        action = "store", 
        type = 'int', 
        dest = "timeout", 
        default = None, 
        help="Specify annalysis execution time limit" 
        ) 
parser.add_option("-u", "--url", 
        action = "store_true", 
        dest = "url", 
        default = False, 
        help = "Specify if the target is an URL" 
        ) 
(options, args) = parser.parse_args() 
 
if options.url: 
  print(args[0]) 
print options.timeout 

运行效果图如下:

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn