Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help: output env var info, even if desc is unset #383

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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"`
}

Expand Down