Source code for pyfcstm.entry.dispatch

"""
Command-line interface entry point for the :mod:`pyfcstm` package.

This module defines the main Click command group used to expose the CLI for
the project, including version reporting and help configuration. It builds
user-facing metadata from the project configuration and provides a top-level
command entry point.

The module contains the following main components:

* :func:`pyfcstmcli` - Main Click command group for the CLI

Example::

    >>> from pyfcstm.entry.dispatch import pyfcstmcli
    >>> # Typically invoked via a Click entry point:
    >>> # python -m pyfcstm --help

.. note::
   The version display is implemented via a Click option callback, which
   prints version information and exits the process.

"""

import click
from click.core import Context, Option

from .base import CONTEXT_SETTINGS
from ..config.meta import __TITLE__, __VERSION__, __AUTHOR__, __AUTHOR_EMAIL__, __DESCRIPTION__

_raw_authors = [item.strip() for item in __AUTHOR__.split(',') if item.strip()]
_raw_emails = [item.strip() for item in __AUTHOR_EMAIL__.split(',')]
if len(_raw_emails) < len(_raw_authors):  # pragma: no cover
    _raw_emails += [None] * (len(_raw_authors) - len(_raw_emails))
elif len(_raw_emails) > len(_raw_authors):  # pragma: no cover
    _raw_emails[len(_raw_authors) - 1] = tuple(_raw_emails[len(_raw_authors) - 1:])
    del _raw_emails[len(_raw_authors):]

_author_tuples = [
    (author, tuple([item for item in (email if isinstance(email, tuple) else ((email,) if email else ())) if item]))
    for author, email in zip(_raw_authors, _raw_emails)
]
_authors = [
    author if not emails else '{author} ({emails})'.format(author=author, emails=', '.join(emails))
    for author, emails in _author_tuples
]


# noinspection PyUnusedLocal




@click.group(context_settings=CONTEXT_SETTINGS, help=__DESCRIPTION__)
@click.option('-v', '--version', is_flag=True,
              callback=print_version, expose_value=False, is_eager=True,
              help="Show pyfcstm's version information.")
def pyfcstmcli() -> None:
    """
    Main Click command group for the :mod:`pyfcstm` CLI.

    This command group provides a common entry point for subcommands and
    integrates global options such as ``--version`` and help flags.

    :return: ``None``.
    :rtype: None

    Example::

        >>> # Typically invoked via a console entry point:
        >>> # $ pyfcstm --help

    """
    pass  # pragma: no cover