python標準庫之argparse模塊

一圖概解:

 

 

1、簡介:

 

argparse是python用於解析命令行參數和選項的標準模塊,用於代替已通過時的optparse模塊。java

 

argparse模塊的做用是用於解析命令行參數python

 

2、使用步驟:

 

1:import argparsedom

 

2:parser = argparse.ArgumentParser()ui

 

3:parser.add_argument()this

 

4:parser.parse_args()spa

 

逐句解釋:.net

一、首先導入該模塊;命令行

二、而後建立一個解析對象;code

三、而後向該對象中添加你要關注的命令行參數和選項,每個add_argument方法對應一個你要關注的參數或選項;orm

四、最後調用parse_args()方法進行解析;解析成功以後便可使用,下面簡單說明一下步驟2和3。

 

 

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參數,如上。

 

4、方法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方法中的參數做用類似,出現的場合也一致(參數做用解釋  add_argument("a", help="params means"))

 

最經常使用的地方就是這些,其餘的能夠參考官方文檔。下面給出一個例子,基本包括了常見的情形:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 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.

 

參考:https://blog.csdn.net/qq_19924321/article/details/79882713

相關文章
相關標籤/搜索