summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2024-09-05 17:20:09 -0400
committerGitHub <[email protected]>2024-09-05 17:20:09 -0400
commitd10a064207fecad73eb3a19b93fead8d566bf1e1 (patch)
tree7589226a8c8757df37b97336d5e2628c32e7bb3a
parenta5045bbe852cb542c8fab73aada516dbb32c7f2e (diff)
parentbf156d17a378826a4832340c79f07aab30578fa8 (diff)
Merge pull request #262 from hhromic/fix-261
Fix help text for positional args with default and env var
-rw-r--r--usage.go2
-rw-r--r--usage_test.go69
2 files changed, 70 insertions, 1 deletions
diff --git a/usage.go b/usage.go
index b84aa10..d091bcb 100644
--- a/usage.go
+++ b/usage.go
@@ -243,7 +243,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))
}
}
diff --git a/usage_test.go b/usage_test.go
index fc8aaff..e276e1a 100644
--- a/usage_test.go
+++ b/usage_test.go
@@ -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())
+}