summaryrefslogtreecommitdiff
path: root/usage_test.go
diff options
context:
space:
mode:
authorHugo Hromic <[email protected]>2024-07-22 19:18:14 +0100
committerHugo Hromic <[email protected]>2024-07-22 19:25:51 +0100
commitbf156d17a378826a4832340c79f07aab30578fa8 (patch)
tree023242966179c40641f04758a3f43b2ec7c8d5c7 /usage_test.go
parent3de7278c4f091f643033f3ebc8fe670c6e7ff7dd (diff)
Fix help text for positional args with default and env var
Diffstat (limited to 'usage_test.go')
-rw-r--r--usage_test.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/usage_test.go b/usage_test.go
index a958abb..fa9ace3 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())
+}