File tree Expand file tree Collapse file tree 3 files changed +20
-5
lines changed
Expand file tree Collapse file tree 3 files changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -567,22 +567,25 @@ def decodebytes(s):
567567def 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 )
Original file line number Diff line number Diff 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\xff b' )
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+
791801if __name__ == '__main__' :
792802 unittest .main ()
Original file line number Diff line number Diff line change 1+ Add help flag to the base64 module's command line interface. Patch contributed
2+ by Robert Kuska.
You can’t perform that action at this time.
0 commit comments