from optparse import OptionParser
import sys useage = []
test_parser = OptionParser(usage="%prog [options]",version="%prog 1.0",description="脚本概括:学习otionparser") test_parser.add_option("-f","--file",
# 这里的意思可以-f选项传递参数,也可以使用--file选项来传递参数
action="store",
# 说实话,这里没弄懂
dest="file_name",
# dest的意思-f选项或者--file选项后面传递的值会赋值给你file_name这个变量中,但是不能直接打印,后面会介绍如何打印
default="test_file",
# 如果-f选项后不传递任何的值,则默认值就是defalut赋值的值
help="input filaname",
# -f选项的帮助信息
type="string")
# -f选项后面的值的类型
test_parser.add_option("-d","--decv",
action="store",
dest="decv_name",
default="test_decv",
help="input decv name",
type="string") if __name__ == '__main__': (options,args) = test_parser.parse_args(sys.argv[:])
   #用来接收选项的值
print(options.file_name)
print(options.decv_name)
print(args)

  

脚本测试:

1、测试-h选项

E:\python\重头开始\day41>python test_optionparser.py -h
Usage: test_optionparser.py [options] 脚本概括:学习otionparser Options:
--version show program's version number and exit
-h, --help show this help message and exit
-f FILE_NAME, --file=FILE_NAME
input filaname
-d DECV_NAME, --decv=DECV_NAME
input decv name E:\python\重头开始\day41>

  

2、测试--version选项

E:\python\重头开始\day41>python test_optionparser.py --version
test_optionparser.py 1.0 E:\python\重头开始\day41>

  

3、测试默认参数的选项

E:\python\重头开始\day41>python test_optionparser.py
test_file
test_decv
['test_optionparser.py']

  

4、测试-f和-d选项

E:\python\重头开始\day41>python test_optionparser.py -d "dddd" -f "fffff"
fffff
dddd
['test_optionparser.py'] E:\python\重头开始\day41>

  

5、测试--file和--decv选项

E:\python\重头开始\day41>python test_optionparser.py --file "ccccc" -d "aaaaa"
ccccc
aaaaa
['test_optionparser.py']

  

OptionParser模块学习的更多相关文章

  1. Python中optionParser模块的使用方法[转]

    本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...

  2. Day5 - Python基础5 常用模块学习

    Python 之路 Day5 - 常用模块学习   本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...

  3. # nodejs模块学习: express 解析

    # nodejs模块学习: express 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的轮子 ...

  4. python中的optionParser模块

    Python 有两个内建的模块用于处理命令行参数:一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数:另一个是 optparse,它功能强大,而 ...

  5. 【转】Python模块学习 - fnmatch & glob

    [转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...

  6. pythone函数基础(7)第三方模块学习

    一,time模块学习 import time # print(int(time.time()))#时间戳# res = time.strftime('%Y-%m-%d %H:%M:%S')#取当前格式 ...

  7. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  8. Python logging 模块学习

    logging example Level When it's used Numeric value DEBUG Detailed information, typically of interest ...

  9. python - argparse 模块学习

    python - argparse 模块学习 设置一个解析器 使用argparse的第一步就是创建一个解析器对象,并告诉它将会有些什么参数.那么当你的程序运行时,该解析器就可以用于处理命令行参数. 解 ...

随机推荐

  1. autocomplete 自动填充 combobox

    目录(?)[-] autocomplete有两种 一种 是 jquery ui里的 autocomplete httpjqueryuicomautocomplete 另一种是 ASPNET AJAX ...

  2. CSS3基础知识核心动画(二)

    Transition过渡 transition-property 过渡属性 all|[attr] transition-duration 过渡时间 transition-delay 延迟时间 tran ...

  3. [Python] 拉格朗日插值

    #-*— coding:utf-8 -*- #Program 0.3 Lagrange Interpolation import matplotlib.pyplot as plt import num ...

  4. CSS3基础

    内容: 1.圆角 border-radius 2.阴影 text-shadow.box-shadow 3.渐变 linear.radial 4.rgba rgb+alpha opacity 5.tra ...

  5. js、C#获取当前url的参数值

    之前很想做一些封装关于获取URL参数值方法,今天简单整理了一下js和后台代码获取url参数值的方法,有什么不好地方,还请大家包涵,代码如下: 1.JS处理URL参数值 <script langu ...

  6. C++官方文档-静态成员

    #include <iostream> using namespace std; class Dummy { public: static int n; int x; Dummy() : ...

  7. windows脚本测试

    一. C:\Users\smc892h>systeminfo | findstr 物理内存物理内存总量:     12,167 MB可用的物理内存:   2,103 MB 二.截取字段 参考网站 ...

  8. leetcode235

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  9. bootstrap左侧边栏

    之前都是想直接把导航栏放左边,但是会占一整行 网上找了好久,看到用bootstrap响应式布局,可以比较简单实现 经典的,可以参考:http://demo.qianduanblog.com/3150/ ...

  10. delphi RAD Studio新版本及路线图 及官方网站 官方 版本发布时间

    delphi  RAD Studio Berlin 10.1 主要是FireMonkey 移动开发的改动,VCL确实没有多大变化. http://docwiki.embarcadero.com/RAD ...