Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

limit verbosity to supported range #1284

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1283.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crashes due to superfluous `-q ` flags by discarding exceeding values
17 changes: 13 additions & 4 deletions src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,20 @@ def get_command_parser() -> Tuple[argparse.ArgumentParser, Dict[str, argparse.Ar
default=0,
help=(
"Give less output. May be used multiple times corresponding to the"
" WARNING, ERROR, and CRITICAL logging levels."
" ERROR and CRITICAL logging levels. The count maxes out at 2."
),
)

shared_parser.add_argument("--verbose", "-v", action="count", default=0, help=("Give more output."))
shared_parser.add_argument(
"--verbose",
"-v",
action="count",
default=0,
help=(
"Give more output. May be used multiple times corresponding to the"
" INFO, DEBUG and NOTSET logging levels. The count maxes out at 3."
),
)

parser = argparse.ArgumentParser(
prog=prog_name(),
Expand Down Expand Up @@ -843,8 +852,8 @@ def setup_logging(verbose: int) -> None:
pipx_str = bold(green("pipx >")) if sys.stdout.isatty() else "pipx >"
pipx.constants.pipx_log_file = setup_log_file()

# Determine logging level
level_number = max(0, logging.WARNING - 10 * verbose)
# Determine logging level, a value between 0 and 50
level_number = min(max(0, logging.WARNING - 10 * verbose), 50)

level = logging.getLevelName(level_number)

Expand Down
5 changes: 5 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ def test_prog_name(monkeypatch, argv, executable, expected):
monkeypatch.setattr("pipx.main.sys.argv", [argv])
monkeypatch.setattr("pipx.main.sys.executable", executable)
assert main.prog_name() == expected


def test_limit_verbosity():
assert not run_pipx_cli(["list", "-qqq"])
assert not run_pipx_cli(["list", "-vvvv"])