How do I pass all arguments in typer to a command?

881 Views Asked by At

I have a complex CLI with a number of commands, each of which uses a Typer instance as described in the docs. One of those commands runs another external command which has its own arguments, but also has a default behaviour if no arguments are passed. I would like to pass all arguments to that command without trying to define them explicitly in my code.

My code looks something like this, using banana as the external subcommand I want to run. In mycli.py:

app = typer.Typer()
app.add_typer(banana.app, name="banana", help="Run the banana command")

then in banana.py I want something like this:

app = typer.Typer()

def default(<with an optional list of arguments>):
   # TODO run the banana command with all arguments passed to it
   #   or nothing if no arguments were passed

More specifically, the command I am trying to run is inside a Docker image.

How do I do this?

1

There are 1 best solutions below

0
Dawngerpony On BEST ANSWER

I went through many iterations before I figured it out. In banana.py:

from typing import List, Optional
import docker
import typer
from typing_extensions import Annotated

@app.callback(invoke_without_command=True)
def main(commands: Annotated[Optional[List[str]], typer.Argument()] = None) -> None:
    # run the banana command with all arguments
    client = docker.from_env()
    client.containers.run(image, commands)

The above code (fully typed!) means I can run commands like the following:

mycli banana
mycli banana arg1
mycli banana arg1 arg2 arg3

and it all works as expected!

Suggestions for improvement would be greatly appreciated.

NB. This question is similar to this one but is not quite the same. And this issue was helpful too.