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

Fix help text for positional args with default and env var #262

Merged
merged 1 commit into from
Sep 5, 2024
Merged
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
2 changes: 1 addition & 1 deletion usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (p *Parser) WriteHelpForSubcommand(w io.Writer, subcommand ...string) error
if len(positionals) > 0 {
fmt.Fprint(w, "\nPositional arguments:\n")
for _, spec := range positionals {
print(w, spec.placeholder, spec.help)
print(w, spec.placeholder, spec.help, withDefault(spec.defaultString), withEnv(spec.env))
}
}

Expand Down
69 changes: 69 additions & 0 deletions usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,3 +1015,72 @@ Commands:
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())
}

func TestHelpShowsPositionalWithDefault(t *testing.T) {
expectedHelp := `
Usage: example [FOO]

Positional arguments:
FOO this is a positional with a default [default: bar]

Options:
--help, -h display this help and exit
`

var args struct {
Foo string `arg:"positional" default:"bar" help:"this is a positional with a default"`
}

p, err := NewParser(Config{Program: "example"}, &args)
require.NoError(t, err)

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())
}

func TestHelpShowsPositionalWithEnv(t *testing.T) {
expectedHelp := `
Usage: example [FOO]

Positional arguments:
FOO this is a positional with an env variable [env: FOO]

Options:
--help, -h display this help and exit
`

var args struct {
Foo string `arg:"positional,env:FOO" help:"this is a positional with an env variable"`
}

p, err := NewParser(Config{Program: "example"}, &args)
require.NoError(t, err)

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())
}

func TestHelpShowsPositionalWithDefaultAndEnv(t *testing.T) {
expectedHelp := `
Usage: example [FOO]

Positional arguments:
FOO this is a positional with a default and an env variable [default: bar, env: FOO]

Options:
--help, -h display this help and exit
`

var args struct {
Foo string `arg:"positional,env:FOO" default:"bar" help:"this is a positional with a default and an env variable"`
}

p, err := NewParser(Config{Program: "example"}, &args)
require.NoError(t, err)

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())
}
Loading