您好,欢迎来到飒榕旅游知识分享网。
搜索
您的当前位置:首页python之参数解析模块argparse

python之参数解析模块argparse

来源:飒榕旅游知识分享网
python之参数解析模块argparse

2.7之后python不再对optparse模块进⾏扩展,python标准库推荐使⽤argparse模块对命令⾏进⾏解析。

简单⼊门

先来看个例⼦:argparse_test.py:

import argparse

parser = argparse.ArgumentParser()parser.parse_args()

运⾏程序:

ShanedeMBP:some_work shane$ python3 argparse_test.py --helpusage: argparse_test.py [-h]

optional arguments:

-h, --help show this help message and exit

ShanedeMBP:some_work shane$ python3 argparse_test.py --verboseusage: argparse_test.py [-h]

argparse_test.py: error: unrecognized arguments: --verboseShanedeMBP:some_work shane$ python3 argparse_test.py foousage: argparse_test.py [-h]

argparse_test.py: error: unrecognized arguments: fooShanedeMBP:some_work shane$

结果分析:

若不给参数⽽运⾏这个程序,将不会得到任何结果。

第⼆条命名显⽰了使⽤的argparse的好处,你什么也没做,却得到了⼀个很好的帮助信息。

我们⽆需⼈为设置--help参数,就能得到⼀个良好的帮助信息。但是若给其他参数(⽐如foo)就会产⽣⼀个错误。

位置参数

我们修改下脚本:

import argparse

