Small main program
()
| 581 | |
| 582 | # Usable as a script... |
| 583 | def main(): |
| 584 | """Small main program""" |
| 585 | import sys, getopt |
| 586 | usage = f"""usage: {sys.argv[0]} [-h|-d|-e|-u] [file|-] |
| 587 | -h: print this help message and exit |
| 588 | -d, -u: decode |
| 589 | -e: encode (default)""" |
| 590 | try: |
| 591 | opts, args = getopt.getopt(sys.argv[1:], 'hdeu') |
| 592 | except getopt.error as msg: |
| 593 | sys.stdout = sys.stderr |
| 594 | print(msg) |
| 595 | print(usage) |
| 596 | sys.exit(2) |
| 597 | func = encode |
| 598 | for o, a in opts: |
| 599 | if o == '-e': func = encode |
| 600 | if o == '-d': func = decode |
| 601 | if o == '-u': func = decode |
| 602 | if o == '-h': print(usage); return |
| 603 | if args and args[0] != '-': |
| 604 | with open(args[0], 'rb') as f: |
| 605 | func(f, sys.stdout.buffer) |
| 606 | else: |
| 607 | if sys.stdin.isatty(): |
| 608 | # gh-138775: read terminal input data all at once to detect EOF |
| 609 | import io |
| 610 | data = sys.stdin.buffer.read() |
| 611 | buffer = io.BytesIO(data) |
| 612 | else: |
| 613 | buffer = sys.stdin.buffer |
| 614 | func(buffer, sys.stdout.buffer) |
| 615 | |
| 616 | |
| 617 | if __name__ == '__main__': |