urfave / cli

A simple, fast, and fun package for building command line apps in Go
https://cli.urfave.org
MIT License
21.9k stars 1.69k forks source link

[v2] Is there any way to customize the format of flag's help text? #1837

Closed Karmenzind closed 6 months ago

Karmenzind commented 6 months ago

Hi,

I checked the manual website but still have no idea how to change the format of GLOBAL OPTIONS.

Now the help text are displayed as:

GLOBAL OPTIONS
   --edit-config            edit configuration file with
 the default editor edit configuration file with the def
ault editor edit configuration file with the default edi
tor edit configuration file with the default editor
   --help, -h               show help

I want to add some newlines and indention, like some linux manpages: image

How can I do this? Thanks!

dearchap commented 6 months ago

@Karmenzind what version of v2 are you using ? Also what are you setting the flag UsageText to ?

Karmenzind commented 6 months ago

Thanks for your reply. I just updated to 2.26.0 but got the same output:

image

I need to display English and Chinese text, so right align might look better.

Here's my code:

var um = map[string]string{
    "text":            "translate long query `TEXT` with e.g. --text=\"Long time ago\" 翻译长句",
    "nocache":         "don't use cached result 不使用本地词库,查询网络结果",
    "theme":           "choose the color theme for current query 选择颜色主题,仅当前查询生效",
    "init":            "initialize shell completion 初始化部分设置,例如shell的自动补全",
    "server":          "start server foreground 在前台启动服务端",
    "daemon":          "ensure/start the daemon process 启动守护进程",
    "stop":            "stop the daemon process 停止守护进程",
    "update":          "check and update kd client 更新kd的可执行文件",
    "generate-config": "generate config sample 生成配置文件,默认地址为`~/.config/kd.toml`",
        "edit-config":     "edit configuration file with the default editor 用默认编辑器打开配置文件",
}

// ...create cli.App

&cli.BoolFlag{Name: "init", DisableDefaultText: true, Hidden: true, Usage: um["init"]},
&cli.BoolFlag{Name: "server", DisableDefaultText: true, Action: flagServer, Usage: um["server"]},
&cli.BoolFlag{Name: "daemon", DisableDefaultText: true, Action: flagDaemon, Usage: um["daemon"]},
&cli.BoolFlag{Name: "stop", DisableDefaultText: true, Hidden: true, Action: flagStop, Usage: um["stop"]},
&cli.BoolFlag{Name: "update", DisableDefaultText: true, Action: flagUpdate, Usage: um["update"]},
&cli.BoolFlag{Name: "generate-config", DisableDefaultText: true, Action: flagGenerateConfig, Usage: um["generate-config"]},
&cli.BoolFlag{Name: "edit-config", DisableDefaultText: true, Action: flagEditConfig, Usage: um["edit-config"]},

I tried inserting three \ns to the Usage like:

"\nedit configuration file with the default editor\n用默认编辑器打开配置文件\n"

But the first and the last one didn't work: image

Seems that there is a Trim in your code. Maybe you can remove the Trim or only trim white spaces.

dearchap commented 6 months ago

@Karmenzind You could define your Usage as a multiline string for example

&cli.StringFlag{
   Name: "foo",
   Usage: `foo1foo2
  bar1bar2
        `,
}

and the output will be something like

GLOBAL OPTIONS:
   --foo value  foo1foo2
                bar1bar2

In the absence of that you would have to define a custom template and write some formatting yourself

Karmenzind commented 6 months ago

Thank you.

Inserting 2 tabs before the second line bar1bar2 did align the text. But it cannot automatically wrap the long text. I will try to customize the template later.

Hope you can make these variables public then users can rewrite a small part instead of the whole template.

Karmenzind commented 6 months ago

BTW, will the official v3 be released in the near future?

dearchap commented 6 months ago

@Karmenzind Making something public is not something we would do lightly. If enough users requests them then perhaps. Also v2 is in maintenance mode so its probably not the best to make the change here. v3 is in alpha mode but you can use the releases right away.

Karmenzind commented 6 months ago

Thanks.