argparse 命令行解析
例1
import argparse
parser = argparse.ArgumentParser(
prog = 'ProgramName',
description = 'What the program does',
epilog = 'Text at the bottom of help')
parser.add_argument('filename') # positional argument
parser.add_argument('-c', '--count', metavar='COUNT') # option that takes a value
parser.add_argument('-v', '--verbose',
action='store_true') # on/off flag
args = parser.parse_args()
print(args.filename, args.count, args.verbose)
输出
(dsdl-sdk) PS D:\workspace\s3utils> python main.py -h
usage: ProgramName [-h] [-c COUNT] [-v] filename
What the program does
positional arguments:
filename
options:
-h, --help show this help message and exit
-c COUNT, --count COUNT
-v, --verbose
Text at the bottom of help
可见使用argparse一共3个步骤
-
创建ArgumentParser对象
-
利用add_argument()方法增加命令选项, 对选项做一些有必要的互斥、默认值等设置
-
调用parse_args()解析参数,得到一个Namescape对象
ArgumentParser对象的创建
- prog - 程序的名称 (默认值:
os.path.basename(sys.argv[0])) - usage - 描述程序用途的字符串(默认值:从添加到解析器的参数生成)
- description - Text to display before the argument help (by default, no text)
- epilog - Text to display after the argument help (by default, no text)
- parents - 一个
ArgumentParser对象的列表,它们的参数也应包含在内 - formatter_class - 用于自定义帮助文档输出格式的类
- prefix_chars - 可选参数的前缀字符集合(默认值: '-')
- fromfile_prefix_chars - 当需要从文件中读取其他参数时,用于标识文件名的前缀字符集合(默认值:
None) - argument_default - 参数的全局默认值(默认值:
None) - conflict_handler - 解决冲突选项的策略(通常是不必要的)
- add_help - 为解析器添加一个
-h/--help选项(默认值:True) - allow_abbrev - 如果缩写是无歧义的,则允许缩写长选项 (默认值:
True) - exit_on_error - 决定当错误发生时是否让 ArgumentParser 附带错误信息退出。 (默认值:
True)
add_argument()方法参数
| Name | Description | Values |
|---|---|---|
| action | Specify how an argument should be handled | 'store', 'store_const', 'store_true', 'append', 'append_const', 'count', 'help', 'version' |
| choices | 限定参数值取值范围 | ['foo', 'bar'], range(1, 10), or Container instance |
| const | Store a constant value | |
| default | 默认值 | Defaults to None |
| dest | 例如add_argument里用的 --name, 想在程序里用 myname | |
| help | Help message for an argument | |
| metavar | 打印usage的时候参数的默认值 | 仅在显示层面替换,dest在code的变量层面替换 |
| nargs | 参数可取的个数 | int, '?', '*', '+', or argparse.REMAINDER |
| required | Indicate whether an argument is required or optional | True or False |
| type | Automatically convert an argument to the given type | int, float, argparse.FileType('w'), or callable function |