"""CLI shim so shell callers share the one locator instead of re-implementing it
(the bash copy in vault-sync.sh reconciles onto this — docs/DEVAULT.md step 2):

    python3 -m alexandria_vault.vault_core locate [--vault PATH] [--start DIR]
"""
import argparse
import sys

from .locate import VaultNotFoundError, locate_vault


def main(argv=None) -> int:
    ap = argparse.ArgumentParser(prog="alexandria_vault.vault_core")
    sub = ap.add_subparsers(dest="cmd", required=True)
    loc = sub.add_parser("locate", help="print the resolved vault root")
    loc.add_argument("--vault", help="explicit vault path (validated, wins over $VAULT_DIR)")
    loc.add_argument("--start", help="directory to walk up from (default: cwd)")
    args = ap.parse_args(argv)
    if args.cmd == "locate":
        try:
            print(locate_vault(args.vault, args.start))
            return 0
        except VaultNotFoundError as e:
            print(f"vault_core: {e}", file=sys.stderr)
            return 1
    return 2


if __name__ == "__main__":
    raise SystemExit(main())
