Repository: pytest-dev/pytest
Pull request: Fix option error
Close #10592

if the cmdline contains both the `--cache-show` and `--help` option will cause the internal error .  But if contains the `--collect-only` and `--help` will just output the help message.

the root cause is inside `pytest_cmdline_main` of `cacheprovider` , it will really execute the `wrap_session` , but it will raise   exception due to missing variable assignment .  so add the condition express to avoid the conflict .


Problem statement:
py.test --cache-show --help triggers an internal error
Hi,

When using `py.test-3 --cache-show --help` or `python3 -m pytest --cache-show --help`, I can reliably trigger an internal error.

The full stack trace is:

```
========================================== test session starts ==========================================
platform linux -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0+repack
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/lib/python3/dist-packages/_pytest/main.py", line 266, in wrap_session
INTERNALERROR>     config.hook.pytest_sessionstart(session=session)
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/usr/lib/python3/dist-packages/_pytest/terminal.py", line 710, in pytest_sessionstart
INTERNALERROR>     lines = self.config.hook.pytest_report_header(
INTERNALERROR>   File "/usr/lib/python3/dist-packages/_pytest/config/compat.py", line 67, in fixed_hook
INTERNALERROR>     return hook(**kw)
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/usr/lib/python3/dist-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/usr/lib/python3/dist-packages/_pytest/terminal.py", line 732, in pytest_report_header
INTERNALERROR>     if config.invocation_params.dir == config.rootpath and config.args == testpaths:
INTERNALERROR> AttributeError: 'Config' object has no attribute 'args'
```


I am using pytest 7.1.2 from Debian (deb version is 7.1.2-4) on python3 (3.10.9)

Hints from issue discussion:
looks like it hasn't handle the exception when fail to parse the options in `config/__init__.py` ,line 1370
```python
except PrintHelp:
    pass
```

so it will cause the `config.args_source` is not assigned
one suggestion is just raise the `PrintHelp` exception to the outside caller , handle it in the function `pytest_cmdline_parse` of `confgi/__init__.py` ,  add a new error handle for printing the helper function

```python
        except PrintHelp:
            self._parser._getparser().print_help()
            sys.stdout.write(
                "\nNOTE: displaying only minimal help due to UsageError.\n\n"
            )
            raise UsageError
```
@markshao thanks for taking a look, feel free to open a PR if you would like to work on this, we would really appreciate it. :+1: 

Btw you can navigate to the file you want here on GitHub (https://github.com/pytest-dev/pytest/blob/main/src/_pytest/config/__init__.py), select the lines you want to highlight, then select the `Copy permalink` option by clicking on the `...` button, this will produce a permalink which expands the code automatically:

https://github.com/pytest-dev/pytest/blob/b31db4809be18b43ba714b80b10285b463cc9315/src/_pytest/config/__init__.py#L1350-L1372
fine, I will work on that

Commit message: fix pre-commit

Patch to review:
diff --git a/AUTHORS b/AUTHORS
index a4ca9926760..4a87b89e74c 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -377,6 +377,7 @@ Xixi Zhao
 Xuan Luong
 Xuecong Liao
 Yoav Caspi
+Yuliang Shao
 Yusuke Kadowaki
 Yuval Shimon
 Zac Hatfield-Dodds
diff --git a/changelog/10592.bugfix.rst b/changelog/10592.bugfix.rst
new file mode 100644
index 00000000000..6dc9705174a
--- /dev/null
+++ b/changelog/10592.bugfix.rst
@@ -0,0 +1 @@
+Fix the bug "--cache-show --help" triggers an internal error
diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py
index 777c1b0b05a..c236dd4176b 100755
--- a/src/_pytest/cacheprovider.py
+++ b/src/_pytest/cacheprovider.py
@@ -32,7 +32,6 @@
 from _pytest.python import Package
 from _pytest.reports import TestReport
 
-
 README_CONTENT = """\
 # pytest cache directory #
 
@@ -492,7 +491,7 @@ def pytest_addoption(parser: Parser) -> None:
 
 
 def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
-    if config.option.cacheshow:
+    if config.option.cacheshow and not config.option.help:
         from _pytest.main import wrap_session
 
         return wrap_session(config, cacheshow)


Code review comments:
diff_hunk: @@ -0,0 +1 @@
+Fix the bug "--cache-show --help" triggers an internal error
path: changelog/10592.bugfix.rst
text: @user1:
```suggestion
Fixed crash if `--cache-show` and `--help` are passed at the same time.
```