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

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

Disable rich help panel in Typer

问题

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

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

  1. import typer
  2. from typing_extensions import Annotated
  3. cli = typer.Typer(rich_help_panel=None)
  4. @cli.command()
  5. def multiply(x: int, y: int, exp: bool = False):
  6. """
  7. Multiply two numbers.
  8. """
  9. if exp:
  10. return print(x**y)
  11. return print(x * y)
  12. @cli.command()
  13. def sum(x: int, y: int):
  14. """
  15. Sum two numbers.
  16. """
  17. return print(x + y)
  18. @cli.callback()
  19. def main():
  20. "Does arithmetic"
  21. if __name__ == "__main__":
  22. 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:

  1. import typer
  2. from typing_extensions import Annotated
  3. cli = typer.Typer(rich_help_panel=None)
  4. @cli.command()
  5. def multiply(x: int, y: int, exp: bool = False):
  6. """
  7. Multiply two numbers.
  8. """
  9. if exp:
  10. return print(x**y)
  11. return print(x * y)
  12. @cli.command()
  13. def sum(x: int, y: int):
  14. """
  15. Sum two numbers.
  16. """
  17. return print(x + y)
  18. @cli.callback()
  19. def main():
  20. "Does arithmetic"
  21. if __name__ == "__main__":
  22. 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:

确定