Skip to content

Typed-settings compatibility #434

Description

@1oglop1

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

Description

Hi,
I'd like to use typed-settings https://typed-settings.readthedocs.io/en/latest/guides/click.html#command-line-arguments-with-click with Typer. It has a decorator to generate click options from the settings object see the link to typed-settings.

I tried to include the click app in typer https://typer.tiangolo.com/tutorial/using-click/#including-a-typer-app-in-a-click-app but then I'm essentially using click and not Typer.

Wanted Solution

I would like to be able to add options from typed-settings Object in a similar style as it is described in docs.

It looks like what am I describing is the ability to dynamically set options from an object. I guess it is similar to #305 #86 (comment)

Here is what typed-settings usage looks like

import click
import os
import typed_settings as ts

os.environ['EXAMPLE_SPAM'] = "23"

@ts.settings
class Settings:
    spam: int = 42

@click.command()
@ts.click_options(Settings, "example")
def cli(settings: Settings):
    print(settings)
    
# As you can see, an option is generated for each setting:

import click.testing

runner = click.testing.CliRunner()
print(runner.invoke(cli, ["--help"]).output)

output

Usage: cli [OPTIONS]

Options:
  --spam INTEGER  [default: 23]
  --help          Show this message and exit.

print(runner.invoke(cli, ["--spam=3"]).output)
Settings(spam=3)

Wanted Code

import typer
import os

os.environ['EXAMPLE_SPAM'] = "23"

import typed_settings as ts


@ts.settings
class Settings:
    spam: int = 42



app = typer.Typer()


@app.command()
def dynamic_options(settings:Settings=typer.Options()):  # load options from the class
    print(settings)

Alternatives

No response

Operating System

Linux

Operating System Details

No response

Typer Version

0.6.1

Python Version

3.9

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature, enhancement or request

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions