Replies: 1 comment 9 replies
-
import typer
from typing_extensions import Annotated
def main(
name: str,
lastname: Annotated[str, typer.Argument(hidden=True)] = "",
):
print(f"Hello {name} {lastname}")
if __name__ == "__main__":
typer.run(main)
If your intention is to forbid passing this argument via command line, you can achieve this with the callback: import typer
from typing_extensions import Annotated
def enforce_default(ctx: typer.Context, param: typer.CallbackParam, value: str):
return param.default
def main(
name: str,
lastname: Annotated[
str,
typer.Argument(hidden=True, callback=enforce_default)
] = "<default>",
):
print(f"Hello {name} {lastname}")
if __name__ == "__main__":
typer.run(main)
With complex types you will also need to specify import inspect
import typer
from typing_extensions import Annotated
def enforce_default(ctx: typer.Context, param: typer.CallbackParam, value: str):
if inspect.isfunction(param.default):
return param.default()
return param.default
class MyClass:
def __init__(self, a: str):
self.a = a
def main(
name: str,
param: Annotated[
MyClass,
typer.Argument(
hidden=True,
callback=enforce_default,
default_factory=lambda: MyClass(a="default_value"),
parser=lambda x: None
),
],
):
print(f"Hello {name}")
print(param)
if __name__ == "__main__":
typer.run(main) |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Example Code
Description
The click module has options such as
hidden
, which tell it not to show the parameter to the user, but I could not find any way to telltyper
to ignore a specific parameter.In my use case, I use the
cls=MyOwnTyperClass
which always adds specific optionals. You can see this as an example:https://github.com/doronz88/pymobiledevice3/blob/cef614ac0042641ffe8bd59df65001ebb5d94549/pymobiledevice3/cli/developer.py#L103
However, I failed to figure out a way in which I can accomplish this skip, and it does not seem like
typer
is designed to ignore such cases. Probably the best case would be implementing insideget_click_type()
function so have such an option. What do you think?This is an example API I think is best for the use case:
Or perhaps simply respect
click
'shidden=True
option.Typer Version
0.19.1
Python Version
3.13
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions