#!/usr/bin/python3
"""Generate a fish completion file from the argparse parser."""

from __future__ import annotations

import argparse
import shlex

from sseurl.cli import build_parser


def quote(value: object) -> str:
    return shlex.quote(str(value))


def emit(parts: list[object]) -> None:
    print(" ".join(quote(part) for part in parts))


parser = build_parser()
print("# AUTOMATICALLY GENERATED from sseurl.cli.build_parser")

for action in parser._actions:
    if not action.option_strings:
        continue

    takes_value = action.nargs != 0
    choices = None
    if action.choices:
        choices = " ".join(str(choice) for choice in action.choices)

    for option in action.option_strings:
        parts: list[object] = ["complete", "-c", parser.prog]
        if option.startswith("--"):
            parts.extend(["-l", option[2:]])
        elif option.startswith("-") and len(option) == 2:
            parts.extend(["-s", option[1:]])
        else:
            parts.extend(["-o", option.lstrip("-")])

        if action.help is not argparse.SUPPRESS:
            parts.extend(["-d", action.help])
        if takes_value:
            parts.append("-r")
        if choices is not None:
            parts.extend(["-f", "-a", choices])

        emit(parts)
