Implement the main CLI interface.
()
| 485 | |
| 486 | |
| 487 | def main(): |
| 488 | """Implement the main CLI interface.""" |
| 489 | desc = ( |
| 490 | "PyTables 2.x -> 3.x API transition tool\n\n" |
| 491 | "This tool displays to standard out, so it is \n" |
| 492 | "common to pipe this to another file:\n\n" |
| 493 | "$ pt2to3 oldfile.py > newfile.py" |
| 494 | ) |
| 495 | parser = argparse.ArgumentParser(description=desc) |
| 496 | parser.add_argument( |
| 497 | "-r", |
| 498 | "--reverse", |
| 499 | action="store_true", |
| 500 | default=False, |
| 501 | dest="reverse", |
| 502 | help="reverts changes, going from 3.x -> 2.x.", |
| 503 | ) |
| 504 | parser.add_argument( |
| 505 | "-p", |
| 506 | "--no-ignore-previous", |
| 507 | action="store_false", |
| 508 | default=True, |
| 509 | dest="ignore_previous", |
| 510 | help="ignores previous_api() calls.", |
| 511 | ) |
| 512 | parser.add_argument( |
| 513 | "-o", default=None, dest="output", help="output file to write to." |
| 514 | ) |
| 515 | parser.add_argument( |
| 516 | "-i", |
| 517 | "--inplace", |
| 518 | action="store_true", |
| 519 | default=False, |
| 520 | dest="inplace", |
| 521 | help="overwrites the file in-place.", |
| 522 | ) |
| 523 | parser.add_argument("filename", help="path to input file.") |
| 524 | ns = parser.parse_args() |
| 525 | |
| 526 | if not Path(ns.filename).is_file(): |
| 527 | sys.exit(f"file {ns.filename!r} not found") |
| 528 | src = Path(ns.filename).read_text() |
| 529 | |
| 530 | subs, repl = make_subs(ns) |
| 531 | targ = subs.sub(repl, src) |
| 532 | |
| 533 | ns.output = ns.filename if ns.inplace else ns.output |
| 534 | if ns.output is None: |
| 535 | sys.stdout.write(targ) |
| 536 | else: |
| 537 | Path(ns.output).write_text(targ) |
| 538 | |
| 539 | |
| 540 | if __name__ == "__main__": |