From e4206272b65d1ff6e9e3114e770d8ae8aa5372b2 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Wed, 10 Oct 2018 15:44:59 +0100 Subject: [PATCH] Do not skip groups that are commands when working out alignment 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 #280. --- help.go | 8 +++++++- help_test.go | 20 ++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/help.go b/help.go index 068fce1..f5e155c 100644 --- a/help.go +++ b/help.go @@ -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 { diff --git a/help_test.go b/help_test.go index 6d7798d..c2d8a0b 100644 --- a/help_test.go +++ b/help_test.go @@ -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 { @@ -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 +}