pytest: --help broken when there's a required option set in pytest_addoption()
Creating this after chatting on #pylib. When adding command line options using pytest_addoption(), if any of the arguments are set to be required (e.g. with required=True) it breaks the ability to run --help (unless you also provide the required option). This is in contrast to how a vanilla argparse config works.
Example of this not occurring with argparse:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', required=True)
args = parser.parse_args()
--help still works without the required argument, but executing the file does not (expected behavior):
$ python vanilla.py --help
usage: vanilla.py [-h] --foo FOO
optional arguments:
-h, --help show this help message and exit
--foo FOO
$ python vanilla.py
usage: vanilla.py [-h] --foo FOO
vanilla.py: error: the following arguments are required: --foo
Example of this not working as expected with pytest:
$ cat conftest.py
def pytest_addoption(parser):
parser.addoption("--foo", required=True)
--help is broken, unless you also provide the required option:
$ pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: the following arguments are required: --foo
$ pytest --help --foo bar
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
...
My environment:
$ pip list
pip (8.1.2)
py (1.4.31)
pytest (3.0.3)
setuptools (20.10.1)
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.10.5
BuildVersion: 14F1912
- Include a detailed description of the bug or suggestion
-
pip listof the virtual environment you are using - pytest and operating system versions
- Minimal example if possible
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (15 by maintainers)
Commits related to this issue
- Fix --help with required options This works by adding an argparse Action that will raise an exception so that we will skip the rest of the argument parsing to prevent argparse from quitting due to mi... — committed to segevfiner/pytest by segevfiner 7 years ago
- Fix --help with required options This works by adding an argparse Action that will raise an exception in order to skip the rest of the argument parsing. This prevents argparse from quitting due to mi... — committed to segevfiner/pytest by segevfiner 7 years ago
Thanks for the fix @segevfiner, seems to work great!