Files
Vlad Lasky 7623f75478 moves.py: require --last to be at least 1
The listing sliced rows[-args.last:], so `--last 0` became rows[-0:],
which is every move, and `--last -1` became rows[1:], every move but the
oldest. Neither is what the flag says. A count below 1 now stops with
"--last must be 1 or more" (argparse usage error, exit 2).
2026-11-26 14:56:06 +11:00

38 lines
1.2 KiB
Python

#!/usr/bin/env python3
import argparse
import sys
import history
def main():
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ap = argparse.ArgumentParser()
ap.add_argument("action", nargs="?", default="show", choices=["show", "add"])
ap.add_argument("text", nargs="*")
ap.add_argument("--last", type=int, default=12)
args = ap.parse_args()
if args.last < 1:
ap.error("--last must be 1 or more")
if args.action == "add":
text = " ".join(args.text).strip()
if not text:
sys.exit("add needs the move, for example: moves.py add \"poster: x-ray method, deep blue ground\"")
try:
history.add({"type": "move", "move": text})
except OSError as e:
sys.exit(f"could not save the move: {e}")
print(f"saved: {text}")
return
rows = [r for r in history.load("move") if isinstance(r.get("move"), str)][-args.last:]
if not rows:
print("no moves saved yet")
return
for row in reversed(rows):
print(f"{row.get('date', '?')} {row['move']}")
if __name__ == "__main__":
main()