"""Every option PyInstaller takes, in PyInstaller's own words.

    python flags.py

Asks the PyInstaller in this Python for its help text and prints it. The last
lines count the options and list every name, so that a table of options can be
compared with what your own PyInstaller really takes. A newer version may have
more of them.
"""
import os
import subprocess
import sys


def ask(option):
    """What `python -m PyInstaller <option>` prints."""
    command = [sys.executable, "-m", "PyInstaller", option]
    # The help text has curly quotes in it. Windows would send them in its own
    # old encoding, so ask that Python to write UTF-8, and read UTF-8 here.
    utf8 = dict(os.environ, PYTHONIOENCODING="utf-8")
    reply = subprocess.run(command, capture_output=True, encoding="utf-8", env=utf8)
    return reply.stdout.strip()


def names_in(help_text):
    """The names of each option, one list for each option, in the help's order."""
    options = []
    for line in help_text.splitlines():
        # An option starts a line, two spaces in: "  -F, --onefile   Create a ..."
        if not line.startswith("  -"):
            continue
        names = []
        for word in line.split("  ")[1].split(", "):
            names.append(word.split()[0])
        options.append(names)
    return options


def main():
    sys.stdout.reconfigure(encoding="utf-8")
    help_text = ask("--help")
    print("PyInstaller " + ask("--version") + ": python -m PyInstaller --help")
    print()
    print(help_text)
    print()
    options = names_in(help_text)
    print(f"{len(options)} options. Their names:")
    for names in options:
        print("    " + "  ".join(names))


if __name__ == "__main__":
    main()
