Skip to content

Commit

Permalink
Merge pull request #271 from chipaca/fixes-for-hidden-bugs
Browse files Browse the repository at this point in the history
This fixes issue #269 and #270
  • Loading branch information
jessevdk committed Sep 23, 2018
2 parents ebe2690 + d761e61 commit c0bdd90
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (c *Command) match(name string) bool {
return false
}

func (c *Command) hasCliOptions() bool {
func (c *Command) hasHelpOptions() bool {
ret := false

c.eachGroup(func(g *Group) {
Expand All @@ -447,7 +447,7 @@ func (c *Command) hasCliOptions() bool {
}

for _, opt := range g.options {
if opt.canCli() {
if opt.showInHelp() {
ret = true
}
}
Expand Down
6 changes: 3 additions & 3 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (p *Parser) getAlignmentInfo() alignmentInfo {
}

for _, info := range grp.options {
if !info.canCli() {
if !info.showInHelp() {
continue
}

Expand Down Expand Up @@ -305,7 +305,7 @@ func (p *Parser) WriteHelp(writer io.Writer) {
}
} else if us, ok := allcmd.data.(Usage); ok {
usage = us.Usage()
} else if allcmd.hasCliOptions() {
} else if allcmd.hasHelpOptions() {
usage = fmt.Sprintf("[%s-OPTIONS]", allcmd.Name)
}

Expand Down Expand Up @@ -393,7 +393,7 @@ func (p *Parser) WriteHelp(writer io.Writer) {
}

for _, info := range grp.options {
if !info.canCli() || info.Hidden {
if !info.showInHelp() {
continue
}

Expand Down
20 changes: 17 additions & 3 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type helpOptions struct {
OptionWithChoices string `long:"opt-with-choices" value-name:"choice" choice:"dog" choice:"cat" description:"Option with choices"`
Hidden string `long:"hidden" description:"Hidden option" hidden:"yes"`

HiddenOptionWithVeryLongName bool `long:"this-hidden-option-has-a-ridiculously-long-name" hidden:"yes"`

OnlyIni string `ini-name:"only-ini" description:"Option only available in ini"`

Other struct {
Expand All @@ -48,6 +50,10 @@ type helpOptions struct {
} `group:"Subsubgroup" namespace:"sap"`
} `group:"Subgroup" namespace:"sip"`

Bommand struct {
Hidden bool `long:"hidden" description:"A hidden option" hidden:"yes"`
} `command:"bommand" description:"A command with only hidden options"`

Command struct {
ExtraVerbose []bool `long:"extra-verbose" description:"Use for extra verbosity"`
} `command:"command" alias:"cm" alias:"cmd" description:"A command"`
Expand Down Expand Up @@ -89,7 +95,7 @@ func TestHelp(t *testing.T) {

if runtime.GOOS == "windows" {
expected = `Usage:
TestHelp [OPTIONS] [filename] [num] [hidden-in-help] <command>
TestHelp [OPTIONS] [filename] [num] [hidden-in-help] <bommand | command>
Application Options:
/v, /verbose Show verbose debug information
Expand Down Expand Up @@ -132,11 +138,12 @@ Arguments:
num: A number
Available commands:
bommand A command with only hidden options
command A command (aliases: cm, cmd)
`
} else {
expected = `Usage:
TestHelp [OPTIONS] [filename] [num] [hidden-in-help] <command>
TestHelp [OPTIONS] [filename] [num] [hidden-in-help] <bommand | command>
Application Options:
-v, --verbose Show verbose debug information
Expand Down Expand Up @@ -178,6 +185,7 @@ Arguments:
num: A number
Available commands:
bommand A command with only hidden options
command A command (aliases: cm, cmd)
`
}
Expand All @@ -197,7 +205,9 @@ func TestMan(t *testing.T) {
p.LongDescription = "This is a somewhat `longer' description of what this does"
p.AddGroup("Application Options", "The application options", &opts)

p.Commands()[0].LongDescription = "Longer `command' description"
for _, cmd := range p.Commands() {
cmd.LongDescription = fmt.Sprintf("Longer `%s' description", cmd.Name)
}

var buf bytes.Buffer
p.WriteManPage(&buf)
Expand Down Expand Up @@ -275,6 +285,10 @@ Not hidden inside group
\fB\fB\-\-sip.sap.opt\fR\fP
This is a subsubgroup option
.SH COMMANDS
.SS bommand
A command with only hidden options
Longer \fBbommand\fP description
.SS command
A command
Expand Down
6 changes: 3 additions & 3 deletions man.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func writeManPageOptions(wr io.Writer, grp *Group) {
}

for _, opt := range group.options {
if !opt.canCli() || opt.Hidden {
if !opt.showInHelp() {
continue
}

Expand Down Expand Up @@ -148,12 +148,12 @@ func writeManPageCommand(wr io.Writer, name string, root *Command, command *Comm
var usage string
if us, ok := command.data.(Usage); ok {
usage = us.Usage()
} else if command.hasCliOptions() {
} else if command.hasHelpOptions() {
usage = fmt.Sprintf("[%s-OPTIONS]", command.Name)
}

var pre string
if root.hasCliOptions() {
if root.hasHelpOptions() {
pre = fmt.Sprintf("%s [OPTIONS] %s", root.Name, command.Name)
} else {
pre = fmt.Sprintf("%s %s", root.Name, command.Name)
Expand Down
4 changes: 2 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ func (option *Option) set(value *string) error {
return convert("", option.value, option.tag)
}

func (option *Option) canCli() bool {
return option.ShortName != 0 || len(option.LongName) != 0
func (option *Option) showInHelp() bool {
return !option.Hidden && (option.ShortName != 0 || len(option.LongName) != 0)
}

func (option *Option) canArgument() bool {
Expand Down

0 comments on commit c0bdd90

Please sign in to comment.