Skip to content

Commit 5baec4a

Browse files
authored
bpo-35970: Add help flag to base64 module (GH-28774)
This continues off rkuska's work from #11789 seeing as the patch wasn't updated to add tests.
1 parent e6ff4eb commit 5baec4a

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

Lib/base64.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,22 +567,25 @@ def decodebytes(s):
567567
def main():
568568
"""Small main program"""
569569
import sys, getopt
570+
usage = """usage: %s [-h|-d|-e|-u|-t] [file|-]
571+
-h: print this help message and exit
572+
-d, -u: decode
573+
-e: encode (default)
574+
-t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
570575
try:
571-
opts, args = getopt.getopt(sys.argv[1:], 'deut')
576+
opts, args = getopt.getopt(sys.argv[1:], 'hdeut')
572577
except getopt.error as msg:
573578
sys.stdout = sys.stderr
574579
print(msg)
575-
print("""usage: %s [-d|-e|-u|-t] [file|-]
576-
-d, -u: decode
577-
-e: encode (default)
578-
-t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0])
580+
print(usage)
579581
sys.exit(2)
580582
func = encode
581583
for o, a in opts:
582584
if o == '-e': func = encode
583585
if o == '-d': func = decode
584586
if o == '-u': func = decode
585587
if o == '-t': test(); return
588+
if o == '-h': print(usage); return
586589
if args and args[0] != '-':
587590
with open(args[0], 'rb') as f:
588591
func(f, sys.stdout.buffer)

Lib/test/test_base64.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,5 +788,15 @@ def test_decode(self):
788788
output = self.get_output('-d', os_helper.TESTFN)
789789
self.assertEqual(output.rstrip(), b'a\xffb')
790790

791+
def test_prints_usage_with_help_flag(self):
792+
output = self.get_output('-h')
793+
self.assertIn(b'usage: ', output)
794+
self.assertIn(b'-d, -u: decode', output)
795+
796+
def test_prints_usage_with_invalid_flag(self):
797+
output = script_helper.assert_python_failure('-m', 'base64', '-x').err
798+
self.assertIn(b'usage: ', output)
799+
self.assertIn(b'-d, -u: decode', output)
800+
791801
if __name__ == '__main__':
792802
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add help flag to the base64 module's command line interface. Patch contributed
2+
by Robert Kuska.

0 commit comments

Comments
 (0)