From 14b8957836290f46f473f39b104761889b2f0af0 Mon Sep 17 00:00:00 2001 From: "John R. Lenton" Date: Sat, 22 Sep 2018 21:47:51 +0100 Subject: [PATCH] This fixes issue #269 and #270 It changes the definition of `(*Option).canCli()` to also check whether an option is hidden. There's still some work to be done in this area as a group with no non-hidden options will also not render right, but I'll get to that later. --- help.go | 2 +- help_test.go | 20 +++++++++++++++++--- man.go | 2 +- option.go | 2 +- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/help.go b/help.go index 8e3eba9..c73e492 100644 --- a/help.go +++ b/help.go @@ -393,7 +393,7 @@ func (p *Parser) WriteHelp(writer io.Writer) { } for _, info := range grp.options { - if !info.canCli() || info.Hidden { + if !info.canCli() { continue } diff --git a/help_test.go b/help_test.go index bb76640..bc10f0f 100644 --- a/help_test.go +++ b/help_test.go @@ -26,6 +26,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 { @@ -47,6 +49,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"` @@ -88,7 +94,7 @@ func TestHelp(t *testing.T) { if runtime.GOOS == "windows" { expected = `Usage: - TestHelp [OPTIONS] [filename] [num] [hidden-in-help] + TestHelp [OPTIONS] [filename] [num] [hidden-in-help] Application Options: /v, /verbose Show verbose debug information @@ -131,11 +137,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] + TestHelp [OPTIONS] [filename] [num] [hidden-in-help] Application Options: -v, --verbose Show verbose debug information @@ -177,6 +184,7 @@ Arguments: num: A number Available commands: + bommand A command with only hidden options command A command (aliases: cm, cmd) ` } @@ -196,7 +204,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) @@ -274,6 +284,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 diff --git a/man.go b/man.go index c2cebae..ad1e939 100644 --- a/man.go +++ b/man.go @@ -54,7 +54,7 @@ func writeManPageOptions(wr io.Writer, grp *Group) { } for _, opt := range group.options { - if !opt.canCli() || opt.Hidden { + if !opt.canCli() { continue } diff --git a/option.go b/option.go index c681c39..f33cd8c 100644 --- a/option.go +++ b/option.go @@ -281,7 +281,7 @@ func (option *Option) set(value *string) error { } func (option *Option) canCli() bool { - return option.ShortName != 0 || len(option.LongName) != 0 + return !option.Hidden && (option.ShortName != 0 || len(option.LongName) != 0) } func (option *Option) canArgument() bool {