diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-27 11:22:04 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-27 11:22:04 +0200 |
| commit | 6fa6848496abd984156167a1c873fca6c724a1f7 (patch) | |
| tree | 48168ae240bb0528dd30609b83dbcf8f49410e44 /internal/askcli | |
| parent | 148c6344a011e0a707016d5825f27b4f3df4dcc4 (diff) | |
Extract parseGlobalFlags helper
Diffstat (limited to 'internal/askcli')
| -rw-r--r-- | internal/askcli/dispatch.go | 19 | ||||
| -rw-r--r-- | internal/askcli/dispatch_test.go | 10 |
2 files changed, 21 insertions, 8 deletions
diff --git a/internal/askcli/dispatch.go b/internal/askcli/dispatch.go index 5160abb..f714bc1 100644 --- a/internal/askcli/dispatch.go +++ b/internal/askcli/dispatch.go @@ -23,18 +23,21 @@ func NewDispatcher(runner Runner) *Dispatcher { return &Dispatcher{runner: runner} } -func (d *Dispatcher) Dispatch(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { - // Extract --json flag before dispatching. - var jsonOutput bool +func parseGlobalFlags(args []string) ([]string, bool) { var filtered []string - for _, a := range args { - if a == "--json" { + var jsonOutput bool + for _, arg := range args { + if arg == "--json" { jsonOutput = true - } else { - filtered = append(filtered, a) + continue } + filtered = append(filtered, arg) } - args = filtered + return filtered, jsonOutput +} + +func (d *Dispatcher) Dispatch(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + args, jsonOutput := parseGlobalFlags(args) d.jsonOutput = jsonOutput if len(args) == 0 { diff --git a/internal/askcli/dispatch_test.go b/internal/askcli/dispatch_test.go index a863481..91f4784 100644 --- a/internal/askcli/dispatch_test.go +++ b/internal/askcli/dispatch_test.go @@ -154,6 +154,16 @@ func TestDispatcher_DispatchPersistsJSONFlagOnDispatcher(t *testing.T) { } } +func TestParseGlobalFlags(t *testing.T) { + filtered, jsonOutput := parseGlobalFlags([]string{"--json", "list", "--json", "extra"}) + if !jsonOutput { + t.Fatalf("json flag not detected") + } + if got := strings.Join(filtered, " "); got != "list extra" { + t.Fatalf("filtered args = %q, want \"list extra\"", got) + } +} + func TestDispatcher_AllSubcommandsReachExecutor(t *testing.T) { dir := t.TempDir() oldRoot := taskAliasCacheRoot |
