mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-35970: Add help flag to base64 module (GH-28774)
This continues off rkuska's work from https://github.com/python/cpython/pull/11789 seeing as the patch wasn't updated to add tests.
This commit is contained in:
parent
e6ff4eba6d
commit
5baec4aea6
3 changed files with 20 additions and 5 deletions
|
@ -567,15 +567,17 @@ def decodebytes(s):
|
||||||
def main():
|
def main():
|
||||||
"""Small main program"""
|
"""Small main program"""
|
||||||
import sys, getopt
|
import sys, getopt
|
||||||
|
usage = """usage: %s [-h|-d|-e|-u|-t] [file|-]
|
||||||
|
-h: print this help message and exit
|
||||||
|
-d, -u: decode
|
||||||
|
-e: encode (default)
|
||||||
|
-t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 'deut')
|
opts, args = getopt.getopt(sys.argv[1:], 'hdeut')
|
||||||
except getopt.error as msg:
|
except getopt.error as msg:
|
||||||
sys.stdout = sys.stderr
|
sys.stdout = sys.stderr
|
||||||
print(msg)
|
print(msg)
|
||||||
print("""usage: %s [-d|-e|-u|-t] [file|-]
|
print(usage)
|
||||||
-d, -u: decode
|
|
||||||
-e: encode (default)
|
|
||||||
-t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0])
|
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
func = encode
|
func = encode
|
||||||
for o, a in opts:
|
for o, a in opts:
|
||||||
|
@ -583,6 +585,7 @@ def main():
|
||||||
if o == '-d': func = decode
|
if o == '-d': func = decode
|
||||||
if o == '-u': func = decode
|
if o == '-u': func = decode
|
||||||
if o == '-t': test(); return
|
if o == '-t': test(); return
|
||||||
|
if o == '-h': print(usage); return
|
||||||
if args and args[0] != '-':
|
if args and args[0] != '-':
|
||||||
with open(args[0], 'rb') as f:
|
with open(args[0], 'rb') as f:
|
||||||
func(f, sys.stdout.buffer)
|
func(f, sys.stdout.buffer)
|
||||||
|
|
|
@ -788,5 +788,15 @@ class TestMain(unittest.TestCase):
|
||||||
output = self.get_output('-d', os_helper.TESTFN)
|
output = self.get_output('-d', os_helper.TESTFN)
|
||||||
self.assertEqual(output.rstrip(), b'a\xffb')
|
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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Add help flag to the base64 module's command line interface. Patch contributed
|
||||||
|
by Robert Kuska.
|
Loading…
Add table
Add a link
Reference in a new issue