From ac7590cca756091072bd422cc8380a0eadc4f18f Mon Sep 17 00:00:00 2001 From: Sebastiaan Pasterkamp <26205277+SebastiaanPasterkamp@users.noreply.github.com> Date: Sat, 10 Dec 2022 15:29:56 +0100 Subject: [PATCH] feat(help): Include environment-only variables The environment only variables don't accept command line options, but should still be listed in the help message. Instead of listing the command line flags the placeholder text '(environment only)' is displayed. --- README.md | 1 + example_test.go | 1 + usage.go | 9 +++++++-- usage_test.go | 3 +++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a891a61..60d6121 100644 --- a/README.md +++ b/README.md @@ -634,6 +634,7 @@ Change the default registry to push to. --host HOST URL of the repository [default: docker.io] --user USER username to connect as [env: REPO_USERNAME] + (environment only) password to connect with [env: REPO_PASSWORD] Global options: --quiet, -q Quiet diff --git a/example_test.go b/example_test.go index 2bd74a2..0e21589 100644 --- a/example_test.go +++ b/example_test.go @@ -296,6 +296,7 @@ func Example_helpTextWithGroups() { // // --host HOST URL of the repository [default: docker.io] // --user USER username to connect as [env: REPO_USERNAME] + // (environment only) password to connect with [env: REPO_PASSWORD] // // Global options: // --quiet, -q Quiet diff --git a/usage.go b/usage.go index e8a1d49..b18e6f2 100644 --- a/usage.go +++ b/usage.go @@ -285,7 +285,7 @@ func (p *Parser) writeHelpForSubcommand(w io.Writer, cmd *command) { // writeHelpForArguments writes the list of short, long, and environment-only // options in order. func (p *Parser) writeHelpForArguments(w io.Writer, cmd *command, header, help string) { - var positionals, longOptions, shortOptions []*spec + var positionals, longOptions, shortOptions, envOnly []*spec for _, spec := range cmd.options { switch { case spec.positional: @@ -294,10 +294,12 @@ func (p *Parser) writeHelpForArguments(w io.Writer, cmd *command, header, help s longOptions = append(longOptions, spec) case spec.short != "": shortOptions = append(shortOptions, spec) + case spec.env != "": + envOnly = append(envOnly, spec) } } - if cmd.parent != nil && len(shortOptions)+len(longOptions) == 0 { + if cmd.parent != nil && len(shortOptions)+len(longOptions)+len(envOnly) == 0 { return } @@ -312,6 +314,9 @@ func (p *Parser) writeHelpForArguments(w io.Writer, cmd *command, header, help s for _, spec := range longOptions { p.printOption(w, spec) } + for _, spec := range envOnly { + p.printOption(w, spec) + } // write the list of argument groups if len(cmd.groups) > 0 { diff --git a/usage_test.go b/usage_test.go index 0350521..7add765 100644 --- a/usage_test.go +++ b/usage_test.go @@ -510,6 +510,7 @@ This block represents related arguments. --host HOST hostname to connect to [default: localhost, env: DB_HOST] --port PORT port to connect to [default: 3306, env: DB_PORT] --user USER username to connect as [env: DB_USERNAME] + (environment only) password to connect with [env: DB_PASSWORD] Global options: --help, -h display this help and exit @@ -746,6 +747,8 @@ Usage: example [-s SHORT] Options: -s SHORT [env: SHORT] + (environment only) [env: ENVONLY] + (environment only) [env: CUSTOM] --help, -h display this help and exit ` var args struct {