gpt4 book ai didi

python - pytest 是否限制自定义命令行参数中的所有小写选项?

转载 作者:行者123 更新时间:2023-12-04 16:45:31 25 4
gpt4 key购买 nike

我正在尝试从 argparse.ArgumentParser 移植一些命令行参数实例到等效的 pytest conftest.py文件使用 pytest 的 pytest_addoption
查看 pytest 帮助功能,我发现 12 个使用过的单字符短选项,即 k, m, x, c, s, v, q, r, l, h, p, o

MacBook-Pro:~ user$ pytest --help | grep " -[a-z]"
-k EXPRESSION only run tests which match the given substring
-k 'test_method or test_other' matches all test
'test_method' or 'test_other', while -k 'not
-m MARKEXPR only run tests matching given mark expression.
example: -m 'mark1 and not mark2'.
-x, --exitfirst exit instantly on first error or failed test.
-c file load configuration from `file` instead of trying to
-s shortcut for --capture=no.
-v, --verbose increase verbosity.
-q, --quiet decrease verbosity.
-r chars show extra test summary info as specified by chars
-l, --showlocals show locals in tracebacks (disabled by default).
-h, --help show help message and configuration info
-p name early-load given plugin (multi-allowed). To avoid
-o [OVERRIDE_INI [OVERRIDE_INI ...]], --override-ini=[OVERRIDE_INI [OVERRIDE_INI ...]]

然而,如果我尝试在该集合之外定义一个选项,我会收到以下异常
def pytest_addoption(parser):
parser.addoption('-b', '--build_special' )


File "/Library/Python/2.7/site-packages/_pytest/config/argparsing.py", line 72, in addoption
self._anonymous.addoption(*opts, **attrs)
File "/Library/Python/2.7/site-packages/_pytest/config/argparsing.py", line 305, in addoption
self._addoption_instance(option, shortupper=False)
File "/Library/Python/2.7/site-packages/_pytest/config/argparsing.py", line 315, in _addoption_instance
raise ValueError("lowercase shortoptions reserved")
ValueError: lowercase shortoptions reserved

无论我选择哪个角色,这似乎都是这种情况,这让我认为这只是行为受限的时期。

问题:pytest 是否限制短选项的任何和所有使用?

我有点困惑,因为当你查看相关的 pytest source
def addoption(self, *opts, **attrs):
""" register a command line option.
:opts: option names, can be short or long options.
:attrs: same attributes which the ``add_option()`` function of the
`argparse library
<http://docs.python.org/2/library/argparse.html>`_
accepts.
After command line parsing options are available on the pytest config
object via ``config.option.NAME`` where ``NAME`` is usually set
by passing a ``dest`` attribute, for example
``addoption("--long", dest="NAME", ...)``.
"""
self._anonymous.addoption(*opts, **attrs)

似乎暗示我可以使用与 argparse 的 add_option 完全相同的语法,这在 argparse documentation 中足够有趣。提到

Replace all optparse.OptionParser.add_option() calls with ArgumentParser.add_argument() calls.



和 add_argument documentation明确列出允许的单个标志选项:

The add_argument() method must know whether an optional argument, like -f or --foo, or a positional argument, like a list of filenames, is expected. The first arguments passed to add_argument() must therefore be either a series of flags, or a simple argument name. For example, an optional argument could be created like:

>>> parser.add_argument('-f', '--foo')

最佳答案

从pytest源代码...

/src/_pytest/config/argparsing.py

def _addoption_instance(self, option: "Argument", shortupper: bool = False) -> None:
if not shortupper:
for opt in option._short_opts:
if opt[0] == "-" and opt[1].islower():
raise ValueError("lowercase shortoptions reserved")
if self.parser:
self.parser.processoption(option)
self.options.append(option)

是的,他们限制所有小写的短选项

关于python - pytest 是否限制自定义命令行参数中的所有小写选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com