Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a33046

Browse files
committed
cli: make --quiet also suppress ConsoleOutput
1 parent b2aa37c commit 3a33046

3 files changed

Lines changed: 44 additions & 14 deletions

File tree

src/streamlink_cli/argparser.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ def build_parser():
412412
"--quiet",
413413
action="store_true",
414414
help="""
415-
Hide all log output.
416-
417-
Alias for --loglevel=none.
415+
Suppress all console and log output, and also disable user prompts.
418416
""",
419417
)
420418
logging.add_argument(

src/streamlink_cli/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,9 @@ def setup_console() -> None:
852852
global console
853853

854854
console_output: TextIO | None
855-
if args.stdout or args.output == "-" or args.record == "-" or args.record_and_pipe:
855+
if args.quiet:
856+
console_output = None
857+
elif args.stdout or args.output == "-" or args.record == "-" or args.record_and_pipe:
856858
# Console output should be on stderr if we are outputting a stream to stdout
857859
console_output = sys.stderr
858860
else:

tests/cli/main/test_logging.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def _no_debug_logs(self, monkeypatch: pytest.MonkeyPatch):
4343
("argv", "stream"),
4444
[
4545
pytest.param([], "stdout", id="default"),
46+
pytest.param(["--quiet"], None, id="--quiet"),
4647
pytest.param(["--stdout"], "stderr", id="--stdout"),
4748
pytest.param(["--output=file"], "stdout", id="--output=file"),
4849
pytest.param(["--output=-"], "stderr", id="--output=-"),
@@ -52,18 +53,26 @@ def _no_debug_logs(self, monkeypatch: pytest.MonkeyPatch):
5253
],
5354
indirect=["argv"],
5455
)
55-
def test_streams(self, capsys: pytest.CaptureFixture, parser: ArgumentParser, argv: list, stream: str):
56+
def test_streams(self, capsys: pytest.CaptureFixture, parser: ArgumentParser, argv: list, stream: str | None):
5657
streamlink_cli.main.setup(parser)
5758

5859
rootlogger = logging.getLogger("streamlink")
5960
clilogger = streamlink_cli.main.log
60-
streamobj = getattr(sys, stream)
61+
streamobj = {
62+
None: None,
63+
"stdout": sys.stdout,
64+
"stderr": sys.stderr,
65+
}.get(stream)
6166

62-
assert clilogger.parent is rootlogger
63-
assert isinstance(rootlogger.handlers[0], logging.StreamHandler)
64-
assert rootlogger.handlers[0].stream is streamobj
6567
assert streamlink_cli.main.console.console_output is streamobj
6668
assert streamlink_cli.main.console.file_output is None
69+
assert clilogger.parent is rootlogger
70+
if stream is None:
71+
assert not rootlogger.handlers
72+
else:
73+
handler = rootlogger.handlers[0]
74+
assert isinstance(handler, logging.StreamHandler)
75+
assert handler.stream is streamobj
6776

6877
@pytest.mark.parametrize(
6978
("argv", "stdout", "stderr"),
@@ -479,20 +488,29 @@ def logpath(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path):
479488

480489
# noinspection PyUnresolvedReferences
481490
@pytest.mark.parametrize(
482-
("argv", "stdout", "stderr"),
491+
("argv", "stream", "stdout", "stderr"),
483492
[
484493
pytest.param(
485494
[],
495+
"stdout",
486496
"[cli][info] a\nb\n",
487497
"",
488498
id="no-logfile",
489499
),
490500
pytest.param(
491501
["--logfile=file", "--loglevel=none"],
502+
"stdout",
492503
"b\n",
493504
"",
494505
id="logfile-loglevel-none",
495506
),
507+
pytest.param(
508+
["--logfile=file", "--quiet"],
509+
None,
510+
"",
511+
"",
512+
id="logfile-quiet",
513+
),
496514
],
497515
indirect=["argv"],
498516
)
@@ -502,20 +520,32 @@ def test_no_logfile(
502520
capsys: pytest.CaptureFixture,
503521
parser: ArgumentParser,
504522
argv: list,
523+
stream: str | None,
505524
stdout: str,
506525
stderr: str,
507526
):
508527
mock_open = Mock()
509528
monkeypatch.setattr("builtins.open", mock_open)
510529

530+
streamobj = {
531+
None: None,
532+
"stdout": sys.stdout,
533+
"stderr": sys.stderr,
534+
}.get(stream)
535+
511536
streamlink_cli.main.setup(parser)
512537

513-
rootlogger = logging.getLogger("streamlink")
514-
assert isinstance(rootlogger.handlers[0], logging.StreamHandler)
515-
assert rootlogger.handlers[0].stream is sys.stdout
516-
assert streamlink_cli.main.console.console_output is sys.stdout
538+
assert streamlink_cli.main.console.console_output is streamobj
517539
assert streamlink_cli.main.console.file_output is None
518540

541+
rootlogger = logging.getLogger("streamlink")
542+
if stream is None:
543+
assert not rootlogger.handlers
544+
else:
545+
handler = rootlogger.handlers[0]
546+
assert isinstance(handler, logging.StreamHandler)
547+
assert handler.stream is streamobj
548+
519549
streamlink_cli.main.log.info("a")
520550
streamlink_cli.main.console.msg("b")
521551
out, err = capsys.readouterr()

0 commit comments

Comments
 (0)