You've already forked www.colben.cn
update
This commit is contained in:
@@ -6,7 +6,7 @@ tags: ["python", "optionparser"]
|
||||
categories: ["python"]
|
||||
---
|
||||
|
||||
# 生成标准的、符合Unix/Posix 规范的命令行说明
|
||||
## 生成标准的、符合Unix/Posix 规范的命令行说明
|
||||
```python
|
||||
from optparse import OptionParser
|
||||
parser = OptionParser()
|
||||
@@ -21,7 +21,7 @@ categories: ["python"]
|
||||
print 'zdcl is True.'
|
||||
```
|
||||
|
||||
# 简单流程
|
||||
## 简单流程
|
||||
```python
|
||||
# 引入OptionParser类,创建OptionParser对象
|
||||
from optparse import OptionParser
|
||||
@@ -32,7 +32,7 @@ categories: ["python"]
|
||||
(options, args) = parser.parse_args()
|
||||
```
|
||||
|
||||
# parse_args 和 add_options 函数
|
||||
## parse_args 和 add_options 函数
|
||||
- parse_args() 接收一个命令行列表,默认使用 sys.argv\[:-1\]
|
||||
- 返回两个值,options 保存命令行参数值,args 是由 positional arguments 组成的列表
|
||||
- add_option() 用来加入选项,parse_args() 解析选项
|
||||
@@ -88,11 +88,11 @@ categories: ["python"]
|
||||
help = 'interaction mode: novice, intermediate, or expert [default: %default]')
|
||||
```
|
||||
|
||||
# 备注
|
||||
## 备注
|
||||
- optparse 解析到 help 后不再解析其他命令行参数,usage 信息会优先打印,默认 "usage: %prog \[options\]"
|
||||
- metavar 提醒用户该参数期待的参数,如 metavar = 'mode' 会在帮助中显示成 -m MODE, --mode=MODE
|
||||
|
||||
# OptionGroup 参数分组
|
||||
## OptionGroup 参数分组
|
||||
```python
|
||||
group = OptionGroup(parser, 'Dangerous Options',
|
||||
'Caution: use these options at your own risk. It is believed that some of them bite.')
|
||||
|
Reference in New Issue
Block a user