python之argparse模块

一、简介

argparse是python用于解析命令行参数和选项的标准模块,用于代替已经过时的optparse。

二、使用步骤

(1)import argparse #导入argparse模块

(2)parser = argparse.ArgumentParser() #创建一个解析对象

(3)parser.add_argument() #往该对象中添加你要关注的命令行参数和选项,一个add_argument方法对应一个参数或选项

(4)parser.parse_args() #调用parse_args()方法进行解析


1)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解释器其实也是调用了print_help()方法)时,会打印这些描述信息,一般只需要传递description参数。


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

其中,name or flags:命令行参数或选项,如果命令行参数没有给定,且没有设置default,则出错;如果是选项的话,则设置为None

nargs:命令行参数个数,一般使用通配符表示,如‘?’表示只用一个,‘*’表示0~多个,‘+’表示至少一个

default:默认值

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

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


以下为一参考实例:

import argparse
 
def parse_args():
    description = usage: %prog [options] poetry-file
 
This is the Slow Poetry Server, blocking edition.
Run it like this:
 
  python slowpoetry.py <path-to-poetry-file>
 
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.txt
 
to 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</path-to-poetry-file>


如运行以下语句:

python test.py --port 1000 --delay 1.2 127.0.0.1 172.16.55.67


输出为:

The Address i: 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值