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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 11:20:10
Original
1479 people have browsed it

本文实例讲述了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 

Copy after login

运行效果图如下:

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template