Skip to content

parse_cli_args

faststream.cli.utils.parser.parse_cli_args #

parse_cli_args(*args)

Parses command line arguments.

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

                    key = remove_prefix(k, "no_")
                    if (exists := extra_kwargs.get(key)) is not None:
                        v = [
                            *(exists if isinstance(exists, list) else [exists]),
                            *(v if isinstance(v, list) else [v]),
                        ]

                    extra_kwargs[key] = v
                    field_args = []

                k = item

            else:
                field_args.append(item)

    return app, extra_kwargs