argparse.REMAINDER更改位置参数的行为 [英] argparse.REMAINDER changes the behavior of positional arguments

查看:94
本文介绍了argparse.REMAINDER更改位置参数的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果没有 argparse.REMAINDER ,则可选参数可以位于位置参数之前或之后:

Without argparse.REMAINDER, optional arguments can be in front of or after positional arguments:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-a')
parser.add_argument('b')

print(parser.parse_args('-a 1 2'.split()))  # Namespace(a='1', b='2')
print(parser.parse_args('2 -a 1'.split()))  # Namespace(a='1', b='2')

但是使用 argparse.REMAINDER 时,可选参数必须位于前面:

But with argparse.REMAINDER, optional arguments must be in front:

parser.add_argument('c', nargs=argparse.REMAINDER)

print(parser.parse_args('-a 1 2 3'.split()))  # Namespace(a='1', b='2', c=['3'])
print(parser.parse_args('2 -a 1 3'.split()))  # Namespace(a=None, b='2', c=['-a', '1', '3'])

使用 argparse.REMAINDER 时,如何正确解析最后一行?

How can I parse the last line correctly, while argparse.REMAINDER is used?

推荐答案

要添加到kabanus的答案中,可能需要了解一些有关如何解析参数的知识.

To add to kabanus's answer, it may help to know a bit about how arguments are parsed.

它遍历参数,首先查找位置,然后是可选的,然后是位置,...,

It iterates over the arguments, first looking for positionals, then optionals, then positionals, ...,

在定位步骤中,它尝试使用 nargs 作为主要因素来匹配尽可能多的匹配项.默认值为一个字符串(您的"b");'*'将与下一个可选内容( -a )匹配;但REMAINDER会忽略该约束并匹配到末尾.因此,位置"评估是贪婪的,而使用REMAINDER的评估尤其贪婪.

At the positionals step it tries to match as many as it can, using the nargs as a primary factor. The default is one string (your 'b'); '*' will match up to the next optional (the -a); but a REMAINDER ignores that constraint and matches to the end. So a 'positionals' evaluation is greedy, and one with REMAINDER is especially greedy.

因此,在'2 -a 1 3'情况下,首字母'2'可以匹配'b',其余字母可以匹配'c'.仅有一个位置"评估消耗了整个列表,包括"-a",并且完成了.

So in the '2 -a 1 3' case, the initial '2' can match 'b', and the rest can match 'c'. Just one 'positionals' evaluation consumes the whole list, including the '-a', and it is done.

文档示例显示以下内容:

The documentation example shows this:

>>> print(parser.parse_args('--foo B cmd --arg1 XX ZZ'.split()))
Namespace(args=['--arg1', 'XX', 'ZZ'], command='cmd', foo='B')

'-foo'被视为可选,但是'--arg1'是REMAINDER的一部分.在命令"之后立即填充"args".

The '--foo' is handled as optional, but the '--arg1' is part of the REMAINDER. 'args' is filled immediately after 'command'.

如果您想保留使用REMAINDER的时间,请将其设为可选的 add_argument('-c',nargs ='...').否则,您会受此positionals/optionals循环的支配.

If you want to retain control over when the REMAINDER is used, make it an optional, add_argument('-c',nargs='...'). Otherwise you are at the mercy of this positionals/optionals loop.

顺便说一句,子解析器使用 narg = argarse.PARSER 实现,这是'+ ...'的名称.就像REMAINDER一样,但是至少需要一个字符串(子解析器名称).它也消耗了路径中的所有东西.

By the way, subparsers are implemented with a narg=argarse.PARSER, which is the name for '+...'. It's like REMAINDER but requires at least one string (the subparser name). It too consumes everything in its path.

您可能想使用'*'而不是'-'来触发'消耗其他所有东西'.

Instead of REMAINDER you might want to use '*' and use '--' to trigger the 'consume everything else' action.

这篇关于argparse.REMAINDER更改位置参数的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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