Skip to content

Commit 68c5eee

Browse files
committed
feat(shortcut): add --json flag as no-op alias for --format json
Skip registration when a custom --json flag already exists on the command (e.g. base shortcuts use --json for body input).
1 parent a92b94b commit 68c5eee

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

shortcuts/common/runner.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,9 @@ func registerShortcutFlagsWithContext(ctx context.Context, cmd *cobra.Command, f
10101010
cmd.Flags().Bool("dry-run", false, "print request without executing")
10111011
if s.HasFormat {
10121012
cmd.Flags().String("format", "json", "output format: json (default) | pretty | table | ndjson | csv")
1013+
if cmd.Flags().Lookup("json") == nil {
1014+
cmd.Flags().Bool("json", false, "shorthand for --format json")
1015+
}
10131016
}
10141017
if s.Risk == "high-risk-write" {
10151018
cmd.Flags().Bool("yes", false, "confirm high-risk operation")

shortcuts/common/runner_flag_completion_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,74 @@ func TestShortcutMount_FlagCompletionsDisabled(t *testing.T) {
9696
t.Fatal("did not expect completion func for --format when disabled")
9797
}
9898
}
99+
100+
func TestShortcutMount_JsonFlag_AcceptedWhenHasFormat(t *testing.T) {
101+
f, _, _, _ := cmdutil.TestFactory(t, nil)
102+
parent := &cobra.Command{Use: "root"}
103+
shortcut := Shortcut{
104+
Service: "test",
105+
Command: "+read",
106+
Description: "test read",
107+
HasFormat: true,
108+
Execute: func(context.Context, *RuntimeContext) error { return nil },
109+
}
110+
shortcut.Mount(parent, f)
111+
112+
cmd, _, err := parent.Find([]string{"+read"})
113+
if err != nil {
114+
t.Fatalf("Find() error = %v", err)
115+
}
116+
if flag := cmd.Flags().Lookup("json"); flag == nil {
117+
t.Fatal("expected --json flag to be registered on HasFormat shortcut")
118+
}
119+
}
120+
121+
func TestShortcutMount_JsonFlag_SkippedWhenConflict(t *testing.T) {
122+
f, _, _, _ := cmdutil.TestFactory(t, nil)
123+
parent := &cobra.Command{Use: "root"}
124+
shortcut := Shortcut{
125+
Service: "test",
126+
Command: "+update",
127+
Description: "test update",
128+
HasFormat: true,
129+
Flags: []Flag{
130+
{Name: "json", Desc: "body JSON object", Required: true},
131+
},
132+
Execute: func(context.Context, *RuntimeContext) error { return nil },
133+
}
134+
shortcut.Mount(parent, f)
135+
136+
cmd, _, err := parent.Find([]string{"+update"})
137+
if err != nil {
138+
t.Fatalf("Find() error = %v", err)
139+
}
140+
// --json flag exists (from custom Flags), but should be the string type, not bool.
141+
flag := cmd.Flags().Lookup("json")
142+
if flag == nil {
143+
t.Fatal("expected --json flag from custom Flags")
144+
}
145+
if flag.DefValue != "" {
146+
t.Errorf("expected empty default (string flag), got %q", flag.DefValue)
147+
}
148+
}
149+
150+
func TestShortcutMount_JsonFlag_AbsentWithoutHasFormat(t *testing.T) {
151+
f, _, _, _ := cmdutil.TestFactory(t, nil)
152+
parent := &cobra.Command{Use: "root"}
153+
shortcut := Shortcut{
154+
Service: "test",
155+
Command: "+write",
156+
Description: "test write",
157+
HasFormat: false,
158+
Execute: func(context.Context, *RuntimeContext) error { return nil },
159+
}
160+
shortcut.Mount(parent, f)
161+
162+
cmd, _, err := parent.Find([]string{"+write"})
163+
if err != nil {
164+
t.Fatalf("Find() error = %v", err)
165+
}
166+
if flag := cmd.Flags().Lookup("json"); flag != nil {
167+
t.Fatal("--json flag should not be registered when HasFormat is false")
168+
}
169+
}

0 commit comments

Comments
 (0)