From 63df81cc3fb814df9f0bde42f7e82f2bf817b56c Mon Sep 17 00:00:00 2001 From: pyq-lsa <26353308+pyq-lsa@users.noreply.github.com> Date: Sat, 11 Jun 2022 12:52:05 -0700 Subject: [PATCH 1/2] helpful error message when flag is missing values --- kong_test.go | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ mapper.go | 8 ++--- 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/kong_test.go b/kong_test.go index 9848356..e8bebc7 100644 --- a/kong_test.go +++ b/kong_test.go @@ -1561,3 +1561,103 @@ func TestHelpShouldStillWork(t *testing.T) { // program; errors will not propagate in the real world). require.Error(t, err) } + +func TestSliceDecoderHelpfulErrorMsg(t *testing.T) { + tests := []struct { + name string + cli interface{} + args []string + err string + }{ + { + "DefaultRune", + &struct { + Stuff []string + }{}, + []string{"--stuff"}, + `--stuff: missing argument: expecting ",..."`, + }, + { + "SpecifiedRune", + &struct { + Stuff []string `sep:","` + }{}, + []string{"--stuff"}, + `--stuff: missing argument: expecting ",..."`, + }, + { + "SpaceRune", + &struct { + Stuff []string `sep:" "` + }{}, + []string{"--stuff"}, + `--stuff: missing argument: expecting " ..."`, + }, + { + "OtherRune", + &struct { + Stuff []string `sep:"_"` + }{}, + []string{"--stuff"}, + `--stuff: missing argument: expecting "_..."`, + }, + } + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + p := mustNew(t, test.cli) + _, err := p.Parse(test.args) + require.EqualError(t, err, test.err) + }) + } +} + +func TestMapDecoderHelpfulErrorMsg(t *testing.T) { + tests := []struct { + name string + cli interface{} + args []string + expected string + }{ + { + "DefaultRune", + &struct { + Stuff map[string]int + }{}, + []string{"--stuff"}, + `--stuff: missing argument: expecting "=;..."`, + }, + { + "SpecifiedRune", + &struct { + Stuff map[string]int `mapsep:";"` + }{}, + []string{"--stuff"}, + `--stuff: missing argument: expecting "=;..."`, + }, + { + "SpaceRune", + &struct { + Stuff map[string]int `mapsep:" "` + }{}, + []string{"--stuff"}, + `--stuff: missing argument: expecting "= ..."`, + }, + { + "OtherRune", + &struct { + Stuff map[string]int `mapsep:","` + }{}, + []string{"--stuff"}, + `--stuff: missing argument: expecting "=,..."`, + }, + } + for _, test := range tests { + test := test + t.Run(test.name, func(t *testing.T) { + p := mustNew(t, test.cli) + _, err := p.Parse(test.args) + require.EqualError(t, err, test.expected) + }) + } +} diff --git a/mapper.go b/mapper.go index 796a6cc..5a476c7 100644 --- a/mapper.go +++ b/mapper.go @@ -439,17 +439,17 @@ func mapDecoder(r *Registry) MapperFunc { target.Set(reflect.MakeMap(target.Type())) } el := target.Type() - sep := ctx.Value.Tag.MapSep + mapsep := ctx.Value.Tag.MapSep var childScanner *Scanner if ctx.Value.Flag != nil { t := ctx.Scan.Pop() // If decoding a flag, we need an argument. if t.IsEOL() { - return errors.New("unexpected EOL") + return fmt.Errorf("missing argument: expecting \"=%c...\"", mapsep) } switch v := t.Value.(type) { case string: - childScanner = ScanAsType(t.Type, SplitEscaped(v, sep)...) + childScanner = ScanAsType(t.Type, SplitEscaped(v, mapsep)...) case []map[string]interface{}: for _, m := range v { @@ -520,7 +520,7 @@ func sliceDecoder(r *Registry) MapperFunc { t := ctx.Scan.Pop() // If decoding a flag, we need an argument. if t.IsEOL() { - return errors.New("unexpected EOL") + return fmt.Errorf("missing argument: expecting \"%c...\"", sep) } switch v := t.Value.(type) { case string: From 39be08f26bb0ec8c18f3c7806cf94d2592892208 Mon Sep 17 00:00:00 2001 From: pyq-lsa <26353308+pyq-lsa@users.noreply.github.com> Date: Sat, 11 Jun 2022 14:08:49 -0700 Subject: [PATCH 2/2] argument -> value --- kong_test.go | 16 ++++++++-------- mapper.go | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/kong_test.go b/kong_test.go index e8bebc7..7549906 100644 --- a/kong_test.go +++ b/kong_test.go @@ -1575,7 +1575,7 @@ func TestSliceDecoderHelpfulErrorMsg(t *testing.T) { Stuff []string }{}, []string{"--stuff"}, - `--stuff: missing argument: expecting ",..."`, + `--stuff: missing value, expecting ",..."`, }, { "SpecifiedRune", @@ -1583,7 +1583,7 @@ func TestSliceDecoderHelpfulErrorMsg(t *testing.T) { Stuff []string `sep:","` }{}, []string{"--stuff"}, - `--stuff: missing argument: expecting ",..."`, + `--stuff: missing value, expecting ",..."`, }, { "SpaceRune", @@ -1591,7 +1591,7 @@ func TestSliceDecoderHelpfulErrorMsg(t *testing.T) { Stuff []string `sep:" "` }{}, []string{"--stuff"}, - `--stuff: missing argument: expecting " ..."`, + `--stuff: missing value, expecting " ..."`, }, { "OtherRune", @@ -1599,7 +1599,7 @@ func TestSliceDecoderHelpfulErrorMsg(t *testing.T) { Stuff []string `sep:"_"` }{}, []string{"--stuff"}, - `--stuff: missing argument: expecting "_..."`, + `--stuff: missing value, expecting "_..."`, }, } for _, test := range tests { @@ -1625,7 +1625,7 @@ func TestMapDecoderHelpfulErrorMsg(t *testing.T) { Stuff map[string]int }{}, []string{"--stuff"}, - `--stuff: missing argument: expecting "=;..."`, + `--stuff: missing value, expecting "=;..."`, }, { "SpecifiedRune", @@ -1633,7 +1633,7 @@ func TestMapDecoderHelpfulErrorMsg(t *testing.T) { Stuff map[string]int `mapsep:";"` }{}, []string{"--stuff"}, - `--stuff: missing argument: expecting "=;..."`, + `--stuff: missing value, expecting "=;..."`, }, { "SpaceRune", @@ -1641,7 +1641,7 @@ func TestMapDecoderHelpfulErrorMsg(t *testing.T) { Stuff map[string]int `mapsep:" "` }{}, []string{"--stuff"}, - `--stuff: missing argument: expecting "= ..."`, + `--stuff: missing value, expecting "= ..."`, }, { "OtherRune", @@ -1649,7 +1649,7 @@ func TestMapDecoderHelpfulErrorMsg(t *testing.T) { Stuff map[string]int `mapsep:","` }{}, []string{"--stuff"}, - `--stuff: missing argument: expecting "=,..."`, + `--stuff: missing value, expecting "=,..."`, }, } for _, test := range tests { diff --git a/mapper.go b/mapper.go index 5a476c7..231ce09 100644 --- a/mapper.go +++ b/mapper.go @@ -443,9 +443,9 @@ func mapDecoder(r *Registry) MapperFunc { var childScanner *Scanner if ctx.Value.Flag != nil { t := ctx.Scan.Pop() - // If decoding a flag, we need an argument. + // If decoding a flag, we need an value. if t.IsEOL() { - return fmt.Errorf("missing argument: expecting \"=%c...\"", mapsep) + return fmt.Errorf("missing value, expecting \"=%c...\"", mapsep) } switch v := t.Value.(type) { case string: @@ -518,9 +518,9 @@ func sliceDecoder(r *Registry) MapperFunc { var childScanner *Scanner if ctx.Value.Flag != nil { t := ctx.Scan.Pop() - // If decoding a flag, we need an argument. + // If decoding a flag, we need an value. if t.IsEOL() { - return fmt.Errorf("missing argument: expecting \"%c...\"", sep) + return fmt.Errorf("missing value, expecting \"%c...\"", sep) } switch v := t.Value.(type) { case string: