go-delve/delve · error

wrong number of arguments: watch [-r|-w|-rw] <expr>

Error message

wrong number of arguments: watch [-r|-w|-rw] <expr>

What it means

The 'watch' command requires exactly two space-separated tokens: a mode flag (-r, -w, or -rw) and the expression to watch. Splitting the args on the first space must yield 2 parts; if there is no space (or args are empty) the required information is missing and Delve rejects the invocation with this usage error.

Source

Thrown at pkg/terminal/command.go:2077

	}
	switch editor {
	case "code":
		return runEditor("--goto", fmt.Sprintf("%s:%d", file, lineno))
	case "zed":
		return runEditor(fmt.Sprintf("%s:%d:0", file, lineno))
	case "hx":
		return runEditor(fmt.Sprintf("%s:%d", file, lineno))
	case "vi", "vim", "nvim":
		return runEditor(fmt.Sprintf("+%d", lineno), file)
	default:
		return runEditor(fmt.Sprintf("+%d", lineno), file)
	}
}

func watchpoint(t *Term, ctx callContext, args string) error {
	v := strings.SplitN(args, " ", 2)
	if len(v) != 2 {
		return errors.New("wrong number of arguments: watch [-r|-w|-rw] <expr>")
	}
	var wtype api.WatchType
	switch v[0] {
	case "-r":
		wtype = api.WatchRead
	case "-w":
		wtype = api.WatchWrite
	case "-rw":
		wtype = api.WatchRead | api.WatchWrite
	default:
		return fmt.Errorf("wrong argument %q to watch", v[0])
	}
	bp, err := t.client.CreateWatchpoint(ctx.Scope, v[1], wtype)
	if err != nil {
		return err
	}
	fmt.Fprintf(t.stdout, "%s set at %s\n", formatBreakpointName(bp, true), t.formatBreakpointLocation(bp))
	return nil

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Include a mode flag and expression: 'watch -w <expr>' for write watchpoints, 'watch -r <expr>' for read, 'watch -rw <expr>' for both.
  2. Ensure a space separates the flag from the expression.
  3. Check 'help watch' for the exact accepted syntax.

Example fix

// before
(dlv) watch myVar
// after
(dlv) watch -w myVar
Defensive patterns

Strategy: validation

Validate before calling

func validateWatchArgs(args string) error {
    parts := strings.SplitN(args, " ", 2)
    if len(parts) != 2 || parts[1] == "" {
        return errors.New("usage: watch [-r|-w|-rw] <expr>")
    }
    switch parts[0] {
    case "-r", "-w", "-rw":
        return nil
    }
    return fmt.Errorf("invalid watch mode %q", parts[0])
}

Type guard

func isWatchMode(tok string) bool {
    return tok == "-r" || tok == "-w" || tok == "-rw"
}

Try / catch

if err := cmd.Watchpoint(term, ctx, args); err != nil {
    if strings.HasPrefix(err.Error(), "wrong number of arguments: watch") {
        return fmt.Errorf("bad watch invocation %q: %w", args, err)
    }
    return err
}

Prevention

When it happens

Trigger: Running 'watch' with no arguments, 'watch myVar' (expression but no mode flag), or any form where strings.SplitN(args, " ", 2) returns fewer than 2 elements inside watchpoint (pkg/terminal/command.go).

Common situations: Users forgetting the -r/-w flag, quoting multi-word expressions without a leading flag, or copying examples that omit the mode.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/914b5cf29650e78e. Report an issue: GitHub.