Skip to content

Commit 150cd19

Browse files
bpo-29958: Minor improvements to zipfile and tarfile CLI. (#944)
1 parent fd0cd07 commit 150cd19

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

Lib/tarfile.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2450,11 +2450,11 @@ def is_tarfile(name):
24502450
def main():
24512451
import argparse
24522452

2453-
description = 'A simple command line interface for tarfile module.'
2453+
description = 'A simple command-line interface for tarfile module.'
24542454
parser = argparse.ArgumentParser(description=description)
24552455
parser.add_argument('-v', '--verbose', action='store_true', default=False,
24562456
help='Verbose output')
2457-
group = parser.add_mutually_exclusive_group()
2457+
group = parser.add_mutually_exclusive_group(required=True)
24582458
group.add_argument('-l', '--list', metavar='<tarfile>',
24592459
help='Show listing of a tarfile')
24602460
group.add_argument('-e', '--extract', nargs='+',
@@ -2467,7 +2467,7 @@ def main():
24672467
help='Test if a tarfile is valid')
24682468
args = parser.parse_args()
24692469

2470-
if args.test:
2470+
if args.test is not None:
24712471
src = args.test
24722472
if is_tarfile(src):
24732473
with open(src, 'r') as tar:
@@ -2478,15 +2478,15 @@ def main():
24782478
else:
24792479
parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
24802480

2481-
elif args.list:
2481+
elif args.list is not None:
24822482
src = args.list
24832483
if is_tarfile(src):
24842484
with TarFile.open(src, 'r:*') as tf:
24852485
tf.list(verbose=args.verbose)
24862486
else:
24872487
parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
24882488

2489-
elif args.extract:
2489+
elif args.extract is not None:
24902490
if len(args.extract) == 1:
24912491
src = args.extract[0]
24922492
curdir = os.curdir
@@ -2508,7 +2508,7 @@ def main():
25082508
else:
25092509
parser.exit(1, '{!r} is not a tar archive.\n'.format(src))
25102510

2511-
elif args.create:
2511+
elif args.create is not None:
25122512
tar_name = args.create.pop(0)
25132513
_, ext = os.path.splitext(tar_name)
25142514
compressions = {
@@ -2534,8 +2534,5 @@ def main():
25342534
if args.verbose:
25352535
print('{!r} file created.'.format(tar_name))
25362536

2537-
else:
2538-
parser.exit(1, parser.format_help())
2539-
25402537
if __name__ == '__main__':
25412538
main()

Lib/test/test_tarfile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,16 @@ def make_simple_tarfile(self, tar_name):
21672167
for tardata in files:
21682168
tf.add(tardata, arcname=os.path.basename(tardata))
21692169

2170+
def test_bad_use(self):
2171+
rc, out, err = self.tarfilecmd_failure()
2172+
self.assertEqual(out, b'')
2173+
self.assertIn(b'usage', err.lower())
2174+
self.assertIn(b'error', err.lower())
2175+
self.assertIn(b'required', err.lower())
2176+
rc, out, err = self.tarfilecmd_failure('-l', '')
2177+
self.assertEqual(out, b'')
2178+
self.assertNotEqual(err.strip(), b'')
2179+
21702180
def test_test_command(self):
21712181
for tar_name in testtarnames:
21722182
for opt in '-t', '--test':

Lib/test/test_zipfile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,16 @@ def zipfilecmd(self, *args, **kwargs):
21402140
def zipfilecmd_failure(self, *args):
21412141
return script_helper.assert_python_failure('-m', 'zipfile', *args)
21422142

2143+
def test_bad_use(self):
2144+
rc, out, err = self.zipfilecmd_failure()
2145+
self.assertEqual(out, b'')
2146+
self.assertIn(b'usage', err.lower())
2147+
self.assertIn(b'error', err.lower())
2148+
self.assertIn(b'required', err.lower())
2149+
rc, out, err = self.zipfilecmd_failure('-l', '')
2150+
self.assertEqual(out, b'')
2151+
self.assertNotEqual(err.strip(), b'')
2152+
21432153
def test_test_command(self):
21442154
zip_name = findfile('zipdir.zip')
21452155
for opt in '-t', '--test':

Lib/zipfile.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,9 +1965,9 @@ def _compile(file, optimize=-1):
19651965
def main(args=None):
19661966
import argparse
19671967

1968-
description = 'A simple command line interface for zipfile module.'
1968+
description = 'A simple command-line interface for zipfile module.'
19691969
parser = argparse.ArgumentParser(description=description)
1970-
group = parser.add_mutually_exclusive_group()
1970+
group = parser.add_mutually_exclusive_group(required=True)
19711971
group.add_argument('-l', '--list', metavar='<zipfile>',
19721972
help='Show listing of a zipfile')
19731973
group.add_argument('-e', '--extract', nargs=2,
@@ -2022,8 +2022,5 @@ def addToZip(zf, path, zippath):
20222022
zippath = ''
20232023
addToZip(zf, path, zippath)
20242024

2025-
else:
2026-
parser.exit(2, parser.format_usage())
2027-
20282025
if __name__ == "__main__":
20292026
main()

0 commit comments

Comments
 (0)