from typing import List, Optional
import typer
def main(device: Optional[List[str]] = typer.Option(None)):
if not device:
print("No provided devices")
raise typer.Abort()
for d in device:
print(f"Processing device: {d}")
if __name__ == "__main__":
typer.run(main)
And then it can be used like in:
$ ./my_cli --device 1 --device 2
Take a look at the documentation.
If the only input for the CLI is a list of devices, you can simplify it to using arguments instead. Then it could be used like:
$ ./my_cli 1 2
Answer from ericbn on Stack Overflowfrom typing import List, Optional
import typer
def main(device: Optional[List[str]] = typer.Option(None)):
if not device:
print("No provided devices")
raise typer.Abort()
for d in device:
print(f"Processing device: {d}")
if __name__ == "__main__":
typer.run(main)
And then it can be used like in:
$ ./my_cli --device 1 --device 2
Take a look at the documentation.
If the only input for the CLI is a list of devices, you can simplify it to using arguments instead. Then it could be used like:
$ ./my_cli 1 2
from pathlib import Path
from typing import List
import typer
def main(files: List[Path], celebration: str):
for path in files:
if path.is_file():
print(f"This file exists: {path.name}")
print(celebration)
if __name__ == "__main__":
typer.run(main)
Apparently there is a way. Not sure if this is how they meant to use it, but this works for most.
main.py
import typer
import mycommand
app = typer.Typer()
app.command()(mycommand.yourcommand)
@app.command()
def othercmd():
pass
if __name__ == '__main__':
app()
and your other module may look like below. The need of typer decorators also vanish.
mycommand.py
from typing import List
import typer
def yourcommand(files: List[str] = typer.Argument(...), name: str = typer.Option(None)):
if name: print(f'Hello {name}')
print(files)
This Github Issue Post is what you maybe looking for, though I've put the solution out anyway.
This can be achieved as of version 0.15.0 by not supplying a name when calling add_typer. Source.
main.py
import typer
import other
app = typer.Typer()
app.add_typer(other.app)
other.py
import typer
app = typer.Typer()
@app.command()
def foo():
pass