Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for click #12

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Adding support for click #12

wants to merge 5 commits into from

Conversation

soldni
Copy link
Owner

@soldni soldni commented Jan 31, 2023

Worklog:

  • Refactor springs.commandline so that logic to parse configurations is separate from creation of command line flags.
  • Add decorator for click that captures unused arguments.
  • Add options in click to map to flags in the current parser (should a user be able to customize them?)

@soldni soldni marked this pull request as draft January 31, 2023 09:01
@soldni soldni linked an issue Jan 31, 2023 that may be closed by this pull request
@soldni
Copy link
Owner Author

soldni commented Jan 31, 2023

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integration with existing click CLIs
1 participant