argparse模塊爲python3中處理命令行參數的庫
命令行參數分爲位置參數和可選參數, 例:
ls home #位置參數
ls -l #可選參數
簡單示例, 主要有三個步驟
- 創建 ArgumentParser() 對象
- 調用 add_argument() 方法添加參數
- 使用 parse_args() 解析添加的參數
位置參數
test.py
文件
import argparse
parse = argparse.ArgumentParser()
parse.add_argument('test') #添加參數 test
args = parse.parse_args()
print(args)
執行命令 python test.py -h
usage: test.py [-h] test
positional arguments:
test
optional arguments:
-h, --help show this help message and exit
# 可以看到多了一個位置參數test
執行命令 python test.py 123
Namespace(test='123')
# 已經成功賦值, 默認爲字符串
可選參數
test.py
文件
import argparse
parse = argparse.ArgumentParser()
parse.add_argument('-l') #添加可選參數 -l
args = parse.parse_args()
print(args)
執行命令 python test.py -h
usage: test.py [-h] [-l L]
optional arguments:
-h, --help show this help message and exit
-l L
# 可以看到多了一個位置參數 -l
執行命令 python test.py -l 123
Namespace(l='123')
# 已經成功賦值, 默認爲字符串
若 parse.add_argument('-l', '--list')
執行命令 python test.py -l 123
Namespace(list='123')
# 可選參數兩種調用方法, l和list都能用, 最後參數爲list
add_argument 方法常用參數
ArgumentParser.add_argument(name or flags…[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
help – 參數的幫助信息
parse.add_argument('-l', '--list', help='help~help~')
# python test.py -h
usage: test.py [-h] [-l LIST]
optional arguments:
-h, --help show this help message and exit
-l LIST, --list LIST help~help~
type – 命令行參數應該被轉換成的類型
parse.add_argument('-l', '--list', type=int)
# python test.py -l 123
Namespace(list=123)
# python test.py -l test
usage: test.py [-h] [-l LIST]
test.py: error: argument -l/--list: invalid int value: 'test'
(錯誤, 提示必須輸入整數)
choices – 取值範圍
parse.add_argument('-l', '--list', type=int, choices=[0,1,2])
# python test.py -l 1
Namespace(list=1)
# python test.py -l 5
usage: test.py [-h] [-l {0,1,2}]
test.py: error: argument -l/--list: invalid choice: 5 (choose from 0, 1, 2)
dest – 解析後的參數名稱
parse.add_argument('-l', '--list', dest='test')
# python test.py -l 123
Namespace(test='123')
default – 默認值
parse.add_argument('-l', '--list', type=int, default=0)
# python test.py -l 123
Namespace(list=123)
# python test.py
Namespace(list=0)
required – 可選參數是否可以省略, 僅針對可選參數
parse.add_argument('-l', '--list', type=int, required=True)
# python test.py -l 123
Namespace(list=123)
# python test.py
usage: test.py [-h] -l LIST
test.py: error: the following arguments are required: -l/--list
nargs – 應該讀取的命令行參數個數, 可以是具體的數字, 或者是 ? 號(0或者1個參數
),或者是 * 號(0或多個參數
), 或者是 + 號(1或多個參數
)
parse.add_argument('-l', '--list', type=int, nargs='?')
# python test.py -l 1
Namespace(list=1)
# python test.py -l 1 2
usage: test.py [-h] [-l [LIST]]
test.py: error: unrecognized arguments: 2
action=‘store_true’ / action=‘store_false’ – 保存相應的布爾值, 這兩個動作被用於實現布爾開關
parse.add_argument('-l', '--list', action='store_true')
# python test.py
Namespace(list=False)
# python test.py -l
Namespace(list=True)
互斥參數
import argparse
parse = argparse.ArgumentParser()
group = parse.add_mutually_exclusive_group()
group.add_argument('-x')
group.add_argument('-y')
args = parse.parse_args()
print(args)
# python test.py -x 2
Namespace(x='2', y=None)
# python test.py -y 3
Namespace(x='None', y='3')
#python test.py -x 2 -y 3
usage: test.py [-h] [-x X | -y Y]
test.py: error: argument -y: not allowed with argument -x