add test for help and usage when a --version flag is present

This commit is contained in:
Alex Flint 2024-04-02 12:05:00 -04:00
parent 68948b2ac1
commit 3ddfffdcd3
1 changed files with 33 additions and 0 deletions

View File

@ -260,6 +260,39 @@ Options:
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
}
type userDefinedVersionFlag struct {
ShowVersion bool `arg:"--version" help:"this is a user-defined version flag"`
}
// Version returns the version for this program
func (userDefinedVersionFlag) Version() string {
return "example 3.2.1"
}
func TestUsageWithUserDefinedVersionFlag(t *testing.T) {
expectedUsage := "example 3.2.1\nUsage: example [--version]"
expectedHelp := `
example 3.2.1
Usage: example [--version]
Options:
--version this is a user-defined version flag
--help, -h display this help and exit
`
os.Args[0] = "example"
p, err := NewParser(Config{}, &userDefinedVersionFlag{})
require.NoError(t, err)
var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())
var usage bytes.Buffer
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
}
type described struct{}
// Described returns the description for this program