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.
This commit is contained in:
Sebastiaan Pasterkamp 2022-12-10 15:29:56 +01:00
parent 97d1ef3a3c
commit ac7590cca7
4 changed files with 12 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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 {