import argparse
import sys


def count_words(text):
    return len(text.split())


def main():
    parser = argparse.ArgumentParser(description="Count words in a text file.")
    parser.add_argument("file", help="Path to the text file")
    args = parser.parse_args()

    try:
        with open(args.file, "r", encoding="utf-8") as f:
            text = f.read()
    except FileNotFoundError:
        print(f"Error: File '{args.file}' not found.", file=sys.stderr)
        sys.exit(1)
    except OSError as e:
        print(f"Error reading file: {e}", file=sys.stderr)
        sys.exit(1)

    print(count_words(text))


if __name__ == "__main__":
    main()
