import signal
import sys

import click

from geo.commands.info import info
from geo.commands.convert import convert
from geo.commands.reproject import reproject
from geo.commands.filter import filter_cmd
from geo.commands.view import view
from geo.commands.serve import serve


@click.group()
@click.option("--json", "output_json", is_flag=True, help="Machine-readable JSON output")
@click.option("-q", "--quiet", is_flag=True, help="Suppress non-essential output")
@click.option("-v", "--verbose", is_flag=True, help="Debug-level logging")
@click.version_option(package_name="gis-terminal")
def cli(output_json, quiet, verbose):
    """Terminal-native GIS tool for vector data."""
    ctx = click.get_current_context()
    ctx.ensure_object(dict)
    ctx.obj["json"] = output_json
    ctx.obj["quiet"] = quiet
    ctx.obj["verbose"] = verbose


cli.add_command(info)
cli.add_command(convert)
cli.add_command(reproject)
cli.add_command(filter_cmd)
cli.add_command(view)
cli.add_command(serve)


def main():
    try:
        if hasattr(signal, "SIGPIPE"):
            signal.signal(signal.SIGPIPE, signal.SIG_DFL)
        cli()
    except BrokenPipeError:
        sys.stderr.close()
        sys.exit(0)
