我试图将列表作为参数传递给命令行程序.是否有argparse
选项可以将列表作为选项传递?
parser.add_argument('-l', '--list',
type=list, action='store',
dest='list',
help='<Required> Set flag',
required=True)
Run Code Online (Sandbox Code Playgroud)
脚本调用如下
python test.py -l "265340 268738 270774 270817"
Run Code Online (Sandbox Code Playgroud)
Set*_*ton 740
TL; DR
使用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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
随append
您提供多次选项以构建列表.
不要用type=list
!!! - 可能没有你想要使用的type=list
情况argparse
.永远.
让我们以一些可能尝试这样做的不同方式和最终结果来详细了解一下.
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)
Run Code Online (Sandbox Code Playgroud)
这是您可以期望的输出:
$ 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']
Run Code Online (Sandbox Code Playgroud)
小贴士:
nargs
或action='append'
nargs
从用户的角度来看可以更直接,但如果存在位置参数则可能不直观,因为argparse
无法分辨出什么应该是位置参数以及什么属于nargs
?如果你有位置参数,那么action='append'
最终可能会成为更好的选择.nargs
给出,或者'*'
,上述情况才适用.如果你提供一个整数(例如),那么混合选项和位置参数就没有问题,因为它将确切地知道该选项需要多少个值.'+'
'?'
4
nargs
argparse
type=list
,因为它将返回列表列表
argparse
使用的值type
来强制给出您所选择的参数的每个个体type
,而不是所有参数的聚合.type=int
(或其他)获取整数列表(或其他)1:我的意思并不一般......我的意思是使用引号来传递列表argparse
并不是你想要的.
doj*_*uba 66
我更喜欢传递一个分隔的字符串,我稍后会在脚本中解析它.原因是:列表可以是任何类型,int
或者str
有时使用nargs
我遇到问题,如果有多个可选参数和位置参数.
parser = ArgumentParser()
parser.add_argument('-l', '--list', help='delimited list input', type=str)
args = parser.parse_args()
my_list = [int(item) for item in args.list.split(',')]
Run Code Online (Sandbox Code Playgroud)
然后,
python test.py -l "265340,268738,270774,270817" [other arguments]
Run Code Online (Sandbox Code Playgroud)
要么,
python test.py -l 265340,268738,270774,270817 [other arguments]
Run Code Online (Sandbox Code Playgroud)
会很好的.分隔符也可以是一个空格,虽然可以在参数值周围强制引用,如问题中的示例所示.
Mar*_*oma 16
此外nargs
,choices
如果您事先知道列表,则可能需要使用:
>>> parser = argparse.ArgumentParser(prog='game.py')
>>> parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
>>> parser.parse_args(['rock'])
Namespace(move='rock')
>>> parser.parse_args(['fire'])
usage: game.py [-h] {rock,paper,scissors}
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
'paper', 'scissors')
Run Code Online (Sandbox Code Playgroud)
lun*_*ini 16
通过命令行处理传递列表(也包括字典)的一个好方法是使用json。
# parse_list.py
import argparse
import json
parser = argparse.ArgumentParser()
# note type arg, used to load json string
parser.add_argument('-l', '--list', type=json.loads)
args = parser.parse_args()
print(args.list)
Run Code Online (Sandbox Code Playgroud)
$ python parse_list.py -l "[265340, 268738, 270774, 270817]"
[265340, 268738, 270774, 270817]
Run Code Online (Sandbox Code Playgroud)
编辑:合并了Katu建议的改进,以删除单独的解析步骤。
在argparse的add_argument方法中使用nargs参数
我使用nargs =' '作为add_argument参数。如果我没有传递任何明确的参数,我专门在选项中使用nargs =' '来选择默认值
包括一个代码片段作为示例:
示例:temp_args1.py
请注意:以下示例代码是用python3编写的。通过更改打印语句的格式,可以在python2中运行
#!/usr/local/bin/python3.6
from argparse import ArgumentParser
description = 'testing for passing multiple arguments and to get list of args'
parser = ArgumentParser(description=description)
parser.add_argument('-i', '--item', action='store', dest='alist',
type=str, nargs='*', default=['item1', 'item2', 'item3'],
help="Examples: -i item1 item2, -i item3")
opts = parser.parse_args()
print("List of items: {}".format(opts.alist))
Run Code Online (Sandbox Code Playgroud)
注意:我正在收集存储在列表中的多个字符串参数-opts.alist如果要获取整数列表,请将parser.add_argument上的type参数更改为int
执行结果:
python3.6 temp_agrs1.py -i item5 item6 item7
List of items: ['item5', 'item6', 'item7']
python3.6 temp_agrs1.py -i item10
List of items: ['item10']
python3.6 temp_agrs1.py
List of items: ['item1', 'item2', 'item3']
Run Code Online (Sandbox Code Playgroud)
在add_argument()
,type
只是一个可调用对象,它接收字符串并返回选项值。
import ast
def arg_as_list(s):
v = ast.literal_eval(s)
if type(v) is not list:
raise argparse.ArgumentTypeError("Argument \"%s\" is not a list" % (s))
return v
def foo():
parser.add_argument("--list", type=arg_as_list, default=[],
help="List of values")
Run Code Online (Sandbox Code Playgroud)
这将允许:
$ ./tool --list "[1,2,3,4]"
Run Code Online (Sandbox Code Playgroud)
如果您打算使单个开关采用多个参数,那么您可以使用nargs='+'
. 如果您的示例“-l”实际上采用整数:
a = argparse.ArgumentParser()
a.add_argument(
'-l', '--list', # either of this switches
nargs='+', # one or more parameters to this switch
type=int, # /parameters/ are ints
dest='lst', # store in 'lst'.
default=[], # since we're not specifying required.
)
print a.parse_args("-l 123 234 345 456".split(' '))
print a.parse_args("-l 123 -l=234 -l345 --list 456".split(' '))
Run Code Online (Sandbox Code Playgroud)
生产
Namespace(lst=[123, 234, 345, 456])
Namespace(lst=[456]) # Attention!
Run Code Online (Sandbox Code Playgroud)
如果多次指定相同的参数,则默认操作 ('store'
) 会替换现有数据。
另一种方法是使用以下append
操作:
a = argparse.ArgumentParser()
a.add_argument(
'-l', '--list', # either of this switches
type=int, # /parameters/ are ints
dest='lst', # store in 'lst'.
default=[], # since we're not specifying required.
action='append', # add to the list instead of replacing it
)
print a.parse_args("-l 123 -l=234 -l345 --list 456".split(' '))
Run Code Online (Sandbox Code Playgroud)
哪个产生
Namespace(lst=[123, 234, 345, 456])
Run Code Online (Sandbox Code Playgroud)
或者您可以编写自定义处理程序/操作来解析逗号分隔的值,以便您可以执行以下操作
-l 123,234,345 -l 456
Run Code Online (Sandbox Code Playgroud)