Skip to content

Commit d10a064

Browse files
authored
Merge pull request #262 from hhromic/fix-261
Fix help text for positional args with default and env var
2 parents a5045bb + bf156d1 commit d10a064

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

usage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (p *Parser) WriteHelpForSubcommand(w io.Writer, subcommand ...string) error
243243
if len(positionals) > 0 {
244244
fmt.Fprint(w, "\nPositional arguments:\n")
245245
for _, spec := range positionals {
246-
print(w, spec.placeholder, spec.help)
246+
print(w, spec.placeholder, spec.help, withDefault(spec.defaultString), withEnv(spec.env))
247247
}
248248
}
249249

usage_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -1015,3 +1015,72 @@ Commands:
10151015
p.WriteHelp(&help)
10161016
assert.Equal(t, expectedHelp[1:], help.String())
10171017
}
1018+
1019+
func TestHelpShowsPositionalWithDefault(t *testing.T) {
1020+
expectedHelp := `
1021+
Usage: example [FOO]
1022+
1023+
Positional arguments:
1024+
FOO this is a positional with a default [default: bar]
1025+
1026+
Options:
1027+
--help, -h display this help and exit
1028+
`
1029+
1030+
var args struct {
1031+
Foo string `arg:"positional" default:"bar" help:"this is a positional with a default"`
1032+
}
1033+
1034+
p, err := NewParser(Config{Program: "example"}, &args)
1035+
require.NoError(t, err)
1036+
1037+
var help bytes.Buffer
1038+
p.WriteHelp(&help)
1039+
assert.Equal(t, expectedHelp[1:], help.String())
1040+
}
1041+
1042+
func TestHelpShowsPositionalWithEnv(t *testing.T) {
1043+
expectedHelp := `
1044+
Usage: example [FOO]
1045+
1046+
Positional arguments:
1047+
FOO this is a positional with an env variable [env: FOO]
1048+
1049+
Options:
1050+
--help, -h display this help and exit
1051+
`
1052+
1053+
var args struct {
1054+
Foo string `arg:"positional,env:FOO" help:"this is a positional with an env variable"`
1055+
}
1056+
1057+
p, err := NewParser(Config{Program: "example"}, &args)
1058+
require.NoError(t, err)
1059+
1060+
var help bytes.Buffer
1061+
p.WriteHelp(&help)
1062+
assert.Equal(t, expectedHelp[1:], help.String())
1063+
}
1064+
1065+
func TestHelpShowsPositionalWithDefaultAndEnv(t *testing.T) {
1066+
expectedHelp := `
1067+
Usage: example [FOO]
1068+
1069+
Positional arguments:
1070+
FOO this is a positional with a default and an env variable [default: bar, env: FOO]
1071+
1072+
Options:
1073+
--help, -h display this help and exit
1074+
`
1075+
1076+
var args struct {
1077+
Foo string `arg:"positional,env:FOO" default:"bar" help:"this is a positional with a default and an env variable"`
1078+
}
1079+
1080+
p, err := NewParser(Config{Program: "example"}, &args)
1081+
require.NoError(t, err)
1082+
1083+
var help bytes.Buffer
1084+
p.WriteHelp(&help)
1085+
assert.Equal(t, expectedHelp[1:], help.String())
1086+
}

0 commit comments

Comments
 (0)