禁用 Typer 中的丰富帮助面板。

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

Disable rich help panel in Typer

问题

我正在尝试使用 typer 来构建一个命令行应用程序。我还在使用 rich 来进行一些格式化。然而,安装了 rich 导致 typer 使用了 rich_help_panel。我想要禁用它,而是希望使用正常的帮助字符串格式。我该如何实现这一点?

我迄今为止尝试过的内容:

import typer
from typing_extensions import Annotated

cli = typer.Typer(rich_help_panel=None)


@cli.command()
def multiply(x: int, y: int, exp: bool = False):
    """
    Multiply two numbers.
    """
    if exp:
        return print(x**y)
    return print(x * y)


@cli.command()
def sum(x: int, y: int):
    """
    Sum two numbers.
    """
    return print(x + y)


@cli.callback()
def main():
    "Does arithmetic"


if __name__ == "__main__":
    cli()
英文:

I'm trying to use typer to build a CLI application. I'm also using rich to do some formatting. However having rich installed leads to typer using rich_help_panel. I would like to disable that. I would prefer the normal formatting of the help string. How can I achieve that?

What I have tried so far:

import typer
from typing_extensions import Annotated

cli = typer.Typer(rich_help_panel=None)


@cli.command()
def multiply(x: int, y: int, exp: bool = False):
    """
    Multiply two numbers.
    """
    if exp:
        return print(x**y)
    return print(x * y)


@cli.command()
def sum(x: int, y: int):
    """
    Sum two numbers.
    """
    return print(x + y)


@cli.callback()
def main():
    "Does arithmetic"


if __name__ == "__main__":
    cli()

答案1

得分: 1

参数 rich_help_panel 用于设置如此处所述的 rich 面板标题文本。您正在寻找的功能目前还不存在,至少目前还没有。我已经提出了一个 pull request,以添加一个用于全局禁用 rich 帮助文本和 rich 追踪的设置器。如果有人阅读这篇文章想要合并这个功能,我建议根据 这里 的说明来审查我的 PR。

英文:

The parameter rich_help_panel is for setting the rich panel title text as described here. The functionality you are looking for doesn't exist. At least not yet. I made a pull request to add a setter to globally disable the rich help text and the rich traceback respectively.

If someone reading this wants to get this merged I would suggest to write a review of my PR according to this.

huangapple
  • 本文由 发表于 2023年7月3日 08:33:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601266.html
匿名

发表评论

匿名网友

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

确定