Small main program
()
| 435 | |
| 436 | # Usable as a script... |
| 437 | def main(): |
| 438 | """Small main program""" |
| 439 | import sys, getopt |
| 440 | usage = f"""usage: {sys.argv[0]} [-h|-d|-e|-u] [file|-] |
| 441 | -h: print this help message and exit |
| 442 | -d, -u: decode |
| 443 | -e: encode (default)""" |
| 444 | try: |
| 445 | opts, args = getopt.getopt(sys.argv[1:], 'hdeu') |
| 446 | except getopt.error as msg: |
| 447 | sys.stdout = sys.stderr |
| 448 | print(msg) |
| 449 | print(usage) |
| 450 | sys.exit(2) |
| 451 | func = encode |
| 452 | for o, a in opts: |
| 453 | if o == '-e': func = encode |
| 454 | if o == '-d': func = decode |
| 455 | if o == '-u': func = decode |
| 456 | if o == '-h': print(usage); return |
| 457 | if args and args[0] != '-': |
| 458 | with open(args[0], 'rb') as f: |
| 459 | func(f, sys.stdout.buffer) |
| 460 | else: |
| 461 | if sys.stdin.isatty(): |
| 462 | # gh-138775: read terminal input data all at once to detect EOF |
| 463 | import io |
| 464 | data = sys.stdin.buffer.read() |
| 465 | buffer = io.BytesIO(data) |
| 466 | else: |
| 467 | buffer = sys.stdin.buffer |
| 468 | func(buffer, sys.stdout.buffer) |
| 469 | |
| 470 | |
| 471 | if __name__ == '__main__': |