python标准库介绍——26 getopt 模块详解
==getopt 模块== ``getopt`` 模块包含用于抽出命令行选项和参数的函数,
它可以处理多种格式的选项. 如 [Example 2-23 #eg-2-23] 所示. 其中第 2 个参数指定了允许的可缩写的选项. 选项名后的冒号(:) 意味这这个选项必须有额外的参数. ====Example 2-23. 使用 getopt 模块====[eg-2-23] ```
File: getopt-example-1.py import getopt
import sys # simulate command-line invocation
# 模仿命令行参数
sys.argv = ["myscript.py", "-l", "-d", "directory", "filename"] # process options
# 处理选项
opts, args = getopt.getopt(sys.argv[1:], "ld:") long = 0
directory = None for o, v in opts:
if o == "-l":
long = 1
elif o == "-d":
directory = v print "long", "=", long
print "directory", "=", directory
print "arguments", "=", args *B*long = 1
directory = directory
arguments = ['filename']*b*
``` 为了让 ``getopt`` 查找长的选项, 如 [Example 2-24 #eg-2-24] 所示,
传递一个描述选项的列表做为第 3 个参数. 如果一个选项名称以等号(=) 结尾,
那么它必须有一个附加参数. ====Example 2-24. 使用 getopt 模块处理长选项====[eg-2-24] ```
File: getopt-example-2.py import getopt
import sys # simulate command-line invocation
# 模仿命令行参数
sys.argv = ["myscript.py", "--echo", "--printer", "lp01", "message"] opts, args = getopt.getopt(sys.argv[1:], "ep:", ["echo", "printer="]) # process options
# 处理选项
echo = 0
printer = None for o, v in opts:
if o in ("-e", "--echo"):
echo = 1
elif o in ("-p", "--printer"):
printer = v print "echo", "=", echo
print "printer", "=", printer
print "arguments", "=", args *B*echo = 1
printer = lp01
arguments = ['message']*b*
``` ```
[!Feather 注: 我不知道大家明白没, 可以自己试下:
myscript.py -e -p lp01 message
myscript.py --echo --printer=lp01 message
]
```
python标准库介绍——26 getopt 模块详解的更多相关文章
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...
- python标准库介绍——8 operator 模块详解
==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
- python标准库介绍——22 UserList 模块详解
==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...
- python标准库介绍——21 UserDict 模块详解
==UserDict 模块== ``UserDict`` 模块包含了一个可继承的字典类 (事实上是对内建字典类型的 Python 封装). [Example 2-15 #eg-2-15] 展示了一个增 ...
随机推荐
- 默认网关和默认路由 —— Cisco CCNA – Default Gateway & Default Routes
原文:https://www.certificationkits.com/cisco-certification/ccna-articles/cisco-ccna-intro-to-routing-b ...
- what difference between libfm and libffm
https://www.kaggle.com/users/25112/steffen-rendle/forum Congratulations to Yu-Chin, Wei-Sheng, Yong ...
- Nuget添加新项目的问题
为已有的几个项目添加了一个nuget package 后,在解决方法中添加了一个新项目,然后想把这个nuget package添加到这个新建的项目中去,可以此时无法添加. 怎么办那? [解决方 ...
- java代码在开始事务后,先做了一个查询,再insert,此时会报: java.sql.SQLException: could not retrieve transation read-only status server
解决过程: 查看mysql的事物隔离级别 SHOW VARIABLES LIKE '%iso%'; 返回结果: REPEATABLE-READ 把这个改成:READ-COMMITTED 就好了: SE ...
- Window 下安装 Redis
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. 一.Window 下安装 redis https://github.com/MicrosoftArchive/r ...
- 【Javascript Demo】根据Email地址跳转到相应的邮箱登录页面
我的初步想法是通过指定的邮箱地址自动查找到对应的邮箱登录页面,但是用数据库.js什么的都有局限性,因为各种各样的邮箱太多了,不能都包含的到,网上找了半天都没有找到满意的答案,自己又想不出方法,只能暂时 ...
- 【转载】oracle更新语法
oracle更新语法:1.一般语法 update tab set col = .... [where ...] =后可以有子查询,但是必须对于tab的每一列返回唯一一行与之对应,where是需 ...
- VS2010中生成遇到的 web.config 问题
1. 错误:无法在此路径使用此配置节.当站点管理员使用继承的配置文件中的 <location allowOverride="false"> 锁定对此节的访问时会出现 ...
- 同时安装不同版本JDK遇到的问题
安装JDK1.8出现 Error opening registry key'software\Javasoft\Java Runtime Environment' java安装1.8后的问题:之前安装 ...
- MySQL user表简介
mysql> DESC MYSQL.USER \G *************************** . row *************************** Field: ...