python的argparse 模块_python的argparse模块

一、简介:

argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse模块。argparse模块的作用是用于解析命令行参数,例如python parseTest.py input.txt output.txt --user=name --port=8080。

二、使用步骤:

1:import argparse

2:parser = argparse.ArgumentParser()

3:parser.add_argument()

4:parser.parse_args()

解释:首先导入该模块;然后创建一个解析对象;然后向该对象中添加你要关注的命令行参数和选项,每一个add_argument方法对应一个你要关注的参数或选项;最后调用parse_args()方法进行解析;解析成功之后即可使用,下面简单说明一下步骤2和3。

三、方法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参数,如上。

四、方法add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

其中:

name or flags:命令行参数名或者选项,如上面的address或者-p,--port.其中命令行参数如果没给定,且没有设置defualt,则出错。但是如果是选项的话,则设置为None

nargs:命令行参数的个数,一般使用通配符表示,其中,'?'表示只用一个,'*'表示0到多个,'+'表示至少一个

default:默认值

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

help:和ArgumentParser方法中的参数作用相似,出现的场合也一致

最常用的地方就是这些,其他的可以参考官方文档。下面给出一个例子,基本包括了常见的情形:

1 importargparse2

3 defparse_args():4 description = usage: %prog [options] poetry-file5

6 This isthe Slow Poetry Server, blocking edition.7 Run it like this:8

9 python slowpoetry.py

10

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

14 python blocking-server/slowpoetry.py poetry/ecstasy.txt15

16 to serve up John Donne's Ecstasy, which I know you want to do.

17

18

19 parser = argparse.ArgumentParser(description =description)20

21 help =The addresses to connect.22 parser.add_argument('addresses',nargs = '*',help =help)23

24 help = The filename to operate on.Default is poetry/ecstasy.txt25 parser.add_argument('filename',help=help)26

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

29

30 help = The interface to listen on. Default islocalhost.31 parser.add_argument('--iface', help=help, default='localhost')32

33 help =The number of seconds between sending bytes.34 parser.add_argument('--delay', type=float, help=help, default=.7)35

36 help =The number of bytes to send at a time.37 parser.add_argument('--bytes', type=int, help=help, default=10)38

39 args =parser.parse_args();40 returnargs41

42 if __name__ == '__main__':43 args =parse_args()44

45 for address inargs.addresses:46 print 'The address is : %s .' %address47

48 print 'The filename is : %s .' %args.filename49 print 'The port is : %d.' %args.port50 print 'The interface is : %s.' %args.iface51 print 'The number of seconds between sending bytes : %f'%args.delay52 print 'The number of bytes to send at a time : %d.' % args.bytes

运行该脚本:python test.py --port 10000 --delay 1.2 127.0.0.1 172.16.55.67 poetry/ecstasy.txt

输出为:

The address is : 127.0.0.1 .

The address is : 172.16.55.67 .

The filename is : poetry/ecstasy.txt .

The port is : 10000.

The interface is : localhost.

The number of seconds between sending bytes : 1.200000

The number of bytes to send at a time : 10.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python argparse 模块Python 标准库中用于解析命令行参数和选项的模块。它提供了一种简单而灵活的方法来处理命令行参数,并且可以自动生成格式漂亮的帮助文档。 argparse 中有许多参数选项,其中一个是 type。type 参数指定了命令行参数的数据类型。当我们使用 argparse 解析命令行参数时,它会将字符串类型的参数转换为指定的类型。例如,如果我们指定 type=int,那么 argparse 将会把输入的字符串转换为整数类型。 argparse 提供了一些内置的类型函数,如 int、float、str、bool 等,也可以自定义类型函数。下面是一个示例,说明如何使用 type 参数来指定参数的数据类型: ```python import argparse parser = argparse.ArgumentParser() parser.add_argument('--name', type=str, help='name of the user') parser.add_argument('--age', type=int, help='age of the user') args = parser.parse_args() print(args.name) print(args.age) ``` 在上面的示例中,我们使用 add_argument() 方法添加了两个参数:--name 和 --age。type 参数分别指定了它们的数据类型为 str 和 int。 当我们在命令行中输入参数时,argparse 会自动将字符串类型的参数转换为指定的类型。例如,我们执行以下命令: ```bash python test.py --name Tom --age 25 ``` argparse 将会把 name 参数解析为字符串类型的 'Tom',将 age 参数解析为整数类型的 25。 总之,argparse 的 type 参数让我们可以轻松地控制命令行参数的数据类型,使得命令行参数处理变得更加简单和方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值