|
| 1 | +from typing import Any |
| 2 | +import click |
| 3 | +import typer |
| 4 | +import typer.core |
| 5 | +import typer.main |
| 6 | +from typer.models import OptionInfo |
| 7 | + |
| 8 | + |
| 9 | +# ======= Patch to support nargs functionality in typer ======= |
| 10 | +# nargs usage is considered bad practice in typer and many CLIs, |
| 11 | +# but since we likely want to be able to parse many optional args at once, this patch allows us to do so. |
| 12 | +# See https://github.com/fastapi/typer/issues/110 |
| 13 | +# ============================================================== |
| 14 | + |
| 15 | + |
| 16 | +class OptionEatAll(typer.core.TyperOption): |
| 17 | + """Click option that consumes arguments until next option flag.""" |
| 18 | + |
| 19 | + def __init__(self, *args: Any, **kwargs: Any) -> None: |
| 20 | + kwargs["multiple"] = True |
| 21 | + kwargs["is_flag"] = False |
| 22 | + super().__init__(*args, **kwargs) |
| 23 | + self._previous_parser_process = None |
| 24 | + self._eat_all_parser = None |
| 25 | + |
| 26 | + def add_to_parser(self, parser: Any, ctx: click.Context) -> Any: |
| 27 | + def parser_process(value: str, state: Any) -> None: |
| 28 | + values = [value] |
| 29 | + done = False |
| 30 | + while state.rargs and not done: |
| 31 | + for prefix in self._eat_all_parser.prefixes: |
| 32 | + if state.rargs[0].startswith(prefix): |
| 33 | + done = True |
| 34 | + break |
| 35 | + if not done: |
| 36 | + values.append(state.rargs.pop(0)) |
| 37 | + for val in values: |
| 38 | + self._previous_parser_process(val, state) |
| 39 | + |
| 40 | + retval = super().add_to_parser(parser, ctx) |
| 41 | + for name in self.opts: |
| 42 | + our_parser = parser._long_opt.get(name) or parser._short_opt.get(name) |
| 43 | + if our_parser: |
| 44 | + self._eat_all_parser = our_parser |
| 45 | + self._previous_parser_process = our_parser.process |
| 46 | + our_parser.process = parser_process |
| 47 | + break |
| 48 | + return retval |
| 49 | + |
| 50 | + |
| 51 | +class MultiOptionInfo(OptionInfo): |
| 52 | + """Marker for multi-argument options.""" |
| 53 | + |
| 54 | + is_multi_option: bool = True |
| 55 | + |
| 56 | + def __init__(self, option_info: OptionInfo) -> None: |
| 57 | + super().__init__(**option_info.__dict__) |
| 58 | + |
| 59 | + |
| 60 | +def MultiOption( |
| 61 | + default: Any = ..., *param_decls: str, **kwargs: Any |
| 62 | +) -> MultiOptionInfo: |
| 63 | + """Create option that accepts multiple values.""" |
| 64 | + return MultiOptionInfo(typer.Option(default, *param_decls, **kwargs)) |
| 65 | + |
| 66 | + |
| 67 | +# Monkey-patch to detect MultiOptionInfo and use OptionEatAll |
| 68 | +_original_get_click_param = typer.main.get_click_param |
| 69 | + |
| 70 | + |
| 71 | +def _patched_get_click_param(param: typer.models.ParamMeta): |
| 72 | + click_param, converter = _original_get_click_param(param) |
| 73 | + if isinstance(param.default, MultiOptionInfo): |
| 74 | + option_eat_all = object.__new__(OptionEatAll) |
| 75 | + option_eat_all.__dict__.update(click_param.__dict__) |
| 76 | + option_eat_all._previous_parser_process = None |
| 77 | + option_eat_all._eat_all_parser = None |
| 78 | + return (option_eat_all, converter) |
| 79 | + return (click_param, converter) |
| 80 | + |
| 81 | + |
| 82 | +typer.main.get_click_param = _patched_get_click_param |
0 commit comments