You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Typer中禁用rich_help_panel,恢复默认帮助格式?

解决Typer自动启用Rich帮助面板的问题

要禁用Typer自动启用的Rich帮助格式,恢复默认的帮助字符串样式,你需要在创建Typer实例时设置rich_markup_mode=None,而非rich_help_panel=None——rich_help_panel仅用于控制Rich帮助面板的标题名称,无法完全禁用Rich的格式化效果。

修改后的代码如下:

import typer

# 设置 rich_markup_mode=None 禁用Rich帮助格式化
cli = typer.Typer(rich_markup_mode=None)


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


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


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


if __name__ == "__main__":
    cli()

补充说明:

  • rich_markup_mode=None会强制Typer使用原生默认的帮助文本格式,不再解析Rich标记语法,也不会启用Rich帮助面板。
  • 该设置仅影响帮助文本的展示,不会干扰你在代码中使用Rich进行其他终端输出的格式化处理。

内容的提问来源于stack exchange,提问作者suvayu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 00:03:09