From 151e28cb4515ba1aa85f7cc97dfb8a8edd182d32 Mon Sep 17 00:00:00 2001 From: Torkel Rogstad Date: Thu, 17 Feb 2022 12:52:47 +0100 Subject: [PATCH] help: output env var info, even if desc is unset Prior to this, description had to be set for environment variable information to be printed. --- help.go | 55 +++++++++++++++++++++++++++------------------------- help_test.go | 3 +++ 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/help.go b/help.go index 8fd3244..77b7f77 100644 --- a/help.go +++ b/help.go @@ -213,38 +213,41 @@ func (p *Parser) writeHelpOption(writer *bufio.Writer, option *Option, info alig written := line.Len() line.WriteTo(writer) - if option.Description != "" { - dw := descstart - written - writer.WriteString(strings.Repeat(" ", dw)) - - var def string - - if len(option.DefaultMask) != 0 { - if option.DefaultMask != "-" { - def = option.DefaultMask - } - } else { - def = option.defaultLiteral + // start out with just the description + desc := option.Description + + // if we have a default value, add that + if option.defaultLiteral != "" { + def := option.defaultLiteral + + // nothing should be displayed for the default value + if option.DefaultMask == "-" { + def = "" + } else if len(option.DefaultMask) != 0 { + def = option.DefaultMask } - var envDef string - if option.EnvKeyWithNamespace() != "" { - var envPrintable string - if runtime.GOOS == "windows" { - envPrintable = "%" + option.EnvKeyWithNamespace() + "%" - } else { - envPrintable = "$" + option.EnvKeyWithNamespace() - } - envDef = fmt.Sprintf(" [%s]", envPrintable) + if def != "" { + desc += fmt.Sprintf(" (default: %s)", def) } + } - var desc string - - if def != "" { - desc = fmt.Sprintf("%s (default: %v)%s", option.Description, def, envDef) + // if we have env vars, add that + if option.EnvKeyWithNamespace() != "" { + var envPrintable string + if runtime.GOOS == "windows" { + envPrintable = "%" + option.EnvKeyWithNamespace() + "%" } else { - desc = option.Description + envDef + envPrintable = "$" + option.EnvKeyWithNamespace() } + desc += fmt.Sprintf(" [%s]", envPrintable) + } + + // lastly, write the actual string + if desc != "" { + // but first, apply padding + dw := descstart - written + writer.WriteString(strings.Repeat(" ", dw)) writer.WriteString(wrapText(desc, info.terminalColumns-descstart, diff --git a/help_test.go b/help_test.go index c713bba..22f9b1d 100644 --- a/help_test.go +++ b/help_test.go @@ -485,6 +485,7 @@ func TestHelpDefaults(t *testing.T) { Application Options: /with-default: With default (default: default-value) /without-default: Without default + /without-default-with-env= [%WITH_ENV%] /with-programmatic-default: With programmatic default (default: default-value) @@ -499,6 +500,7 @@ Help Options: Application Options: --with-default= With default (default: default-value) --without-default= Without default + --without-default-with-env= [$WITH_ENV] --with-programmatic-default= With programmatic default (default: default-value) @@ -525,6 +527,7 @@ Help Options: var opts struct { WithDefault string `long:"with-default" default:"default-value" description:"With default"` WithoutDefault string `long:"without-default" description:"Without default"` + WithoutDefaultWithEnv string `long:"without-default-with-env" env:"WITH_ENV"` WithProgrammaticDefault string `long:"with-programmatic-default" description:"With programmatic default"` }