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

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

Django 查询数据库并返回页面的例子

views.py 视图文件 message = None all_message = UserMessage.objects.filter(name='测试2') if...

pycharm 取消默认的右击运行unittest的方法

取消默认的右击运行unittest方法: File-> Settings -> Tools -> Python Integrated Tools -> Defau...

python字典排序实例详解

本文实例分析了python字典排序的方法。分享给大家供大家参考。具体如下: 1、 准备知识: 在python里,字典dictionary是内置的数据类型,是个无序的存储结构,每一元素是k...

python中的函数用法入门教程

本文较为详细的讲述了Python程序设计中函数的用法,对于Python程序设计的学习有不错的借鉴价值。具体分析如下: 一、函数的定义: Python中使用def关键字定义函数,函数包括函...

python实时获取外部程序输出结果的方法

如下所示: s=subprocess.Popen("ping baidu.com -t",bufsize=0,stdout=subprocess.PIPE,universal_new...