TL; DR
使用nargs
選項或action
選項的'append'
設置(取決於您希望用戶界面表現)。
NARGS
parser.add_argument('-l','--list', nargs='+', help='<Required> Set flag', required=True)
# Use like:
# python arg.py -l 1234 2345 3456 4567
nargs='+'
需要1個或多個參數,nargs='*'
取零個或多個。
追加
parser.add_argument('-l','--list', action='append', help='<Required> Set flag', required=True)
# Use like:
# python arg.py -l 1234 -l 2345 -l 3456 -l 4567
隨着append
您提供的選項多次以建立名單。
請勿使用type=list
!!! - 您可能不想使用type=list
和。永遠。
讓我們來看看更詳細的一些不同的方式,可能會嘗試這樣做,最終的結果。
import argparse
parser = argparse.ArgumentParser()
# By default it will fail with multiple arguments.
parser.add_argument('--default')
# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)
# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')
# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')
# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)
# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')
# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
if value is not None:
print(value)
下面是輸出,你可以期望:
$ python arg.py --default 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567
$ python arg.py --list-type 1234 2345 3456 4567
...
arg.py: error: unrecognized arguments: 2345 3456 4567
$ # Quotes won't help here...
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']
$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]
$ # Negative numbers are handled perfectly fine out of the box.
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]
$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
外賣:
- 使用
nargs
或action='append'
nargs
可以更簡單˚F從用戶的角度來看,但它可能是不直觀的,如果有位置參數,因爲不能說出什麼應該是一個位置參數和屬於nargs
;如果您有位置參數,則action='append'
可能最終成爲更好的選擇。
- 以上僅在給出
nargs
時才爲真'*'
,'+'
或'?'
。如果你提供一個整數(例如4
),那麼nargs
和位置參數的混合選項就沒有問題,因爲會準確知道該選項的期望值。
- 不要使用命令行報價
- 不要使用
type=list
,因爲它會返回列表
- 出現這種情況的一個列表,因爲引擎蓋下使用值
type
脅迫每個人給出的論點你選擇的type
,而不是所有參數的聚合。
- 可以使用
type=int
(或其他)來獲得整數的列表(或其他)
:我不一般的意思..我的意思是用引號到通過一個列表是不是你想要的。
怎麼樣的字符串列表?這會將多個字符串參數(「wassup」,「something」和「else」)轉換爲如下所示的列表列表:[['w','a','s','s','u' ,'','','','','','','','','','','',' l','s','e']] – rd108
@ rd108不,不。如圖所示,這個'add_argument'方法將所有內容解釋爲字符串,包括數字。您必須添加額外的東西才能獲得這些列表清單。我只是複製並粘貼了我發佈的內容,並沒有得到列表清單。你怎麼調用'add_argument'? – SethMMorton
@ rd108我明白了,我敢打賭你正在使用'type = list'選項。不要使用它。這將字符串轉換爲列表,並因此列出了列表。 – SethMMorton