Stop flag interpretation at unknown flags
This commit is contained in:
@@ -264,3 +264,31 @@ func TestParseFailuresRetainEnteredFlagSpellings(t *testing.T) {
|
||||
t.Fatalf("earlier parser failure = %#v", result.Failure)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagInterpretationStopsAtTheBackendFailurePrefix(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
args []string
|
||||
want []string
|
||||
}{
|
||||
{"long", []string{"--debug", "--unknown"}, []string{"--debug", "--unknown"}},
|
||||
{"short cluster", []string{"-dqz"}, []string{"--debug", "--quiet", "-z"}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
// The later value must not be canonicalized or retained in spellings.
|
||||
args := append(append([]string(nil), test.args...), "--message="+strings.Repeat("value", 1024), "--quiet=false")
|
||||
flags := append(commandmeta.ReportingFlags(), commandmeta.Flag{Name: "message"})
|
||||
interpreted, spellings, occurrences, failure := interpretFlagTokens(args, flags, reportingFlagOptions().FlagAliases)
|
||||
if failure != nil || !reflect.DeepEqual(interpreted, test.want) || len(spellings["message"]) != 0 {
|
||||
t.Fatalf("interpreted=%q spellings=%v failure=%v", interpreted, spellings, failure)
|
||||
}
|
||||
if len(occurrences) != len(test.want)-1 || occurrences[0].Name != "debug" {
|
||||
t.Fatalf("consumed occurrences=%#v", occurrences)
|
||||
}
|
||||
result := Parse(reportingFlagTree(), append([]string{"run"}, args...), reportingFlagOptions())
|
||||
if result.Failure == nil || !strings.Contains(result.Failure.Message, "unknown") || !reflect.DeepEqual(result.FlagOccurrences, occurrences) {
|
||||
t.Fatalf("backend result=%#v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user