parser = argparse.ArgumentParser()parser.add_argument(\"echo\")args = parser.parse_args()

运⾏:

nedeMBP:some_work shane$ python3 argparse_test.py usage: argparse_test.py [-h] echo

argparse_test.py: error: the following arguments are required: echoShanedeMBP:some_work shane$ python3 argparse_test.py --helpusage: argparse_test.py [-h] echopositional arguments: echo

optional arguments:

-h, --help show this help message and exit

ShanedeMBP:some_work shane$ python3 argparse_test.py foo

结果分析:

这次,我们增加了⼀个add_argument()⽅法,⽤来设置程序可接受的命令⾏参数。现在要运⾏程序,就必须设置⼀个参数。

parse_args()⽅法实际上从我们的命令⾏参数中返回了⼀些数据,在上⾯的例⼦中是echo这个像“魔法”⼀样的过程,是argparse⾃动完成的。

尽管⾃动产⽣的帮助信息展⽰地很美观,但是我们仍然⽆法只根据echo这个参数知道它是做什么的。所以,我们增加了⼀些东西,使得它变得更有⽤。

import argparse

parser = argparse.ArgumentParser()

parser.add_argument(\"echo\args = parser.parse_args()# print(args.echo)

运⾏结果

ShanedeMBP:some_work shane$ python3 argparse_test.py usage: argparse_test.py [-h] echo

ShanedeMBP:some_work shane$ python3 argparse_test.py foofoo

ShanedeMBP:some_work shane$ python3 argparse_test.py -husage: argparse_test.py [-h] echopositional arguments:

echo echo the string you use here!optional arguments:

-h, --help show this help message and exit

再多⼀点改变:

import argparse

parser = argparse.ArgumentParser()

# parser.add_argument(\"echo\parser.add_argument('square',help=\"input the digital number\")args = parser.parse_args()print(args.square**2)

输出结果:

ShanedeMBP:some_work shane$ python3 argparse_test.py 4Traceback (most recent call last):

File \"argparse_test.py\ print(args.square**2)

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

这个程序并不能正确运⾏,因为argparse会将输⼊当作字符串处理,所以我们需要设置它的类型:(type=int)

import argparse

parser = argparse.ArgumentParser()

# parser.add_argument(\"echo\parser.add_argument('square',help=\"input the digital number\args = parser.parse_args()print(args.square**2)

运⾏结果:

ShanedeMBP:some_work shane$ python3 argparse_test.py 416

ShanedeMBP:some_work shane$ python3 argparse_test.py fourusage: argparse_test.py [-h] square

argparse_test.py: error: argument square: invalid int value: 'four'

这样,指定了type类型后,可⾃动识别类型,好⽅便有⽊有...

详解

import argparse

#创建⼀个解析对象

parser = argparse.ArgumentParser()

# parser.add_argument(\"echo\

#向对象中添加相关命令⾏参数或选项,每⼀个add_argument⽅法对应⼀个参数或选项parser.add_argument('square',help=\"input the digital number\#调⽤parse_args()⽅法进⾏解析args = parser.parse_args()print(args.square**2)

⽅法ArgumentParser

ArgumentParser(prog=None, usage=None,description=None, epilog=None, parents=[],formatter_class=argparse.HelpFormatter, prefix_chars='-',fromfile_prefix_chars=None, argument_default=None,conflict_handler='error', add_help=True)

这些参数都有默认值,当调⽤parser.print_help()或者运⾏程序时由于参数不正确(此时python解释器其实也是调⽤了pring_help()⽅法)时,会打印这些描述信息,⼀般只需要传递description参数,如上。prog

程序的名字,默认为sys.argv[0],⽤来在help信息中描述程序的名称。

>>> parser = argparse.ArgumentParser(prog='myprogram')>>> parser.print_help()usage: myprogram [-h]

optional arguments:

-h, --help show this help message and exit

usage

描述程序⽤途的字符串

>>> parser = argparse.ArgumentParser(prog='PROG', usage='%(prog)s [options]')>>> parser.add_argument('--foo', nargs='?', help='foo help')>>> parser.add_argument('bar', nargs='+', help='bar help')>>> parser.print_help()usage: PROG [options]positional arguments: bar bar help

optional arguments:

-h, --help show this help message and exit --foo [FOO] foo help

descriptionhelp信息前的⽂字。epilog

help信息之后的信息

>>> parser = argparse.ArgumentParser(... description='A foo that bars',

... epilog=\"And that's how you'd foo a bar\")>>> parser.print_help()usage: argparse.py [-h]A foo that bars

optional arguments:

-h, --help show this help message and exitAnd that's how you'd foo a bar

parents

由ArgumentParser对象组成的列表,它们的arguments选项会被包含到新ArgumentParser对象中。

>>> parent_parser = argparse.ArgumentParser(add_help=False)>>> parent_parser.add_argument('--parent', type=int)

>>> foo_parser = argparse.ArgumentParser(parents=[parent_parser])>>> foo_parser.add_argument('foo')

>>> foo_parser.parse_args(['--parent', '2', 'XXX'])Namespace(foo='XXX', parent=2)

formatter_classhelp信息输出的格式.prefix_chars

参数前缀,默认为'-'

>>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='-+')>>> parser.add_argument('+f')>>> parser.add_argument('++bar')

>>> parser.parse_args('+f X ++bar Y'.split())Namespace(bar='Y', f='X')

fromfile_prefix_chars前缀字符,放在⽂件名之前

>>> with open('args.txt', 'w') as fp:... fp.write('-f\\nbar')

>>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')>>> parser.add_argument('-f')

>>> parser.parse_args(['-f', 'foo', '@args.txt'])Namespace(f='bar')

当参数过多时,可以将参数放到⽂件中读取,例⼦中parser.parse_args(['-f', 'foo', '@args.txt'])解析时会从⽂件args.txt读取,相当于 ['-f', 'foo', '-f', 'bar']。argument_default

参数的全局默认值。例如,要禁⽌parse_args时的参数默认添加,我们可以:

>>> parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)>>> parser.add_argument('--foo')

>>> parser.add_argument('bar', nargs='?')>>> parser.parse_args(['--foo', '1', 'BAR'])Namespace(bar='BAR', foo='1')>>> parser.parse_args()Namespace()

当parser.parse_args()时不会⾃动解析foo和bar了。conflict_handler

解决冲突的策略,默认情况下冲突会发⽣错误:

>>> parser = argparse.ArgumentParser(prog='PROG')>>> parser.add_argument('-f', '--foo', help='old foo help')>>> parser.add_argument('--foo', help='new foo help')Traceback (most recent call last): ..

ArgumentError: argument --foo: conflicting option string(s): --foo

我们可以设定冲突解决策略:

>>> parser = argparse.ArgumentParser(prog='PROG', conflict_handler='resolve')>>> parser.add_argument('-f', '--foo', help='old foo help')>>> parser.add_argument('--foo', help='new foo help')>>> parser.print_help()

usage: PROG [-h] [-f FOO] [--foo FOO]optional arguments:

-h, --help show this help message and exit -f FOO old foo help --foo FOO new foo help

add_help

设为False时,help信息⾥⾯不再显⽰-h --help信息。

⽅法add_argument 参数选项

add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])>>> parser.add_argument('integers', metavar='N', type=int, nargs='+',... help='an integer for the accumulator')

