Interpret command flag aliases in the parser

This commit is contained in:
2026-10-19 19:31:23 +00:00
parent 58b71c390f
commit 75fc45f072
8 changed files with 389 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ import (
"context"
"io"
"reflect"
"strings"
"testing"
)
@@ -189,6 +190,46 @@ func TestExecuteDispatchesEnteredAliasToCanonicalRunner(t *testing.T) {
}
}
func TestExecuteAppliesCanonicalCommandFlagAliasesAfterRouting(t *testing.T) {
type input struct{ color string }
var got string
command := NewCommand(CommandSpec[input]{
Name: "inspect",
Flags: []Flag[input]{ValueFlag(FlagMeta{Name: "color", Short: "c"}, (*string)(nil),
func(value string) (string, error) { return value, nil },
func(target *input, supplied Supplied[string]) { target.color = supplied.Value },
)},
Run: func(_ Invocation, value input) Outcome {
got = value.color
return Success()
},
})
app, err := New(Identity{Name: "Example"}, command,
Alias("shortcut", "inspect"),
FlagAliasFor("inspect", "--colour", "-c"),
)
if err != nil {
t.Fatal(err)
}
outcome := app.Execute(context.Background(), []string{"shortcut", "--colour=blue"}, Streams{Out: io.Discard, Err: io.Discard})
if outcome.Kind != OutcomeSuccess || got != "blue" {
t.Fatalf("alias flag execution = (%v, color %q), want success and blue", outcome.Kind, got)
}
}
func TestExecuteAppliesGlobalHelpAliasAtApplicationRoot(t *testing.T) {
app, err := New(Identity{Name: "Example"}, FlagAlias("--hlep", "--help"))
if err != nil {
t.Fatal(err)
}
var output bytes.Buffer
outcome := app.Execute(context.Background(), []string{"--hlep"}, Streams{Out: &output, Err: io.Discard})
if outcome.Kind != OutcomeSuccess || !strings.Contains(output.String(), "Usage:") {
t.Fatalf("root help alias outcome = %v, output %q", outcome.Kind, output.String())
}
}
func TestAliasOnlyPrefixProducesDiscoveryRoute(t *testing.T) {
app, err := New(Identity{Name: "Example"},
Group("promptkit", "PromptKit", Group("list", "List", simpleRunnable("collections"))),