Skip to content

Commit

Permalink
Add "Hidden" option to comamnds and options
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Mar 31, 2015
1 parent 5e11878 commit f59c328
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions command.go
Expand Up @@ -23,6 +23,9 @@ type Command struct {
// Whether positional arguments are required
ArgsRequired bool

// If true, the option is not displayed in the help output
Hidden bool

commands []*Command
hasBuiltinHelpGroup bool
args []*Arg
Expand Down
10 changes: 8 additions & 2 deletions command_private.go
Expand Up @@ -201,9 +201,15 @@ func (c commandList) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}

// FIXME: maybe call this sortedVisibleCommands ?
func (c *Command) sortedCommands() []*Command {
ret := make(commandList, len(c.commands))
copy(ret, c.commands)
ret := make(commandList, 0, len(c.commands))

for _, e := range c.commands {
if !e.Hidden {
ret = append(ret, e)
}
}

sort.Sort(ret)
return []*Command(ret)
Expand Down
1 change: 1 addition & 0 deletions flags.go
Expand Up @@ -104,6 +104,7 @@ The following is a list of tags for struct fields supported by go-flags:
slices and maps (optional)
value-name: the name of the argument value (to be shown in the help)
(optional)
hidden: the option is not visible in the help output
base: a base (radix) used to convert strings to integer values, the
default base is 10 (i.e. decimal) (optional)
Expand Down
2 changes: 2 additions & 0 deletions group_private.go
Expand Up @@ -131,6 +131,7 @@ func (g *Group) scanStruct(realval reflect.Value, sfield *reflect.StructField, h

optional := (mtag.Get("optional") != "")
required := (mtag.Get("required") != "")
hidden := (mtag.Get("hidden") != "")

option := &Option{
Description: description,
Expand All @@ -144,6 +145,7 @@ func (g *Group) scanStruct(realval reflect.Value, sfield *reflect.StructField, h
Required: required,
ValueName: valueName,
DefaultMask: defaultMask,
Hidden: hidden,

group: g,

Expand Down
4 changes: 4 additions & 0 deletions help.go
Expand Up @@ -108,6 +108,10 @@ func (p *Parser) writeHelpOption(writer *bufio.Writer, option *Option, info alig
prefix += 4
}

if option.Hidden {
return
}

line.WriteString(strings.Repeat(" ", prefix))

if option.ShortName != 0 {
Expand Down
3 changes: 3 additions & 0 deletions option.go
Expand Up @@ -51,6 +51,9 @@ type Option struct {
// error.
Required bool

// If true, the option is not displayed in the help output
Hidden bool

// A name for the value of an option shown in the Help as --flag [ValueName]
ValueName string

Expand Down

0 comments on commit f59c328

Please sign in to comment.