Skip to content

Commit

Permalink
bug[ASM-10942]- Required: add option to not enforce required fields (#4)
Browse files Browse the repository at this point in the history
* bug[ASM-10942]- Pointers: add option to not enforce required fields

* bug[ASM-10942]- Pointers: add test

* bug[ASM-10942]- Required not enforced: if tag is not properly written return err
  • Loading branch information
dan-akeyless authored Sep 24, 2024
1 parent 6791f96 commit 955b433
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func parseArgsToFlagSet(args []string, flagSet *flagSet, clr color.Color) {

buff := bytes.NewBufferString("")
for _, fl := range flagSet.flagSlice {
if !fl.isAssigned && fl.tag.isRequired {
if !fl.isAssigned && fl.tag.isRequired && !fl.tag.ignoreRequired {
if buff.Len() > 0 {
buff.WriteByte('\n')
}
Expand Down
18 changes: 12 additions & 6 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ type argT struct {
Float32 float32 `cli:"f32" usage:"type float32"`
Float64 float64 `cli:"f64" usage:"type float64"`

PtrBool *bool `cli:"ptrBool"`
PtrStr *string `cli:"ptrStr"`
PtrBool *bool `cli:"ptrBool"`
PtrStr *string `cli:"ptrStr"`

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

func toStr(i interface{}) string {
Expand Down Expand Up @@ -334,11 +336,11 @@ func TestParse(t *testing.T) {
// Case: bool pointer
{
args: []string{"--required=0", "--ptrBool=true"},
want: argT{Default: 102, PtrBool: func(b bool)*bool{return &b}(true)},
want: argT{Default: 102, PtrBool: func(b bool) *bool { return &b }(true)},
},
{
args: []string{"--required=0", "--ptrBool=false"},
want: argT{Default: 102, PtrBool: func(b bool)*bool{return &b}(false)},
want: argT{Default: 102, PtrBool: func(b bool) *bool { return &b }(false)},
},
{
args: []string{"--required=0"},
Expand All @@ -347,16 +349,20 @@ func TestParse(t *testing.T) {
// Case: str pointer
{
args: []string{"--required=0", "--ptrStr=true"},
want: argT{Default: 102, PtrStr: func(b string)*string{return &b}("true")},
want: argT{Default: 102, PtrStr: func(b string) *string { return &b }("true")},
},
{
args: []string{"--required=0", "--ptrStr=false"},
want: argT{Default: 102, PtrStr: func(b string)*string{return &b}("false")},
want: argT{Default: 102, PtrStr: func(b string) *string { return &b }("false")},
},
{
args: []string{"--required=0"},
want: argT{Default: 102, PtrStr: nil},
},
{
args: []string{"--required=0", "--requiredNotEnforced=hikikomori"},
want: argT{Default: 102, RequiredNotEnforced: "hikikomori"},
},
} {
if tab.args == nil {
tab.args = []string{}
Expand Down
47 changes: 31 additions & 16 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ const (
tagPw = "pw" // password
tagEdit = "edit"

tagUsage = "usage"
tagDefaut = "dft"
tagDefautInject = "ignoreDft"
tagName = "name"
tagPrompt = "prompt"
tagParser = "parser"
tagSep = "sep" // used to seperate key/value pair of map, default is `=`
tagUsage = "usage"
tagDefaut = "dft"
tagIgnoreDefault = "ignoreDft"
tagIgnoreRequired = "ignoreRequired"
tagName = "name"
tagPrompt = "prompt"
tagParser = "parser"
tagSep = "sep" // used to seperate key/value pair of map, default is `=`

dashOne = "-"
dashTwo = "--"
Expand All @@ -41,13 +42,14 @@ type tagProperty struct {
isEdit bool `edit:"xxx"`
editFile string `edit:"FILE:xxx"`

usage string `usage:"usage string"`
dft string `dft:"default value or expression"`
ignoreDft bool `ignoreDft:"true/false, by default true"`
name string `name:"tag reference name"`
prompt string `prompt:"prompt string"`
sep string `sep:"string for seperate kay/value pair of map"`
parserCreator FlagParserCreator `parser:"parser for flag"`
usage string `usage:"usage string"`
dft string `dft:"default value or expression"`
ignoreDft bool `ignoreDft:"true/false, by default false"`
ignoreRequired bool `ignoreRequired:"true/false, by default false"`
name string `name:"tag reference name"`
prompt string `prompt:"prompt string"`
sep string `sep:"string for seperate kay/value pair of map"`
parserCreator FlagParserCreator `parser:"parser for flag"`

// flag names
shortNames []string
Expand Down Expand Up @@ -101,8 +103,21 @@ func parseTag(fieldName string, structTag reflect.StructTag) (p *tagProperty, is
p.dft = tag.Get(tagDefaut)

// `ignoreDft` TAG
toIgnore, _ := strconv.ParseBool(tag.Get(tagDefautInject))
p.ignoreDft = toIgnore
if tag := tag.Get(tagIgnoreDefault); tag != "" {
p.ignoreDft, err = strconv.ParseBool(tag)

}
if err != nil {
return
}

// `ignoreRequired` TAG
if tag := tag.Get(tagIgnoreRequired); tag != "" {
p.ignoreRequired, err = strconv.ParseBool(tag)
}
if err != nil {
return
}

// `name` TAG
p.name = tag.Get(tagName)
Expand Down
6 changes: 4 additions & 2 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func TestTag(t *testing.T) {
dft:"dft-value"`
StillPrintsDft string `cli:"stillDft" usage:"multi usage" dft:"dft-value" ignoreDft:"false"`

Required string `cli:"*r"`
Required string `cli:"*r"`
RequiredNotEnforced string `cli:"*re" ignoreRequired:"true"`

Force string `cli:"!f"`
EditFile string `edit:"Filename:file"`
ShortAndLong string `cli:"x,y,z,xy,yz,xyz"`
Expand Down Expand Up @@ -110,7 +112,7 @@ func TestTag(t *testing.T) {
assert.False(t, tag.isEdit)
assert.Equal(t, tag.longNames, []string{"--sep"})
assert.Equal(t, tag.sep, ":")
case "Required":
case "Required", "RequiredNotEnforced":
assert.True(t, tag.isRequired)
assert.False(t, tag.isForce)
assert.False(t, tag.isPassword)
Expand Down

0 comments on commit 955b433

Please sign in to comment.