soldni / springs

A set of utilities to turn Dataclasses into useful configuration managers.
https://springs.soldaini.net
MIT License
11 stars 2 forks source link

Adding support for `click` #12

Open soldni opened 1 year ago

soldni commented 1 year ago

Worklog:

soldni commented 1 year ago

Demo code of how the decorator could look like:

from functools import partial

import click

import springs as sp

@sp.dataclass
class Config:
    other: int = 1

def click_parser(
    ctx: click.Context, arg: click.Argument, val: tuple, /, config_cls: type
):
    # NOTE: other flags are in `ctx.params`
    # TODO: logic to merge cli args with config file goes here
    ...

def add_click(config_cls):
    # TODO: need to add typing annotations (maybe)
    # TODO: need to add all the flags a springs cli app requires
    return click.argument(
        "springs_config",
        nargs=-1,
        type=click.UNPROCESSED,
        callback=partial(click_parser, config_cls=config_cls),
    )

@click.command()
@click.option("-c", "--count", default=1, help="Number of greetings.")
@add_click(Config)
def main(count: int, springs_config: Config):
    print(springs_config)
    print(count)

if __name__ == "__main__":
    main()