Skip right column if the left is empty

This commit is contained in:
Andrew Morozko 2020-12-20 03:51:33 +03:00
parent 04c3fdbd80
commit 438a91dba1
2 changed files with 23 additions and 0 deletions

View File

@ -117,6 +117,9 @@ func (p *Parser) writeUsageForCommand(w io.Writer, cmd *command) {
}
func printTwoCols(w io.Writer, left, help string, defaultVal string, envVal string) {
if left == "" {
return
}
lhs := " " + left
fmt.Fprint(w, lhs)
if help != "" {

View File

@ -328,3 +328,23 @@ Options:
p.WriteHelp(&help)
assert.Equal(t, expectedHelp, help.String())
}
func TestUsageWithEnvOptions(t *testing.T) {
expectedHelp := `Usage: example [-s SHORT]
Options:
-s SHORT [env: SHORT]
--help, -h display this help and exit
`
var args struct {
Short string `arg:"--,-s,env"`
EnvOnly string `arg:"--,env"`
EnvOnlyOverriden string `arg:"--,env:CUSTOM"`
}
p, err := NewParser(Config{Program: "example"}, &args)
assert.NoError(t, err)
var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp, help.String())
}