Skip to content

Commit

Permalink
Do not skip groups that are commands when working out alignment
Browse files Browse the repository at this point in the history
Without this change, groups that are commands but that don't satisfy
(*Group).showInHelp() would be skipped when working out the alignment,
which would result in negative lengths when trying to process a group
with only positional options.

This fixes jessevdk#280.
  • Loading branch information
chipaca committed Oct 10, 2018
1 parent 9c49a33 commit e420627
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 7 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,16 @@ func (p *Parser) getAlignmentInfo() alignmentInfo {
ret.terminalColumns = 80
}

isCmd := map[*Group]bool{}
p.eachCommand(func(cmd *Command) {
// TODO: have (*Command).showInHelp() to consider hidden vs active
isCmd[cmd.Group] = true
}, true)

var prevcmd *Command

p.eachActiveGroup(func(c *Command, grp *Group) {
if !grp.showInHelp() {
if !(isCmd[grp] || grp.showInHelp()) {
return
}
if c != prevcmd {
Expand Down
20 changes: 16 additions & 4 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,10 @@ func TestWroteHelp(t *testing.T) {
isHelp bool
}
tests := map[string]testInfo{
"No error": testInfo{value: nil, isHelp: false},
"Plain error": testInfo{value: errors.New("an error"), isHelp: false},
"ErrUnknown": testInfo{value: newError(ErrUnknown, "an error"), isHelp: false},
"ErrHelp": testInfo{value: newError(ErrHelp, "an error"), isHelp: true},
"No error": {value: nil, isHelp: false},
"Plain error": {value: errors.New("an error"), isHelp: false},
"ErrUnknown": {value: newError(ErrUnknown, "an error"), isHelp: false},
"ErrHelp": {value: newError(ErrHelp, "an error"), isHelp: true},
}

for name, test := range tests {
Expand All @@ -578,3 +578,15 @@ func TestWroteHelp(t *testing.T) {
})
}
}

func TestOnlyPositional(t *testing.T) {
type options struct {
Positional struct {
Bar string `description:"bar"`
} `positional-args:"yes"`
}

var buf bytes.Buffer
NewParser(&options{}, 0).WriteHelp(&buf)
// just checking for no panic, at this point
}

0 comments on commit e420627

Please sign in to comment.