>>> parser.add_argument('--sum', dest='accumulate', action='store_const',... const=sum, default=max,

... help='sum the integers (default: find the max)')

name or flags

命令⾏参数名或者选项,如上⾯的address或者-p,--port.其中命令⾏参数如果没给定,且没有设置defualt,则出错。但是如果是选项的话,则设置为None,如果是多个参数(短参数和长参数)表⽰同⼀个意义的话,可⽤,隔开⼀共有两种,位置参数和可选参数.添加可选参数:

>>> parser.add_argument('-f', '--foo')

添加位置参数:

>>> parser.add_argument('bar')

parse_args()运⾏时,会⽤'-'来认证可选参数,剩下的即为位置参数。

>>> parser = argparse.ArgumentParser(prog='PROG')>>> parser.add_argument('-f', '--foo')>>> parser.add_argument('bar')>>> parser.parse_args(['BAR'])Namespace(bar='BAR', foo=None)

>>> parser.parse_args(['BAR', '--foo', 'FOO'])Namespace(bar='BAR', foo='FOO')>>> parser.parse_args(['--foo', 'FOO'])usage: PROG [-h] [-f FOO] barPROG: error: too few arguments

解析时没有位置参数就会报错了。action默认为store

store_const,值存放在const中:

>>> parser = argparse.ArgumentParser()

>>> parser.add_argument('--foo', action='store_const', const=42)>>> parser.parse_args('--foo'.split())Namespace(foo=42)

store_true和store_false,值存为True或False:

>>> parser = argparse.ArgumentParser()

>>> parser.add_argument('--foo', action='store_true')>>> parser.add_argument('--bar', action='store_false')>>> parser.add_argument('--baz', action='store_false')>>> parser.parse_args('--foo --bar'.split())Namespace(bar=False, baz=True, foo=True)

append:存为列表

>>> parser = argparse.ArgumentParser()

>>> parser.add_argument('--foo', action='append')>>> parser.parse_args('--foo 1 --foo 2'.split())Namespace(foo=['1', '2'])

append_const:存为列表,会根据const关键参数进⾏添加:

>>> parser = argparse.ArgumentParser()

>>> parser.add_argument('--str', dest='types', action='append_const', const=str)>>> parser.add_argument('--int', dest='types', action='append_const', const=int)>>> parser.parse_args('--str --int'.split())

Namespace(types=[, ])

count:统计参数出现的次数

>>> parser = argparse.ArgumentParser()

>>> parser.add_argument('--verbose', '-v', action='count')>>> parser.parse_args('-vvv'.split())Namespace(verbose=3)

version:版本

>>> import argparse

>>> parser = argparse.ArgumentParser(prog='PROG')

>>> parser.add_argument('--version', action='version', version='%(prog)s 2.0')>>> parser.parse_args(['--version'])PROG 2.0

nargs

命令⾏参数的个数,⼀般使⽤通配符表⽰,其中,'?'表⽰只⽤⼀个,'*'表⽰0到多个,'+'表⽰⾄少⼀个值可以为整数N(N个),*(任意多个),+(⼀个或更多)

>>> parser = argparse.ArgumentParser()>>> parser.add_argument('--foo', nargs='*')>>> parser.add_argument('--bar', nargs='*')>>> parser.add_argument('baz', nargs='*')

>>> parser.parse_args('a b --foo x y --bar 1 2'.split())Namespace(bar=['1', '2'], baz=['a', 'b'], foo=['x', 'y'])

值为?时,⾸先从命令⾏获得参数,若没有则从const获得,然后从default获得:

>>> parser = argparse.ArgumentParser()

>>> parser.add_argument('--foo', nargs='?', const='c', default='d')>>> parser.add_argument('bar', nargs='?', default='d')>>> parser.parse_args('XX --foo YY'.split())Namespace(bar='XX', foo='YY')

>>> parser.parse_args('XX --foo'.split())Namespace(bar='XX', foo='c')>>> parser.parse_args(''.split())Namespace(bar='d', foo='d')

获取参数顺序:命令⾏-->const-->default更常⽤的情况是允许参数为⽂件:

>>> parser = argparse.ArgumentParser()

>>> parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),... default=sys.stdin)

>>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),... default=sys.stdout)

>>> parser.parse_args(['input.txt', 'output.txt'])

Namespace(infile=, outfile=)>>> parser.parse_args([])

Namespace(infile=', mode 'r' at 0x...>, outfile=', mode 'w' at 0x...>)

