"""
Command-line entry point for the check-duplicate-variables package.

This module allows the package to be run as a module using:
    python -m check_duplicate_variables [directory]
"""

import sys
import os
from check_duplicate_variables.analyzer import main


def cli_main():
    """
    Command-line entry point for the script.

    Parses command-line arguments and executes the main analysis function.
    Handles exceptions and sets appropriate exit codes for CI/CD integration.
    """
    try:
        # Accept optional directory path as first command-line argument
        pageobjects_dir = None if len(sys.argv) < 2 else os.path.abspath(sys.argv[1])
        has_issues = main(pageobjects_dir)
        # Exit code 1 indicates issues found (for CI/CD failure detection)
        # Exit code 0 indicates success (no duplicates found)
        sys.exit(1 if has_issues else 0)
    except Exception as e:
        # Catch any unexpected errors and exit with error code
        print(f"[ERROR] {e}")
        sys.exit(1)


if __name__ == "__main__":
    cli_main()
