High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: five command groups exit 0 on an unknown subcommand, so a typo'd cache/cookies/session verb silently no-ops and reports success (PIN-117)
Luigi Agosti09ac3ad7b8ab
diff --git a/cmd/pinchtab/cmd_mcp.go b/cmd/pinchtab/cmd_mcp.goindex 3d47b581..b57503ce 100644--- a/cmd/pinchtab/cmd_mcp.go+++ b/cmd/pinchtab/cmd_mcp.go@@ -15,7 +15,9 @@ var mcpCmd = &cobra.Command{Long: "Start the Model Context Protocol stdio server and proxy browser actions to a running PinchTab instance.",// No subcommands to mistype, so the group guard cannot reach this one: an unrecognised// token here would otherwise start the server and ignore it.- Args: cobra.NoArgs,+ Args: cobra.NoArgs,+ SilenceUsage: true,+ SilenceErrors: true,Run: func(cmd *cobra.Command, args []string) {cfg := loadConfig()runMCP(cfg)diff --git a/cmd/pinchtab/unknown_subcommand.go b/cmd/pinchtab/unknown_subcommand.goindex dde6aec6..a8beff47 100644--- a/cmd/pinchtab/unknown_subcommand.go+++ b/cmd/pinchtab/unknown_subcommand.go@@ -8,27 +8,14 @@ import ("github.com/spf13/cobra")-// unknownSubcommandExitCode is the one exit code for a typo'd verb anywhere in the CLI.-// It is 1 because that is what cobra's own error path already returns for a top-level-// unknown command, and every other reported error in this CLI exits 1 as well; daemon's-// hand-rolled 2 is what changed to match, not the other way round.+// unknownSubcommandExitCode is 1 because that is what cobra's own error path already returns+// for a top-level unknown command; daemon's hand-rolled 2 is what changed to match.const unknownSubcommandExitCode = 1-// installUnknownSubcommandGuard makes an unrecognised subcommand an error on every group,-// which is what cobra does NOT do: its default argument check only rejects an unknown-// command on the ROOT, so `pinchtab cache clera` fell through to the group — printing help,-// or in the case of a runnable group such as `config` running the group's own action and-// swallowing the arguments — and exited 0. A script cannot tell that from the real verb, so-// `set -e` does not trip and the state it believed it reset was never reset.-//-// The guard is derived from the command tree rather than a list of group names, because a-// hand-written list is exactly how eleven groups came to be missing it. A group that-// declares its own Args is left alone: that declaration is how a parent whose operand is-// data rather than a verb opts out — `tab [id]` focuses a tab, `network <filter>` takes a-// pattern — and it is also the record of that decision.-//-// It must run after every command is registered, so it belongs at Execute rather than in an-// init: registration is spread across many init functions and their order is not a contract.+// installUnknownSubcommandGuard runs at Execute rather than in an init because registration+// is spread across many init functions and their order is not a contract. A group declaring+// its own Args opts out, which is how a parent whose first argument is data rather than a verb+// says so.func installUnknownSubcommandGuard(cmd *cobra.Command) {for _, sub := range cmd.Commands() {installUnknownSubcommandGuard(sub)@@ -37,6 +24,8 @@ func installUnknownSubcommandGuard(cmd *cobra.Command) {return}cmd.Args = rejectUnknownSubcommand+ cmd.SilenceUsage = true+ cmd.SilenceErrors = trueif !cmd.Runnable() {// A group with no action of its own never reaches its argument check: cobra returns// "print the help" for an unrunnable command BEFORE validating arguments, which is why@@ -50,8 +39,6 @@ func printGroupHelp(cmd *cobra.Command, _ []string) error {return cmd.Help()}-// rejectUnknownSubcommand names the valid verbs in the error itself rather than relying on-// the usage template, which this CLI shortens to a single "run --help" line.func rejectUnknownSubcommand(cmd *cobra.Command, args []string) error {if len(args) == 0 {return nildiff --git a/cmd/pinchtab/unknown_subcommand_test.go b/cmd/pinchtab/unknown_subcommand_test.goindex 45b67202..e101fd95 100644--- a/cmd/pinchtab/unknown_subcommand_test.go+++ b/cmd/pinchtab/unknown_subcommand_test.go@@ -2,6 +2,7 @@ package mainimport ("bytes"+ "fmt""io""os""strings"@@ -142,6 +143,27 @@ func TestUnknownSubcommandExitsWithOneCodeEverywhere(t *testing.T) {}}+// A refusal is two lines, the shape daemon already had: the token and the valid verbs. Cobra+// adds the parent's whole usage block and prints the error a second time unless the command+// silences both, so a refusal without those flags names the typo three times over.+func TestARefusalRendersOnceWithoutTheUsageBlock(t *testing.T) {+ for _, args := range [][]string{+ {"cache", "zzz-clera"},+ {"config", "zzz-st", "server.port", "18899"},+ {"mcp", "zzz-bogus"},+ } {+ invocation := "pinchtab " + strings.Join(args, " ")+ rendered := renderedRefusal(t, args...)++ if got := strings.Count(rendered, args[1]); got != 1 {+ t.Errorf("`%s` named the token %d times, want once:\n%s", invocation, got, rendered)+ }+ if strings.Contains(rendered, "Usage:") {+ t.Errorf("`%s` printed the usage block, so the valid verbs appear twice over:\n%s", invocation, rendered)+ }+ }+}+// The guard lives at Execute because the command tree is only complete there. A test that// installs it itself proves the walk works, not that the binary runs it.func TestExecuteInstallsTheGuard(t *testing.T) {@@ -194,15 +216,35 @@ func runRootArgs(t *testing.T, args ...string) (string, error) {t.Helper()var out bytes.Buffer+ err := runRoot(t, &out, io.Discard, args...)+ return out.String(), err+}++func runRoot(t *testing.T, out, errOut io.Writer, args ...string) error {+ t.Helper()+t.Cleanup(func() {rootCmd.SetArgs(nil)rootCmd.SetOut(nil)rootCmd.SetErr(nil)})rootCmd.SetArgs(args)- rootCmd.SetOut(&out)- rootCmd.SetErr(io.Discard)+ rootCmd.SetOut(out)+ rootCmd.SetErr(errOut)- err := rootCmd.Execute()- return out.String(), err+ return rootCmd.Execute()+}++// renderedRefusal is everything a user sees on a refusal: what cobra printed plus the line+// Execute prints itself. Asserting on the error VALUE instead is what let the refusal grow to+// eighteen lines unnoticed.+func renderedRefusal(t *testing.T, args ...string) string {+ t.Helper()++ var stream bytes.Buffer+ err := runRoot(t, &stream, &stream, args...)+ if err == nil {+ t.Fatalf("`pinchtab %s` succeeded, want a refusal", strings.Join(args, " "))+ }+ return stream.String() + fmt.Sprintln(err)}diff --git a/docs/reference/cli.md b/docs/reference/cli.mdindex ce7feb9c..0560f7fb 100644--- a/docs/reference/cli.md+++ b/docs/reference/cli.md@@ -94,6 +94,13 @@ names the valid subcommands, at the top level and inside every group. `pinchtabdoes not share an exit code with `pinchtab cache clear`, so `set -e` and `&&` chains stop ona typo instead of continuing as though the state had been reset.+```bash+$ pinchtab cache clera ; echo exit=$?+unknown command "clera" for "pinchtab cache"+Valid subcommands: clear, status+exit=1+```+Two commands take an argument rather than a subcommand and are unaffected: `pinchtab tab<id>` focuses a tab, and `pinchtab network <filter>` filters the network log. An unknownvalue there is data the server rejects, not a typo the CLI can catch.