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
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> |
輸出為:
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.
相關文章
- python argparse(引數解析模組)Python
- Python 中argparse模組的使用Python
- Python學習之模組Python
- Python學習之常用模組Python
- argparse學習筆記筆記
- 【python】python 模組學習之--FabricPython
- 【python】python 模組學習之--pexpectPython
- Python命令列引數解析模組argparsePython命令列
- python中的argparse模組(引數解析)Python
- Python學習之 datetime模組Python
- Python學習之模組與包Python
- python3.x中argparse模組詳解Python
- [Python模組學習] glob模組Python
- Python學習之如何引用Python自定義模組?Python
- 模組學習之hashlib模組
- nginx學習之模組Nginx
- Python學習——xml模組PythonXML
- Python time模組學習Python
- python模組學習:CookiePythonCookie
- Python模組學習:atexitPython
- Python模組學習:fileinputPython
- python開發學習之如何更好的引用Python模組?Python
- 模組學習之logging模組
- 使用argparse模組新增命令列引數命令列
- Python學習——hashlib模組Python
- Python學習——shelve模組Python
- python 各種模組學習Python
- python optparse模組學習薦Python
- Python模組學習:urllibPython
- Python模組學習:datetimePython
- node模組化之require學習UI
- node 核心模組學習之 Buffer
- 命令列引數解析模組argparse的使用命令列
- python基礎學習16—-模組Python
- 學習Python的urllib模組Python
- Python學習——configparser模組Python
- python模組學習:anydbm, shelvePython
- 小豬的Python學習之旅 —— 12.Python併發之queue模組Python