"""Remove all --json store_const aliases inserted by add_json_alias.py. Strips every block of the form: .add_argument( "--json", action="store_const", const="json", dest="fmt", help="Shorthand for --format json." ) Safe to run multiple times. """ from __future__ import annotations import pathlib import re _COMMANDS_DIR = pathlib.Path(__file__).parent.parent / "muse" / "cli" / "commands" # Match the exact 4-line block the insertion script emits (any indentation). _BLOCK_RE = re.compile( r'\n[ \t]*\w+\.add_argument\(\n' r'[ \t]+"--json", action="store_const", const="json", dest="fmt",\n' r'[ \t]+help="Shorthand for --format json\."\n' r'[ \t]+\)', re.MULTILINE, ) def revert_file(path: pathlib.Path) -> bool: original = path.read_text(encoding="utf-8") cleaned, count = _BLOCK_RE.subn("", original) if count: path.write_text(cleaned, encoding="utf-8") print(f" reverted {count} block(s) in {path.relative_to(_COMMANDS_DIR.parent.parent.parent)}") return bool(count) def main() -> None: total = 0 for f in sorted(_COMMANDS_DIR.rglob("*.py")): if revert_file(f): total += 1 print(f"\n{total} file(s) reverted.") if __name__ == "__main__": main()