Python Typer one argument value required for another argument

432 Views Asked by At

I was trying CLI using typer, suppose I have two commands say -d and -s,

eg. python main.py -d apples -s red

  1. -d could be apple, oranges or banana
  2. -s could be green or red
  3. if -d is apple then -s must be present, but if -d is oranges or banana then -s is not required.
def app(
  device: str = typer.Option(
        None,
        "--device",
        "-d",
        help="Select the device you want to proceed with apples, oranges, banana",
        callback=__device_callback
        is_eager=True,
    ),
 soc: Optional[str] = typer.Option(
        "EFR",
        "--soc",
        "-s",
        help="Select the soc for the device type apples. [red or green]",
        is_eager=True,
    )      
):
    pass

if __name__ == "__main__":
    typer.run(app)

so if I use python main.py -d oranges -> it should run the callback as expected
if I use python main.py -d apples -> it should throw an error saying -s is required
if I use python main.py -d apples -s red -> it should run the callback as expected

  1. I want to know if this is possible with typer or not?
  2. If it is possible I need suggestions so I can develop the program
0

There are 0 best solutions below