首页 文章

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

提问于
浏览
253

我试图将列表作为参数传递给命令行程序 . 是否有argparse选项将列表作为选项传递?

parser.add_argument('-l', '--list',
                      type=list, action='store',
                      dest='list',
                      help='<Required> Set flag',
                      required=True)

脚本调用如下

python test.py -l "265340 268738 270774 270817"

7 回答

  • 1

    也许是最简单的答案

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("-l", "--tolist", help="input to list", action="store_true")
    parser.add_argument("newlist", type=str, help="generate a list")
    args = parser.parse_args()
    if args.tolist:
        print(args.newlist.split(" "))
    
  • 497

    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='*' 需要零或更多 .

    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
    

    使用 append ,您可以多次提供选项以构建列表 .

    Don't use type=list!!! - 您可能不希望将 type=listargparse 一起使用 . 永远 .


    让我们以一些可能尝试这样做的不同方式以及最终结果来详细了解 .

    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']
    

    小贴士:

    • 使用 nargsaction='append'
      从用户的角度来看,

    • nargs 可以更直接,但如果存在位置参数,它可能是不直观的,因为 argparse 不能告诉什么应该是位置参数,什么属于 nargs ;如果你有位置参数,那么 action='append' 可能最终成为一个更好的选择 .

    • 以上仅在 nargs 被赋予 '*''+''?' 时才为真 . 如果您提供整数(例如 4 ),那么将选项与 nargs 和位置参数混合没有问题,因为 argparse 将准确知道该选项需要多少个值 .

    • 不要在命令行1上使用引号

    • 不要使用 type=list ,因为它会返回列表列表

    • 发生这种情况是因为在引擎盖下 argparse 使用 type 的值来强制给出您所选择的 type 给定参数,而不是所有参数的聚合 .

    • 您可以使用 type=int (或其他)获取整数列表(或其他)


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

  • 42

    我更喜欢传递一个分隔的字符串,我稍后会在脚本中解析它 . 原因是:列表可以是任何类型 intstr ,有时使用 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(',')]
    

    然后,

    python test.py -l "265340,268738,270774,270817" [other arguments]
    

    要么,

    python test.py -l 265340,268738,270774,270817 [other arguments]
    

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

  • 3

    除了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')
    
  • 2

    如果您打算使用多个参数进行单个切换,则使用 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='list',     # store in 'list'.
        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(' '))
    

    产生

    Namespace(list=[123, 234, 345, 456])
    Namespace(list=[456])  # Attention!
    

    如果多次指定相同的参数,则默认操作( 'store' )将替换现有数据 .

    另一种方法是使用 append 动作:

    a = argparse.ArgumentParser()
    a.add_argument(
        '-l', '--list',  # either of this switches
        type=int,        # /parameters/ are ints
        dest='list',     # store in 'list'.
        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(' '))
    

    哪个产生

    Namespace(list=[123, 234, 345, 456])
    

    或者,您可以编写自定义处理程序/操作来解析以逗号分隔的值,以便您可以执行此操作

    -l 123,234,345 -l 456
    
  • 11

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

    这将允许:

    $ ./tool --list "[1,2,3,4]"
    
  • 1

    在argparse的add_argument方法中使用 nargs parameter

    如果我没有传递任何显式参数,我会使用nargs ='' as an add_argument parameter. I specifically used nargs=''来选择默认值

    包含代码段作为示例:

    Example: temp_args1.py

    请注意:下面的示例代码是用python3编写的 . 通过更改print语句格式,可以在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))
    

    注意:我正在收集存储在列表中的多个字符串参数 - opts.alist如果需要整数列表,请将parser.add_argument上的type参数更改为int

    Execution Result:

    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']
    

相关问题