Replies: 1 comment
-
There is no such feature in Typer, but if you really need such behavior I think you can achieve this using callback and global state object: from dataclasses import dataclass
from typing import Optional
import typer
from typing_extensions import Annotated
@dataclass
class State:
feature_switch: str = "undefined"
state = State()
def feature_switch_callback(ctx: typer.Context, param: typer.CallbackParam, value: bool):
if value:
# You can add logic to make these flags mutually exclusive
state.feature_switch = param.name
return value
def main(
upper: Annotated[Optional[bool], typer.Option("--upper", callback=feature_switch_callback)] = None,
lower: Annotated[Optional[bool], typer.Option("--lower", callback=feature_switch_callback)] = None
):
typer.echo(state.feature_switch)
if __name__ == "__main__":
typer.run(main) Try it:
|
Beta Was this translation helpful? Give feedback.
0 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.
-
First Check
Commit to Help
Example Code
Description
Is it possible to have multiple options map to a single parameter?
It's one of the basic patterns possible in Click, called Feature Switches: https://click.palletsprojects.com/en/8.0.x/options/#feature-switches
Given Typer's 1-to-1 option-to-parameter mapping, would this feature be impossible to implement in Typer? If not, what would the equivalent Typer code for the Click code above look like?
Operating System
Windows
Operating System Details
No response
Typer Version
0.4.0
Python Version
Python 3.10.0
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions