Skip to content

Commit f86c8f9

Browse files
committed
fix(cli): use '--' separator to prevent snip from consuming proxied command flags
ParseFlags now stops consuming snip flags when it encounters '--'. Everything after the separator is passed verbatim to the underlying command, so flags like --help or --version in the original invocation reach the proxied tool instead of being interpreted by snip. The hook script is updated to rewrite commands as 'snip -- <cmd>' instead of 'snip <cmd>', ensuring the separator is always injected automatically. Existing users must re-run 'snip init' to update the installed hook script.
1 parent 1c39207 commit f86c8f9

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

internal/cli/flags.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ type Flags struct {
1212
}
1313

1414
// ParseFlags extracts global flags from args and returns remaining args.
15+
// A "--" separator stops flag parsing: everything after it is passed
16+
// verbatim to the underlying command, preventing snip from consuming
17+
// flags like --help or --version that belong to the proxied tool.
1518
func ParseFlags(args []string) (Flags, []string) {
1619
var flags Flags
1720
var remaining []string
1821

19-
for _, arg := range args {
22+
for i, arg := range args {
23+
if arg == "--" {
24+
// Everything after "--" belongs to the underlying command.
25+
remaining = append(remaining, args[i+1:]...)
26+
break
27+
}
2028
switch {
2129
case arg == "-vv":
2230
flags.Verbose = 2

internal/cli/flags_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,37 @@ func TestParseFlags(t *testing.T) {
5353
wantFlags: Flags{Verbose: 1, UltraCompact: true},
5454
wantArgs: []string{"git", "status"},
5555
},
56+
// "--" separator: everything after it is passed verbatim to the command.
57+
{
58+
name: "double dash passes remaining verbatim",
59+
args: []string{"--", "opencode", "--help"},
60+
wantFlags: Flags{},
61+
wantArgs: []string{"opencode", "--help"},
62+
},
63+
{
64+
name: "snip flags before double dash, command flags after",
65+
args: []string{"-v", "--", "go", "test", "--version"},
66+
wantFlags: Flags{Verbose: 1},
67+
wantArgs: []string{"go", "test", "--version"},
68+
},
69+
{
70+
name: "double dash alone produces empty remaining",
71+
args: []string{"--"},
72+
wantFlags: Flags{},
73+
wantArgs: nil,
74+
},
75+
{
76+
name: "double dash before --help prevents snip help",
77+
args: []string{"--", "--help"},
78+
wantFlags: Flags{},
79+
wantArgs: []string{"--help"},
80+
},
81+
{
82+
name: "double dash before -v prevents snip verbose",
83+
args: []string{"--", "-v", "git", "log"},
84+
wantFlags: Flags{},
85+
wantArgs: []string{"-v", "git", "log"},
86+
},
5687
}
5788

5889
for _, tt := range tests {

internal/initcmd/init.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ BASE=$(echo "$BARE_CMD" | awk '{print $1}')
4949
REWRITE=""
5050
case "$BASE" in
5151
git|go|cargo|npm|npx|yarn|pnpm|docker|kubectl|make|pip|pytest|jest|tsc|eslint|rustc)
52-
# Rewrite: prefix with snip
53-
REWRITE=$(echo "$CMD" | sed "s|$BARE_CMD|snip $BARE_CMD|")
52+
# Rewrite: prefix with "snip --" so flags like --help or --version in the
53+
# original command are passed verbatim to the underlying tool, not parsed
54+
# by snip itself.
55+
REWRITE=$(echo "$CMD" | sed "s|$BARE_CMD|snip -- $BARE_CMD|")
5456
;;
5557
esac
5658

internal/initcmd/init_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ func TestHookScriptMultilineCommand(t *testing.T) {
243243
updated, _ := hookOut["updatedInput"].(map[string]any)
244244
rewritten, _ := updated["command"].(string)
245245

246-
if !strings.HasPrefix(rewritten, "snip git add ") {
247-
t.Errorf("expected rewritten command to start with 'snip git add', got: %s", rewritten)
246+
if !strings.HasPrefix(rewritten, "snip -- git add ") {
247+
t.Errorf("expected rewritten command to start with 'snip -- git add', got: %s", rewritten)
248248
}
249249
}
250250

0 commit comments

Comments
 (0)