const保存⼀个常量default

默认值,上⾯刚介绍过choices可供选择的值

>>> parser = argparse.ArgumentParser(prog='doors.py')

>>> parser.add_argument('door', type=int, choices=range(1, 4))>>> print(parser.parse_args(['3']))Namespace(door=3)

>>> parser.parse_args(['4'])usage: doors.py [-h] {1,2,3}

doors.py: error: argument door: invalid choice: 4 (choose from 1, 2, 3)

type

参数的类型,默认是字符串string类型,还有float、int等类型required

是否必选desk可作为参数名

>>> parser = argparse.ArgumentParser()>>> parser.add_argument('--foo', dest='bar')>>> parser.parse_args('--foo XXX'.split())Namespace(bar='XXX')

help

和ArgumentParser⽅法中的参数作⽤相似,出现的场合也⼀致

解析参数的⼏种写法

最常见的空格分开:

>>> parser = argparse.ArgumentParser(prog='PROG')>>> parser.add_argument('-x')>>> parser.add_argument('--foo')>>> parser.parse_args('-x X'.split())Namespace(foo=None, x='X')

>>> parser.parse_args('--foo FOO'.split())Namespace(foo='FOO', x=None)

长选项⽤=分开

>>> parser.parse_args('--foo=FOO'.split())Namespace(foo='FOO', x=None)

短选项可以写在⼀起:

>>> parser.parse_args('-xX'.split())Namespace(foo=None, x='X')

parse_args()⽅法的返回值为namespace,可以⽤vars()内建函数化为字典:

>>> parser = argparse.ArgumentParser()>>> parser.add_argument('--foo')

>>> args = parser.parse_args(['--foo', 'BAR'])>>> vars(args){'foo': 'BAR'}

例⼦

例⼦1:

import argparse

def parse_args():

description = usage: %prog [options] poetry-fileThis is the Slow Poetry Server, blocking edition.Run it like this: python slowpoetry.py

If you are in the base directory of the twisted-intro package,you could run it like this:

python blocking-server/slowpoetry.py poetry/ecstasy.txtto serve up John Donne's Ecstasy, which I know you want to do.

parser = argparse.ArgumentParser(description = description)

help = The addresses to connect.

parser.add_argument('addresses',nargs = '*',help = help) help = The filename to operate on.Default is poetry/ecstasy.txt parser.add_argument('filename',help=help)

help = The port to listen on. Default to a random available port. parser.add_argument('-p',--port', type=int, help=help) help = The interface to listen on. Default is localhost.

parser.add_argument('--iface', help=help, default='localhost') help = The number of seconds between sending bytes.

parser.add_argument('--delay', type=float, help=help, default=.7) help = The number of bytes to send at a time.

parser.add_argument('--bytes', type=int, help=help, default=10) args = parser.parse_args(); return args

if __name__ == '__main__': args = parse_args()

for address in args.addresses:

print 'The address is : %s .' % address

print 'The filename is : %s .' % args.filename print 'The port is : %d.' % args.port

print 'The interface is : %s.' % args.iface

print 'The number of seconds between sending bytes : %f'% args.delay print 'The number of bytes to send at a time : %d.' % args.bytes

例⼦2

有⼀道⾯试题:编写⼀个脚本main.py,使⽤⽅式如下:main.py -u -d 'a=1,b=2,c=3' -o /tmp/index.html

功能要求:打开-u指定的页⾯,将页⾯中所有的链接后⾯增加参数a=1&b=2&c=3(需要考虑链接中已经存在指定的参数的问题), 然后保存到-o指定的⽂件中。

import argparseimport urllib

from pyquery import PyQuery as pqdef getArgs():

parse=argparse.ArgumentParser() parse.add_argument('-u',type=str) parse.add_argument('-d',type=str) parse.add_argument('-o',type=str) args=parse.parse_args() return vars(args)

def urlAddQuery(url,query): query=query.replace(',','&') if '?' in url:

return url.replace('?','?'+query+'&') else:

return url+'?'+querydef getHref(): args=getArgs() url=args['u']

query=args['d'].strip(\"\\'\") fileName=args['o'] doc=pq(url=url)

with open(fileName,'w') as f: for a in doc('a'): a=pq(a)

href=a.attr('href') if href:

newurl=urlAddQuery(href,query) f.write(newurl+'\\n')

if __name__=='__main__': getHref()

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sarr.cn 版权所有 赣ICP备2024042794号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务