Skip to content

parse_cli_args

faststream.cli.utils.parser.parse_cli_args #

parse_cli_args(
    *args: str,
) -> Tuple[str, Dict[str, SettingField]]

Parses command line arguments.

PARAMETER DESCRIPTION
*args

Command line arguments as strings.

TYPE: str DEFAULT: ()

RETURNS DESCRIPTION
Tuple[str, Dict[str, SettingField]]

A tuple containing the application name and a dictionary of additional keyword arguments.

Note

The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)

Source code in faststream/cli/utils/parser.py
def parse_cli_args(*args: str) -> Tuple[str, Dict[str, SettingField]]:
    """Parses command line arguments.

    Args:
        *args: Command line arguments as strings.

    Returns:
        A tuple containing the application name and a dictionary of additional keyword arguments.
    !!! note

        The above docstring is autogenerated by docstring-gen library (https://docstring-gen.airt.ai)
    """
    extra_kwargs: Dict[str, SettingField] = {}

    k: str = ""
    v: SettingField

    field_args: List[str] = []
    app = ""
    for item in reduce(
        lambda acc, x: acc + x.split("="),  # type: ignore
        args,
        [],
    ) + ["-"]:
        if ":" in item:
            app = item

        else:
            if "-" in item:
                if k:
                    k = k.strip().lstrip("-").replace("-", "_")

                    if len(field_args) == 0:
                        v = not k.startswith("no_")
                    elif len(field_args) == 1:
                        v = field_args[0]
                    else:
                        v = field_args

                    extra_kwargs[remove_prefix(k, "no_")] = v
                    field_args = []

                k = item

            else:
                field_args.append(item)

    return app, extra_kwargs

Last update: 2023-11-13