如何在 typer 中将所有参数传递给一个命令?

huangapple go评论60阅读模式
英文:

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

问题

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?

英文:

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=&quot;banana&quot;, help=&quot;Run the banana command&quot;)

then in banana.py I want something like this:

app = typer.Typer()

def default(&lt;with an optional list of arguments&gt;):
   # 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

得分: 1

我在找到解决方法之前进行了许多迭代。在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) -&gt; None:
    # 使用所有参数运行banana命令
    client = docker.from_env()
    client.containers.run(image, commands)

上述代码(完全类型化!)意味着我可以运行以下类似的命令:

mycli banana
mycli banana arg1
mycli banana arg1 arg2 arg3

一切都按预期工作!

对于改进的建议将不胜感激。

英文:

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) -&gt; 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.

huangapple
  • 本文由 发表于 2023年6月26日 17:10:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定