Skip to content

Commit

Permalink
ASM-11931 Hidden Params (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuvia-akeyless authored Sep 25, 2024
2 parents 955b433 + b34d2f2 commit 8171930
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func initFlagSet(typ reflect.Type, val reflect.Value, flagSet *flagSet, clr colo
if fl == nil {
continue
}
// hidden flag shouldn't appear in usage menu (--help)
if dontSetValue && fl.tag.hidden {
continue
}
flagSet.flagSlice = append(flagSet.flagSlice, fl)

// encode flag value
Expand Down
5 changes: 5 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type argT struct {
PtrStr *string `cli:"ptrStr"`

RequiredNotEnforced string `cli:"*requiredNotEnforced" ignoreRequired:"true"`
Hidden string `cli:"hidden" hidden:"true"`
}

func toStr(i interface{}) string {
Expand Down Expand Up @@ -363,6 +364,10 @@ func TestParse(t *testing.T) {
args: []string{"--required=0", "--requiredNotEnforced=hikikomori"},
want: argT{Default: 102, RequiredNotEnforced: "hikikomori"},
},
{
args: []string{"--required=0", "--hidden=hide_me"},
want: argT{Default: 102, Hidden: "hide_me"},
},
} {
if tab.args == nil {
tab.args = []string{}
Expand Down
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (cmd *Command) defaultUsageFn(ctx *Context) string {
if !isEmpty {
fmt.Fprintf(buff, "%s:\n\n%s", clr.Bold("Options"), usage(argvList, clr, style))
}
if cmd.children != nil && len(cmd.children) > 0 {
if len(cmd.children) > 0 {
if !isEmpty {
buff.WriteByte('\n')
}
Expand Down
9 changes: 9 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
tagPrompt = "prompt"
tagParser = "parser"
tagSep = "sep" // used to seperate key/value pair of map, default is `=`
tagHidden = "hidden"

dashOne = "-"
dashTwo = "--"
Expand Down Expand Up @@ -50,6 +51,7 @@ type tagProperty struct {
prompt string `prompt:"prompt string"`
sep string `sep:"string for seperate kay/value pair of map"`
parserCreator FlagParserCreator `parser:"parser for flag"`
hidden bool `hidden:"hide from help menu"`

// flag names
shortNames []string
Expand Down Expand Up @@ -138,6 +140,13 @@ func parseTag(fieldName string, structTag reflect.StructTag) (p *tagProperty, is
p.sep = sep
}

if hidden := tag.Get(tagHidden); hidden != "" {
p.hidden, err = strconv.ParseBool(hidden)
}
if err != nil {
return
}

cli = strings.TrimSpace(cli)
for {
if strings.HasPrefix(cli, "*") {
Expand Down
21 changes: 21 additions & 0 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestTag(t *testing.T) {
Required string `cli:"*r"`
RequiredNotEnforced string `cli:"*re" ignoreRequired:"true"`

Hidden string `cli:"hide-me" hidden:"true"`

Force string `cli:"!f"`
EditFile string `edit:"Filename:file"`
ShortAndLong string `cli:"x,y,z,xy,yz,xyz"`
Expand Down Expand Up @@ -57,82 +59,101 @@ func TestTag(t *testing.T) {
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--cli"})
case "Pw":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.True(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--pw"})
case "Edit":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.True(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--edit"})
case "Usage":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--usage"})
assert.Equal(t, tag.usage, "hello,usage")
case "Dft":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--dft"})
assert.Equal(t, tag.dft, "hello,dft")
case "Name":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--name"})
assert.Equal(t, tag.name, "hello-name")
case "Prompt":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--prompt"})
assert.Equal(t, tag.prompt, "hello,prompt")
case "Parser":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--parser"})
assert.Equal(t, typField.Tag.Get(tagParser), "json")
case "Sep":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.longNames, []string{"--sep"})
assert.Equal(t, tag.sep, ":")
case "Required", "RequiredNotEnforced":
assert.True(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
case "Hidden":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.True(t, tag.hidden)
case "Force":
assert.False(t, tag.isRequired)
assert.True(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
case "EditFile":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.True(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.editFile, "Filename")
case "ShortAndLong":
assert.False(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
assert.False(t, tag.isEdit)
assert.False(t, tag.hidden)
assert.Equal(t, tag.shortNames, []string{"-x", "-y", "-z"})
assert.Equal(t, tag.longNames, []string{"--xy", "--yz", "--xyz"})
case "Multi", "StillPrintsDft":
Expand Down

0 comments on commit 8171930

Please sign in to comment.