Skip to content

Commit

Permalink
Fix help text for positional args with default and env var
Browse files Browse the repository at this point in the history
  • Loading branch information
hhromic committed Jul 22, 2024
1 parent 3de7278 commit 1b14285
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
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 [NUMBER]
Positional arguments:
NUMBER this is a positional with a default [default: foo]
Options:
--help, -h display this help and exit
`

var args struct {
Number string `arg:"positional" default:"foo" 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 [NUMBER]
Positional arguments:
NUMBER this is a positional with an env variable [env: NUMBER]
Options:
--help, -h display this help and exit
`

var args struct {
Number string `arg:"positional,env:NUMBER" 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 [NUMBER]
Positional arguments:
NUMBER this is a positional with a default and an env variable [default: foo, env: NUMBER]
Options:
--help, -h display this help and exit
`

var args struct {
Number string `arg:"positional,env:NUMBER" default:"foo" 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())
}

0 comments on commit 1b14285

Please sign in to comment.