argparse nargs="+"正在吃位置参数 [英] Argparse nargs="+" is eating positional argument

查看:21
本文介绍了argparse nargs="+"正在吃位置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的解析器配置的一个小节

Here's a subsection of my parser configuration

parser.add_argument(
    'infile', help="The file to be imported",
    type=argparse.FileType('r'), default=sys.stdin
)

parser.add_argument(
    '--carpark', nargs='+', dest='CarparkID', type=int, default=[],
    help="One or many carpark IDs"
)

然而,--carpark 参数似乎过于贪婪,并且会吃掉它后面的任何东西:

However, the --carpark argument seems to be too greedy and eats anything that follows it:

$ mycommand --carpark 17 ~/path-to-file
mycommand: error: argument --carpark: invalid int value: '/home/oli/path-to-file'

有什么好的方法可以解决这样的问题?我需要将整数 ID 列表传递到命令中,但还有一个位置文件(也可以是标准输入).

What's a good way around something like this? I need to pass a list of integer IDs into the command but also have a positional file (which can also be stdin).

是否有——例如——一个非贪婪的 nargs 选项,它只会解析尽可能多的有意义的内容?

Is there —for example— a non-greedy nargs option that will only parse as much of this as makes sense?

推荐答案

如果您想指定多个停车场 ID,我会执行以下两种操作之一,而不是使用 nargs='+':

If you want to specify multiple car park IDs, I would do one of two things instead of using nargs='+':

  1. 每个 ID 使用一次该选项 (mycommand --carpark 17 --carpark 18)

parser.add_argument('--carpark',
                    dest='carpark_ids',
                    type=int,
                    action='append',
                    default=[],
                    help="One carpark ID (can be used multiple times)"
)

  • 改为使用单个逗号分隔的参数 (mycommand --carpark 17,18)

    def intlist(s):
        rv = []
        for x in s.split(','):
            try:
                x = int(x)
            except ValueError:
                raise ArgumentTypeError("Non-integer carpark id {x}" % (x,))
            rv.append(x)
        return rv
    
    parser.add_argument('--carpark',
                        type=intlist,
                        dest='carpark_ids',
                        default=[],
                        help="One or more carpark IDs"
    )
    

    稍加工作,您可以修改它以允许多次使用 --carpark 将其所有值累积到一个列表中.

    With a little more work, you could modify this to allow multiple uses of --carpark to accumulate all its values into a single list.

    我不是特别喜欢的第三种选择是放弃位置参数,而是将其作为可选参数.(mycommand --carpark 17 18 --infile ~/path-to-file).

    A third alternative, one I'm not particularly fond of, is to abandon the positional argument, making it an optional argument instead. (mycommand --carpark 17 18 --infile ~/path-to-file).

    parser.add_argument('--infile',
                        help="The file to be imported",
                        type=argparse.FileType('r'),
                        default=sys.stdin
    )
    
    parser.add_argument('--carpark',
                        nargs='+',
                        dest='CarparkID',
                        type=int,
                        default=[],
                        help="One or many carpark IDs"
    )
    

    这篇关于argparse nargs="+"正在吃位置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