(argv: list[str])
| 2676 | |
| 2677 | |
| 2678 | def main(argv: list[str]) -> int: |
| 2679 | # Windows defaults stdout/stderr to cp1252; violation messages can carry |
| 2680 | # non-ASCII (em-dashes, smart quotes, U+2713) lifted from user source and |
| 2681 | # crash with UnicodeEncodeError before any report is written. |
| 2682 | for stream in (sys.stdout, sys.stderr): |
| 2683 | reconfigure = getattr(stream, "reconfigure", None) |
| 2684 | if reconfigure is not None: |
| 2685 | reconfigure(encoding="utf-8", errors="replace") |
| 2686 | |
| 2687 | parser = argparse.ArgumentParser( |
| 2688 | description=__doc__.splitlines()[0], |
| 2689 | epilog="With no arguments, runs --fix on the repo's default trees.", |
| 2690 | ) |
| 2691 | group = parser.add_mutually_exclusive_group() |
| 2692 | group.add_argument("--check", action="store_true", help="report only, no writes") |
| 2693 | group.add_argument( |
| 2694 | "--fix", action="store_true", help="rewrite files in place (default)" |
| 2695 | ) |
| 2696 | parser.add_argument( |
| 2697 | "--diff", action="store_true", help="show unified diff of proposed changes" |
| 2698 | ) |
| 2699 | parser.add_argument( |
| 2700 | "--no-report", |
| 2701 | action="store_true", |
| 2702 | help="skip writing .code-report at the repo root", |
| 2703 | ) |
| 2704 | parser.add_argument( |
| 2705 | "paths", |
| 2706 | nargs="*", |
| 2707 | type=Path, |
| 2708 | help="files or directories to process (default: repo trees)", |
| 2709 | ) |
| 2710 | |
| 2711 | args = parser.parse_args(argv) |
| 2712 | |
| 2713 | # Default to --fix when neither mode was explicitly requested |
| 2714 | if not args.check and not args.fix: |
| 2715 | args.fix = True |
| 2716 | |
| 2717 | # Default to the configured repo trees when no paths were supplied |
| 2718 | if not args.paths: |
| 2719 | targets = [t for t in default_targets() if t.exists()] |
| 2720 | if not targets: |
| 2721 | print("no default targets exist; pass paths explicitly", file=sys.stderr) |
| 2722 | return 2 |
| 2723 | args.paths = targets |
| 2724 | |
| 2725 | files = sorted(set(iter_source_files(args.paths))) |
| 2726 | if not files: |
| 2727 | print("no source files found", file=sys.stderr) |
| 2728 | return 2 |
| 2729 | |
| 2730 | total_violations = 0 |
| 2731 | total_changed = 0 |
| 2732 | error_count = 0 |
| 2733 | advisory_count = 0 |
| 2734 | flag_only: list[Violation] = [] |
| 2735 |
no test coverage detected