diff options
| author | Alex Flint <[email protected]> | 2022-10-29 12:29:07 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-10-29 12:29:07 -0700 |
| commit | 727f8533acca70ca429dce4bfea729a6af75c3f7 (patch) | |
| tree | 4f09f68ab87dbff2f2a40edc15ba85d38a8119be /usage_test.go | |
| parent | dbc2ba5d0c9a6a439d1f825f8c299fb276bbc911 (diff) | |
| parent | 3489ea5b2e9aa82dab4efc5e3f48fe6171f11ddd (diff) | |
Merge pull request #185 from alexflint/default-value-issue
Do not turn values intro strings and then back into values when processing default values
Diffstat (limited to 'usage_test.go')
| -rw-r--r-- | usage_test.go | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/usage_test.go b/usage_test.go index fd67fc8..be5894a 100644 --- a/usage_test.go +++ b/usage_test.go @@ -50,7 +50,7 @@ Options: --optimize OPTIMIZE, -O OPTIMIZE optimization level --ids IDS Ids - --values VALUES Values [default: [3.14 42 256]] + --values VALUES Values --workers WORKERS, -w WORKERS number of workers to start [default: 10, env: WORKERS] --testenv TESTENV, -a TESTENV [env: TEST_ENV] @@ -74,7 +74,6 @@ Options: } args.Name = "Foo Bar" args.Value = 42 - args.Values = []float64{3.14, 42, 256} args.File = &NameDotName{"scratch", "txt"} p, err := NewParser(Config{Program: "example"}, &args) require.NoError(t, err) @@ -506,7 +505,7 @@ Options: ShortOnly2 string `arg:"-b,--,required" help:"some help2"` } p, err := NewParser(Config{Program: "example"}, &args) - assert.NoError(t, err) + require.NoError(t, err) var help bytes.Buffer p.WriteHelp(&help) @@ -633,3 +632,35 @@ error: something went wrong assert.Equal(t, expectedStdout[1:], b.String()) assert.Equal(t, -1, exitCode) } + +type lengthOf struct { + Length int +} + +func (p *lengthOf) UnmarshalText(b []byte) error { + p.Length = len(b) + return nil +} + +func TestHelpShowsDefaultValueFromOriginalTag(t *testing.T) { + // check that the usage text prints the original string from the default tag, not + // the serialization of the parsed value + + expectedHelp := ` +Usage: example [--test TEST] + +Options: + --test TEST [default: some_default_value] + --help, -h display this help and exit +` + + var args struct { + Test *lengthOf `default:"some_default_value"` + } + 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()) +} |
