summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--internal/askcli/dispatch.go19
-rw-r--r--internal/askcli/dispatch_test.go10
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