You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question 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 tutorials 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 👆
Example Code
fromtypingimportAnnotatedimporttyperclassEnvConfig:
def__init__(self, source_paths: list[str]):
self._source_paths=source_paths# ... load config from paths ...defenv_callback(ctx: typer.Context, param: typer.CallbackParam, value: list[str]):
env_config=EnvConfig(source_paths=value)
# optionally set it as ctx.obj to base other config injections on the env config#ctx.obj = env_configreturnenv_configdefparser(value: str):
# just pass through the input valuesreturnvalueapp=typer.Typer()
@app.command()defmy_test_command(env: Annotated[list[EnvConfig], typer.Option(callback=env_callback, parser=parser)])
# now env is actually a list of EnvConfigs with one item# prints [...EnvConfig...]print(env)
Description
So the above code successfully parses multiple string options into a single EnvConfig instance but it injects the parameter as a list with one element.
Is it somehow possible to tell typer to interpret the input as multi-valued and then parse it into a single instance and pass that as argument?
I already have it running with a group callback (there I add it as a list of string options to the callback, construct the EnvConfig in the callback and set the instance as ctx.obj. In a command I can then use a 'fake' option to inject it back in (if required). However this is an approach that only works if I actually have a group to use with the group callback and this is not always the case.
The original reason I got here is that I have multiple environments that can define where to find other available configurations or which actual configurations to use in the CLI. I already built some logic around this using the 'click_type' to inject the other configurations based on the base EnvConfig in the context on demand and that works pretty well for me.
An example for this would be to enable a set of VM configurations and be able to point it to different 1Password configurations to use:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
So the above code successfully parses multiple string options into a single EnvConfig instance but it injects the parameter as a list with one element.
Is it somehow possible to tell typer to interpret the input as multi-valued and then parse it into a single instance and pass that as argument?
I already have it running with a group callback (there I add it as a list of string options to the callback, construct the EnvConfig in the callback and set the instance as ctx.obj. In a command I can then use a 'fake' option to inject it back in (if required). However this is an approach that only works if I actually have a group to use with the group callback and this is not always the case.
The original reason I got here is that I have multiple environments that can define where to find other available configurations or which actual configurations to use in the CLI. I already built some logic around this using the 'click_type' to inject the other configurations based on the base EnvConfig in the context on demand and that works pretty well for me.
An example for this would be to enable a set of VM configurations and be able to point it to different 1Password configurations to use:
mycli --env vm-set1 --env op-vault1 create-ssh-keys "vm-s1-foo"
mycli --env vm-set2 --env op-vault2 create-ssh-keys "vm-s2-bar"
The command would then look something like this
@app.command()
def create_ssh_keys(
env: Annotated[EnvConfig, typer.Option(click_type=from_context_param(EnvConfig), hidden=True, default_factory=lambda: True)],
vm_set_config: Annotated(VMSetConfig, from_env_config(VMSetConfig)),
op_vault_config: Annotated(OPVaultConfig, from_env_config(OPVaultConfig)),
vm_name: str
):
... code ...
Operating System
Linux
Operating System Details
No response
Typer Version
0.15.1
Python Version
Python 3.12.1
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions