python argparse_python argparse(参数解析)模块学习(一)

classArgumentParser(_AttributeHolder, _ActionsContainer):"""Object for parsing command line strings into Python objects.

Keyword Arguments:

- prog -- The name of the program (default: sys.argv[0])

- usage -- A usage message (default: auto-generated from arguments)

- description -- A description of what the program does

- epilog -- Text following the argument descriptions

- parents -- Parsers whose arguments should be copied into this one

- formatter_class -- HelpFormatter class for printing help messages

- prefix_chars -- Characters that prefix optional arguments

- fromfile_prefix_chars -- Characters that prefix files containing

additional arguments

- argument_default -- The default value for all arguments

- conflict_handler -- String indicating how to handle conflicts

- add_help -- Add a -h/-help option"""

取自argparse-1.4.0

1、prog 程序名(默认是sys.argv[0])

importargparse

parser=argparse.ArgumentParser()

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: argparse-3.py [-h]

optional arguments:-h, --help show this help message and exit

显示程序名为:argparse-3.py

可通过设置prog改变结果

importargparse

parser= argparse.ArgumentParser(prog="learn_argparse_3")

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h]

optional arguments:-h, --help show this help message and exit

可见程序名已经修改为learn_argparse_3

2、usage 程序的使用用例,默认情况下回自动生成

importargparse

parser= argparse.ArgumentParser(prog="learn_argparse_3",usage="[-h] [--help] [...] [what you want???]")

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: [-h] [--help] [...] [what you want???]

optional arguments:-h, --help show this help message and exit

这里可见learn_argparse_3没有打印,故在自定义usage是要注意。

可用:

importargparse

parser= argparse.ArgumentParser(prog="learn_argparse_3",usage="%(prog)s [-h] [--help] [...] [what you want???]")

parser.add_argument('bar', nargs='+', help='bar help')

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

positional arguments:

bar bar help

optional arguments:-h, --help show this help message and exit

3、description help参数之前显示的信息

importargparse

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description="A learn program")

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

A learn program

optional arguments:-h, --help show this help message and exit

description经常会被调用用了简单的描述这个程序的用途,默认在信息的前后会有换行

4、epilog 收场白,可在最后加入你想展示的信息

importargparse

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description="A learn program",

epilog="All for one, one for all")

arg= parser.parse_args()

结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

A learn program

optional arguments:-h, --help show this help message andexit

Allfor one, one for all

5、parents 有时候多个解析器共享一个参数,你可以将这个参数出递给parent

importargparse

parent_parser= argparse.ArgumentParser(add_help=False)

parent_parser.add_argument("--parent", type=int)

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description="A learn program",

epilog="All for one, one for all",

parents=[parent_parser])

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

A learn program

optional arguments:-h, --help show this help message andexit--parent PARENT

Allfor one, one for all

可见--parent 共享了

6、formatter_class 打印信息格式设定

-HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,

ArgumentDefaultsHelpFormatter--Formatter classes which

may be passed as the formatter_class=argument to the

ArgumentParser constructor. HelpFormatteristhe default,

RawDescriptionHelpFormatterandRawTextHelpFormatter tell the parsernot to change the formatting for help text, andArgumentDefaultsHelpFormatter adds information about argument defaults

to the help.

摘自argparse-1.4.0

可见formatter_class有4个参数:HelpFormatter,RawDescriptionHelpFormatter,RawTextHelpFormatter,ArgumentDefaultsHelpFormatter

HelpFormatter 是默认参数

importargparse

parent_parser= argparse.ArgumentParser(add_help=False)

parent_parser.add_argument("--parent", type=int)

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description='''other men live ti eat, while i eat to live -- socrates

love rules his kingdom without a sword -- herbert''',

epilog='''All for one, one for all -- dumas

we soon believe what we desire -- chaucer

the darkest hour is that before the dawn -- fuller''',

parents=[parent_parser],

formatter_class=argparse.HelpFormatter)

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

other men live ti eat,while i eat to live --socrates love rules his kingdom

without a sword--herbert

optional arguments:-h, --help show this help message andexit--parent PARENT

Allfor one, one for all -- dumas we soon believe what we desire --chaucer

the darkest houris that before the dawn -- fuller

可见HelpFormatter粗暴的去除了所有的'\s' '\n'

RawDescriptionHelpFormatter,description和epilog不做修改原样输出

importargparse

parent_parser= argparse.ArgumentParser(add_help=False)

parent_parser.add_argument("--parent", type=int)

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description='''other men live ti eat, while i eat to live -- socrates

love rules his kingdom without a sword -- herbert''',

epilog='''All for one, one for all -- dumas

we soon believe what we desire -- chaucer

the darkest hour is that before the dawn -- fuller''',

parents=[parent_parser],

formatter_class=argparse.RawDescriptionHelpFormatter)

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

other men live ti eat,while i eat to live --socrates

love rules his kingdom without a sword--herbert

optional arguments:-h, --help show this help message andexit--parent PARENT

Allfor one, one for all --dumas

we soon believe what we desire--chaucer

the darkest houris that before the dawn -- fuller

然源码中好像在line之间加入了个indent,不明所以,有知道的小伙伴可以指导下吗?

RawTextHelpFormatter  会保留预定意的帮助信息中的空格

ArgumentDefaultsHelpFormatter会在HelpFormatter的基础上打印默认值给参数(测试发现在add_argument中无help参数也不会显示default)

importargparse

parent_parser= argparse.ArgumentParser(add_help=False)

parent_parser.add_argument("--parent", type=int,default=[1, 2, 3, 4, 5, 6])

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description='''other men live ti eat, while i eat to live -- socrates

love rules his kingdom without a sword -- herbert''',

epilog='''All for one, one for all -- dumas

we soon believe what we desire -- chaucer

the darkest hour is that before the dawn -- fuller''',

parents=[parent_parser],

formatter_class=argparse.RawDescriptionHelpFormatter)

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

other men live ti eat,while i eat to live --socrates

love rules his kingdom without a sword--herbert

optional arguments:-h, --help show this help message andexit--parent PARENT

Allfor one, one for all --dumas

we soon believe what we desire--chaucer

the darkest houris that before the dawn -- fuller

default值并没有打印出来

importargparse

parent_parser= argparse.ArgumentParser(add_help=False)

parent_parser.add_argument("--parent", type=int,default=[1, 2, 3, 4, 5, 6,],help='parent')

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description='''other men live ti eat, while i eat to live -- socrates

love rules his kingdom without a sword -- herbert''',

epilog='''All for one, one for all -- dumas

we soon believe what we desire -- chaucer

the darkest hour is that before the dawn -- fuller''',

parents=[parent_parser],

formatter_class=argparse.ArgumentDefaultsHelpFormatter)

arg= parser.parse_args()

运行结果:

argparse-learn]#python argparse-3.py -h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

other men live ti eat,while i eat to live --socrates love rules his kingdom

without a sword--herbert

optional arguments:-h, --help show this help message andexit--parent PARENT parent (default: [1, 2, 3, 4, 5, 6])

Allfor one, one for all -- dumas we soon believe what we desire --chaucer

the darkest houris that before the dawn -- fuller

7、prefix_chars 自定义前缀,默认'-'

importargparse

parent_parser= argparse.ArgumentParser(add_help=False)

parent_parser.add_argument("--parent", type=int,default=[1, 2, 3, 4, 5, 6,],help='parent')

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description='''other men live ti eat, while i eat to live -- socrates

love rules his kingdom without a sword -- herbert''',

epilog='''All for one, one for all -- dumas

we soon believe what we desire -- chaucer

the darkest hour is that before the dawn -- fuller''',

parents=[parent_parser],

formatter_class=argparse.ArgumentDefaultsHelpFormatter,

prefix_chars='=')

arg= parser.parse_args()

这是不能用“python argparse-3.py -h”,会报错

运行结果:

argparse-learn]#python argparse-3.py =h

usage: learn_argparse_3 [-h] [--help] [...] [what you want???]

other men live ti eat,while i eat to live --socrates love rules his kingdom

without a sword--herbert

optional arguments:=h, ==help show this help message andexit--parent PARENT parent (default: [1, 2, 3, 4, 5, 6])

Allfor one, one for all -- dumas we soon believe what we desire --chaucer

the darkest houris that before the dawn -- fuller

8、fromfile_prefix_chars 有时候将从文件中读取参数,如果设置fromfile_prefix_chars参数的话,解析器会将带有这个前缀的参数当作文件处理

importargparse

parser= argparse.ArgumentParser(fromfile_prefix_chars='@')

parser.add_argument('-f')print parser.parse_args(['-f', 'foo', '@args.txt'])

9、argument_default 给所有没有默认值的参数设置默认值。

importargparse

parent_parser= argparse.ArgumentParser(add_help=False)

parent_parser.add_argument("--parent", type=int,default=[1, 2, 3, 4, 5, 6,],help='parent')

parser= argparse.ArgumentParser(prog="learn_argparse_3",

usage="%(prog)s [-h] [--help] [...] [what you want???]",

description='''other men live ti eat, while i eat to live -- socrates

love rules his kingdom without a sword -- herbert''',

epilog='''All for one, one for all -- dumas

we soon believe what we desire -- chaucer

the darkest hour is that before the dawn -- fuller''',

parents=[parent_parser],

formatter_class=argparse.ArgumentDefaultsHelpFormatter,

prefix_chars='-',

fromfile_prefix_chars='$',

argument_default='The default value for all arguments')

parser.add_argument('--argument_default')print parser.parse_args()

运行结果:

Namespace(argument_default='The default value for all arguments', parent=[1, 2, 3, 4, 5, 6])

10、conflict_handler argumentparser对象不允许传入俩个相同的参数,否则会报错,通过设置conflict_handler为resolve,可以用新的参数覆盖旧的同名参数

importargparse

parser= argparse.ArgumentParser(prog="learn_argparse_4",

conflict_handler='resolve')

parser.add_argument('-b','--boy',help='boy')

parser.add_argument('--boy',help='girl')

parser.print_help()

运行结果:

argparse-learn]#python argparse-4.py

usage: learn_argparse_4 [-h] [-b BOY] [--boy BOY]

optional arguments:-h, --help show this help message andexit-b BOY boy--boy BOY girl

11、add_help默认情况下ArgumentParser对象对自动添加-h/--help选项,可设置add_help=False取消

importargparse

parser= argparse.ArgumentParser(prog="learn_argparse_4",

add_help=False)

parser.add_argument('boy',help='good boy help')

parser.print_help()

运行结果:

argparse-learn]#python argparse-4.py

usage: learn_argparse_4 boy

positional arguments:

boy good boy help

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值