英文:
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="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
得分: 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) -> 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) -> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论