3 ms·
No, you take code away by using argparse! Handles all this GNU longopt and argument parsing stuff for you, and autogenerates the --help display. Probably someth
by tom_ 6y ago
No, you take code away by using argparse! Handles all this GNU longopt and argument parsing stuff for you, and autogenerates the --help display. Probably something like this:
import argparse
def auto_int(x): return int(x,0) # http://stackoverflow.com/questions/25513043/
def main(argv):
parser=argparse.ArgumentParser()
parser.add_option('-r',dest='reverse',action='store_true',help='reverse operation: convert hex to binary')
parser.add_option('-w',default=40,dest='width',nargs='?',type=auto_int,help='maximum width: fit lines within width (default is %(default)s.)")
options=parser.parse_args(sys.argv[1:])
if options.reverse: bin2hex(options.width)
else: hex2bin()
if __name__=='__main__': main(sys.argv[1:])