Skip to content
Open
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
40 changes: 20 additions & 20 deletions Lib/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,31 +462,31 @@ def decodebytes(s):
# Usable as a script...
def main():
"""Small main program"""
import sys, getopt
usage = f"""usage: {sys.argv[0]} [-h|-d|-e|-u] [file|-]
-h: print this help message and exit
-d, -u: decode
-e: encode (default)"""
try:
opts, args = getopt.getopt(sys.argv[1:], 'hdeu')
except getopt.error as msg:
sys.stdout = sys.stderr
print(msg)
print(usage)
sys.exit(2)
func = encode
for o, a in opts:
if o == '-e': func = encode
if o == '-d': func = decode
if o == '-u': func = decode
if o == '-h': print(usage); return
if args and args[0] != '-':
with open(args[0], 'rb') as f:
import argparse
import sys

parser = argparse.ArgumentParser()
parser.add_argument(
"-d", "-u", "--decode", action="store_true", help="decode data"
)
parser.add_argument(
"-e", "--encode", action="store_true", help="encode data (default)"
)
parser.add_argument("infile", nargs="?", default="-", help="the file to decode or encode")
args = parser.parse_args()
if args.decode:
func = decode
else:
func = encode

if args.infile != "-":
with open(args.infile, "rb") as f:
func(f, sys.stdout.buffer)
else:
if sys.stdin.isatty():
# gh-138775: read terminal input data all at once to detect EOF
import io

data = sys.stdin.buffer.read()
buffer = io.BytesIO(data)
else:
Expand Down
19 changes: 11 additions & 8 deletions Lib/test/test_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1422,8 +1422,9 @@ def get_output(self, *args):
def test_encode_file(self):
with open(os_helper.TESTFN, 'wb') as fp:
fp.write(b'a\xffb\n')
output = self.get_output('-e', os_helper.TESTFN)
self.assertEqual(output.rstrip(), b'Yf9iCg==')
for flag in ('-e', '--encode'):
output = self.get_output(flag, os_helper.TESTFN)
self.assertEqual(output.rstrip(), b'Yf9iCg==')

def test_encode_from_stdin(self):
with script_helper.spawn_python('-m', 'base64', '-e') as proc:
Expand All @@ -1434,18 +1435,20 @@ def test_encode_from_stdin(self):
def test_decode(self):
with open(os_helper.TESTFN, 'wb') as fp:
fp.write(b'Yf9iCg==')
output = self.get_output('-d', os_helper.TESTFN)
self.assertEqual(output.rstrip(), b'a\xffb')
for flag in ('-d', '--decode'):
output = self.get_output(flag, os_helper.TESTFN)
self.assertEqual(output.rstrip(), b'a\xffb')

def test_prints_usage_with_help_flag(self):
output = self.get_output('-h')
self.assertIn(b'usage: ', output)
self.assertIn(b'-d, -u: decode', output)
for flag in ('-h', '--help'):
output = self.get_output(flag)
self.assertIn(b'usage: ', output)
self.assertIn(b'-d', output)

def test_prints_usage_with_invalid_flag(self):
output = script_helper.assert_python_failure('-m', 'base64', '-x').err
self.assertIn(b'usage: ', output)
self.assertIn(b'-d, -u: decode', output)
self.assertIn(b'-d', output)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`base64`: Replace :mod:`getopt` with :mod:`argparse` for parsing
command line arguments.
Loading