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 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 👆
Example Code
from __future__ importannotationsimportreimportyamlfromtypingimportAnnotated, Anyfromml_collections.config_dictimportConfigDictapp=typer.Typer(name="myapp")
defget_config(raw: str):
withopen(raw, 'r') asfilecfg=yaml.unsafe_load(file.to_yaml())
returncfgdefset_key_in_config(raw: str):
ifrawisNone:
return# Some regexp based logic navigating the configdict given by the user# such as this one, note that this is pseudocodeassertlen(re.findall(raw, "=")) ==1keys_value=raw.split("=")
value=keys_value[-1]
keys=keys_value[0]
keys_accessed=keys.split(".")
count_inner_access=len(keys_accessed)
forkey_accessedinkeys_accessed:
ifpointerisNone:
pointer=cfg.key_accessed# Note that cfg should be accessibleelse:
pointer=pointer.key_accessedpointer=value@app.command()defrun(
cfg: str=typer.Option(
...,
"--user-config",
"-UC",
help="The config given by the user.",
parser=get_config,
),
cfg_kwargs: str=typer.Option(
None,
"--user-config.*", # Note the regexp syntaxhelp="Set specific key of cfg",
parse=set_key_in_config,
),
Description
Firstable thanks for the wonderful package ! ml_collections, a widely used package by Google, provides easydicts (nested dicts with key access using dot syntax) (ConfigDict) with lots and lots of added functionalities such as a nice CLI syntax when used in conjunction with absl (one of Typer's" competitor" by Google).
It allows the user to provide a config (say a dict) and to overwrite some of its values dynamically using CLI aka being able to write something like:
myapp run --user-config path/towards/dict/in/yaml --user-config.data.value=3 --user-config.key2=foo --user-config.key2=23.
Where path/towards/dict/in/yaml is loaded first then --user-config.* arguments are used to modify its values inplace.
In Typer with the introduction of the parser argument this would facilitate emulating this behavior using the code above. However two (maybe three) features are missing:
being able to first evaluate --user-config before any other --user-config.* arguments aka having the notion of dependent options
being able to pass a regexp instead of a string to typer.Option
the = plays the role of the space aka there is no space between the arg and its value (note that this is not a key feature and one could allow the syntax --user-config.data.value 3 for instance).
What do you think ? Does it make sense ? Would there be a simpler way to emulate this behavior using Typer existing functionalities ?
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
Firstable thanks for the wonderful package !
ml_collections, a widely used package by Google, provides easydicts (nested dicts with key access using dot syntax) (ConfigDict) with lots and lots of added functionalities such as a nice CLI syntax when used in conjunction with absl (one of Typer's" competitor" by Google).
It allows the user to provide a config (say a dict) and to overwrite some of its values dynamically using CLI aka being able to write something like:
Where
path/towards/dict/in/yaml
is loaded first then --user-config.* arguments are used to modify its values inplace.In Typer with the introduction of the parser argument this would facilitate emulating this behavior using the code above. However two (maybe three) features are missing:
What do you think ? Does it make sense ? Would there be a simpler way to emulate this behavior using Typer existing functionalities ?
Operating System
macOS
Operating System Details
No response
Typer Version
0.8.0
Python Version
3.9.18
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions