spf13 / cobra

A Commander for modern Go CLI interactions
https://cobra.dev
Apache License 2.0
37.59k stars 2.83k forks source link

Wrapping `--help` output lines to 80 (or whatever) characters #810

Open farnasirim opened 5 years ago

farnasirim commented 5 years ago
      --smtp-server-host string   SMTP server host address to use for sending emails. Default: smtp.gmail.com                                                            
      --smtp-to string            Address to send the diff email to. Defaults to smtp-user

I had a look around the code and figured that this is not supported. Was wondering if you would accept a patch.

This is not very trivial to do right now as the padding that comes before the command descriptions is dependent on the longest flag name + type and calculating the padding and repadding subsequent lines is cumbersome for the user.

Therefore I think it makes more sense to handle this inside cobra.

github-actions[bot] commented 4 years ago

This issue is being marked as stale due to a long period of inactivity

johnSchnake commented 2 years ago

Having worked on a few different tools I agree that the general text-wrapping problem is frustrating for every dev to handle and if cobra could do it for the help/usage it would make a lot of sense.

The main issue is that there are lots of different approaches/libraries that help so it may be a bit opinionated. Open to a PR for sure though.

znewman01 commented 1 year ago

kubectl has a printer here that might be a useful starting point: https://github.com/kubernetes/kubernetes/pull/104736

jdogmcsteezy commented 1 month ago

If someone wants something quick and dirty:

    width, _, _ := term.GetSize(int(os.Stdout.Fd()))
    usageTemplate := rootCmd.UsageTemplate()
    usageTemplate = strings.ReplaceAll(usageTemplate, ".FlagUsages", fmt.Sprintf(".FlagUsagesWrapped %d", width))
        rootCmd.SetUsageTemplate(usageTemplate)