简短回答
Use the nargs
选项或'append'
设置的action
选项(取决于您希望用户界面的行为方式)。
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='*'
需要零个或多个。
append
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
With append
您多次提供该选项来构建列表。
不要使用type=list
!!!- 可能在任何情况下您都不想使用type=list
with argparse
. Ever.
长答案
让我们更详细地了解一些可能尝试执行此操作的不同方法以及最终结果。
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']
要点:
- Use
nargs
or action='append'
-
nargs
从用户的角度来看可能更直接,但如果存在位置参数,则可能会不直观,因为argparse
无法区分什么应该是位置参数以及什么属于nargs
;如果你有位置参数那么action='append'
可能最终是一个更好的选择。
- 上述仅在以下情况下成立:
nargs
给出'*'
, '+'
, or '?'
。如果您提供一个整数(例如4
)那么将选项与nargs
和位置参数,因为argparse
将确切地知道该选项期望有多少个值。
- Don't use quotes on the command line1
- Don't use
type=list
, as it will return a list of lists
- 发生这种情况是因为在幕后
argparse
使用的值type
胁迫每个人给出的论点你是你所选择的type
,不是所有参数的总和。
- 您可以使用
type=int
(或其他)获取整数列表(或其他)
1: I don't mean in general.. I mean using quotes to pass a list to argparse
is not what you want.