Retain entered flag spellings in parser failures

This commit is contained in:
2026-10-20 00:29:49 +00:00
parent eb92f7be7f
commit 520d7e125b
5 changed files with 108 additions and 8 deletions

View File

@@ -11,6 +11,46 @@ import (
"testing"
)
func TestParserFailuresRetainAliasUsageAndFlagSpellings(t *testing.T) {
built := 0
command := NewCommand(CommandSpec[struct{}]{
Name: "inspect",
NewInput: func() struct{} { built++; return struct{}{} },
Flags: []Flag[struct{}]{ValueFlag(FlagMeta{Name: "format"}, (*string)(nil),
func(value string) (string, error) { return value, nil },
func(*struct{}, Supplied[string]) {})},
Run: func(Invocation, struct{}) Outcome { t.Fatal("invalid input executed"); return Success() },
})
app, err := New(Identity{Name: "Demo"}, command, Alias("i", "inspect"),
FlagAliasFor("inspect", "--fmt", "--format"),
FlagAliasFor("inspect", "-F", "--format"), FlagAlias("--hlep", "--help"))
if err != nil {
t.Fatal(err)
}
for _, test := range []struct {
args []string
spelling string
}{
{[]string{"i", "--fmt"}, ": --fmt"},
{[]string{"i", "-F"}, ": -F"},
{[]string{"i", "--hlep=bad", "--help=true"}, "for --hlep"},
{[]string{"i", "-h=bad"}, "for -h"},
} {
t.Run(strings.Join(test.args, " "), func(t *testing.T) {
var diagnostics bytes.Buffer
outcome := app.Execute(context.Background(), test.args, Streams{Out: io.Discard, Err: &diagnostics})
if outcome.Kind != OutcomeUsage || built != 0 {
t.Fatalf("outcome = %#v, inputs built = %d", outcome, built)
}
for _, fragment := range []string{test.spelling, "Usage: Demo i", "Alias for: Demo inspect"} {
if !strings.Contains(diagnostics.String(), fragment) {
t.Errorf("diagnostics %q omit %q", diagnostics.String(), fragment)
}
}
})
}
}
func TestCommandContextSurvivesIncompleteAndInvalidOperandCounts(t *testing.T) {
for _, spelling := range []string{"run", "r"} {
for _, test := range []struct {