用于传递列表作为选项的argparse选项

use*_*827 377 python argparse

我试图将列表作为参数传递给命令行程序.是否有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)

小贴士:

  • 使用nargsaction='append'
    • nargs从用户的角度来看可以更直接,但如果存在位置参数则可能不直观,因为argparse无法分辨出什么应该是位置参数以及什么属于nargs?如果你有位置参数,那么action='append'最终可能会成为更好的选择.
    • 只有nargs给出,或者'*',上述情况才适用.如果你提供一个整数(例如),那么混合选项和位置参数就没有问题,因为它将确切地知道该选项需要多少个值.'+''?'4nargsargparse
  • 不要在命令行1上使用引号
  • 不要使用type=list,因为它将返回列表列表
    • 发生这种情况是因为在引擎盖下argparse使用的值type来强制给出您所选择的参数的每个个体type,而不是所有参数的聚合.
    • 您可以使用type=int(或其他)获取整数列表(或其他)

1:我的意思并不一般......我的意思是使用引号来传递列表argparse并不是你想要的.

  • 字符串列表怎么样?这会将多个字符串参数("wassup","something"和"else")转换为如下所示的列表:[['w','a','s','s','u' ,'p'],['s','o','m','e','t','h','i','n','g'],['e',' l','s','e']] (3认同)
  • @ rd108我明白了,我打赌你正在使用`type = list`选项.不要使用它.这会将字符串转换为列表,从而列出列表. (3认同)

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)

会很好的.分隔符也可以是一个空格,虽然可以在参数值周围强制引用,如问题中的示例所示.

  • 您可以将`type`参数设置为`lambda s:[int(time)for item in s.split(',')]`而不是后处理`args.list`. (45认同)
  • @ chepner,是的,你是绝对正确的,它会更加pythonic - 只是一个小错字:`int(time)`应该是`int(item)`.我的例子是我通常做的简化版本,在那里我检查了许多其他东西而不是简单的处理.但是简单地回答这个问题,我也发现你的方式更优雅.. (11认同)
  • @chepner 的评论是一些严肃的忍者技能 +1 (2认同)

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列表解决方案

通过命令行处理传递列表(也包括字典)的一个好方法是使用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建议的改进,以删除单独的解析步骤。


Py_*_*ion 7

在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)


won*_*ice 6

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)


kfs*_*one 5

如果您打算使单个开关采用多个参数,那么您可以使用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)