Skip to content

Commit

Permalink
When --quiet is used twice, suppress error output from archive commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
wummel committed Nov 18, 2024
1 parent c4c7982 commit 2685879
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 11 deletions.
5 changes: 4 additions & 1 deletion doc/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
3.0.4 (released xx.xx.xxxx)
* Added support for UDF (.udf) archives with 7z.
Closes: GH bug #80
Closes: GH bug #80
* The options --quiet and --verbose are now mutually exclusive options.
* When --quiet is given twice, output on stderr from archive commands is
suppressed. When using the API functions, set verbosity to -2.

3.0.3 (released 04.11.2024)
* Fix UnicodeDecode errors when logging, especially on windows systems.
Expand Down
4 changes: 3 additions & 1 deletion doc/patool.1
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ of helper applications. Can be given multiple times to increase
the output even more.
.TP
\fB\-q\fP, \fB\-\-quiet\fP
Work quietly, exept on commands \fBformats\fP and \fBversion\fP, which still print their output.
Work quietly, exept on commands \fBformats\fP and \fBversion\fP,
which still print their output. Conflicts with \fB\-\-verbose\fP.
If given twice suppresses error output from archive commands.
.TP
\fB\-\-non\-interactive\fP
Try to prevent any interactive user input (i.e. prompting for passwords
Expand Down
4 changes: 3 additions & 1 deletion doc/patool.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ GLOBAL OPTIONS

-q, --quiet
Work quietly, exept on commands formats and version,
which still print their output.
which still print their output. Conflicts with --ver‐
bose. If given twice suppresses error output from ar‐
chive commands.

--non-interactive
Try to prevent any interactive user input (i.e. prompt‐
Expand Down
15 changes: 9 additions & 6 deletions patoolib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,22 @@ def create_argparser() -> argparse.ArgumentParser:
epilog=epilog,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--verbose',
'-v',
action='count',
default=0,
dest='verbosity',
help="verbose operation; can be given multiple times",
)
parser.add_argument(
group.add_argument(
'--quiet',
'-q',
action='store_const',
const=-1,
dest='verbosity',
help="quiet operation",
action='count',
default=0,
dest='quiet',
help="quiet operation; if given twice suppresses error output from archive commands",
)
parser.add_argument(
'--non-interactive',
Expand Down Expand Up @@ -293,6 +294,8 @@ def main(args=None) -> int:
try:
argparser = create_argparser()
pargs = argparser.parse_args(args=args)
if pargs.quiet:
pargs.verbosity = -pargs.quiet
# run subcommand function
res = globals()[f"run_{pargs.command}"](pargs)
except KeyboardInterrupt:
Expand Down
3 changes: 3 additions & 0 deletions patoolib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def run(cmd: Sequence[str], verbosity: int = 0, **kwargs) -> int:
if verbosity < 1:
# hide command output on stdout
kwargs['stdout'] = subprocess.DEVNULL
if verbosity < -1:
# hide command output on stdout
kwargs['stderr'] = subprocess.DEVNULL
if kwargs:
if verbosity > 0:
info = ", ".join(f"{k}={shell_quote(str(v))}" for k, v in kwargs.items())
Expand Down
2 changes: 1 addition & 1 deletion tests/archives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ArchiveTest(unittest.TestCase):
# default archive basename to check
filename = 't'
# verbosity levels in decreasing order, so that failing tests have maximum verbosity
verbosity_levels = (2, 1, 0, -1)
verbosity_levels = (2, 1, 0, -1, -2)

def archive_commands(self, filename, **kwargs):
"""Run archive commands list, test, extract and create.
Expand Down
10 changes: 9 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Test patool command line parsing."""

import unittest
import pytest
from patoolib import cli


Expand All @@ -35,4 +36,11 @@ def test_cli_verbosity(self):
self.assertEqual(pargs.verbosity, 2)
args = ["-q", "extract", "t.zip"]
pargs = parser.parse_args(args=args)
self.assertEqual(pargs.verbosity, -1)
self.assertEqual(pargs.quiet, 1)
args = ["-qq", "extract", "t.zip"]
pargs = parser.parse_args(args=args)
self.assertEqual(pargs.quiet, 2)
# conflicting options
with pytest.raises(SystemExit):
args = ["-qv", "extract", "t.zip"]
pargs = parser.parse_args(args=args)

0 comments on commit 2685879

Please sign in to comment.