diff --git a/Lib/base64.py b/Lib/base64.py index fa562f74a81034..dd4475c40c8f19 100644 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -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: diff --git a/Lib/test/test_base64.py b/Lib/test/test_base64.py index 868abcfee24e10..8a12d072bb9012 100644 --- a/Lib/test/test_base64.py +++ b/Lib/test/test_base64.py @@ -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: @@ -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() diff --git a/Misc/NEWS.d/next/Library/2026-06-25-14-05-32.gh-issue-152150.lKgHxY.rst b/Misc/NEWS.d/next/Library/2026-06-25-14-05-32.gh-issue-152150.lKgHxY.rst new file mode 100644 index 00000000000000..f331de51144d03 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-25-14-05-32.gh-issue-152150.lKgHxY.rst @@ -0,0 +1,2 @@ +:mod:`base64`: Replace :mod:`getopt` with :mod:`argparse` for parsing +command line arguments.