Skip to content

Use type inference of pydantic settings class to create ArgumentParser #2

@bbugyi200

Description

@bbugyi200
  • The following client code should work (see pydantic.BaseSettings for more information on pydantic's settings management):
### dummy.py

import clap


class Context(clap.Context):
    foo: str
    bar: int = 4


def run(ctx: Context) -> int:
    assert isinstance(ctx.foo, str)
    assert isinstance(ctx.bar, int)
    return 0


main = clap.main_factory(run)
  • The foo and bar arguments should be derived from the following sources, ordered from least-preferred to most-preferred:
    • Default value (if specified) in Context class.
    • Global YAML configuration file (e.g. ~/.config/clap.yml).
    • Project-specific YAML configuration file (e.g. dummy.yml).
    • Environment variables (e.g. DUMMY_FOO or DUMMY_BAR) or .env file(s).
    • Command-line arguments (e.g. -f|--foo or -b|--bar).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions