grafana/k6 · error

accepts %d arg(s), received %d: %s

Error message

accepts %d arg(s), received %d: %s

What it means

Cobra positional-argument validator built by exactArgsWithMsg(n, msg). It rejects a command invocation when the number of positional (non-flag) arguments differs from n; currently n=1 for `k6 run`, `k6 cloud run`, `k6 cloud upload`, and the `k6 deps` subcommand, and the trailing msg explains what the single argument should be ('-' for stdin or a script path).

Source

Thrown at internal/cmd/common.go:74

	if err != nil {
		panic(err)
	}
	return types.NullDuration{Duration: types.Duration(v), Valid: flags.Changed(key)}
}

func getNullString(flags *pflag.FlagSet, key string) null.String {
	v, err := flags.GetString(key)
	if err != nil {
		panic(err)
	}
	return null.NewString(v, flags.Changed(key))
}

//nolint:unparam // this is only currently used with 1 as its first argument
func exactArgsWithMsg(n int, msg string) cobra.PositionalArgs {
	return func(_ *cobra.Command, args []string) error {
		if len(args) != n {
			return fmt.Errorf("accepts %d arg(s), received %d: %s", n, len(args), msg)
		}
		return nil
	}
}

func printToStdout(gs *state.GlobalState, s string) {
	if _, err := fmt.Fprint(gs.Stdout, s); err != nil {
		gs.Logger.Errorf("could not print '%s' to stdout: %s", s, err.Error())
	}
}

func getExampleText(gs *state.GlobalState, tpl string) string {
	var exampleText bytes.Buffer
	exampleTemplate := template.Must(template.New("").Parse(tpl))

	if err := exampleTemplate.Execute(&exampleText, gs.BinaryName); err != nil {
		gs.Logger.WithError(err).Error("Error during help example generation")
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Pass exactly one script path (or '-' for stdin): `k6 run script.js`
  2. Quote paths containing spaces: `k6 run "my script.js"`
  3. Check the custom msg in the error output — it states the accepted argument for that specific command
  4. If reading from stdin in a pipeline, use `cat script.js | k6 run -`

Example fix

# before
k6 run my script.js
# error: accepts 1 arg(s), received 2: arg should either be "-", if reading script from stdin, or a path to a script file

# after
k6 run "my script.js"
Defensive patterns

Strategy: validation

Validate before calling

# Validate argv before invoking k6: exactly one non-flag positional for run/cloud/cloud upload/deps
args=()
for a in "$@"; do case "$a" in -*) ;; *) args+=("$a");; esac; done
[ "${#args[@]}" -eq 1 ] || { echo "usage: $0 <script.js|->"; exit 2; }

Prevention

When it happens

Trigger: Passing zero or multiple positional args to one of the exactArgsWithMsg(1) commands: `k6 run` (no script), `k6 run a.js b.js`, `k6 cloud upload extra file.js`, `k6 deps one two`. Also triggered by scripts that accidentally forward extra argv entries to k6.

Common situations: Quoting mistakes that split one path into several args (`k6 run my script.js`); CI pipelines interpolating an empty or multi-file variable into the command line; wrappers that append flags after `--` incorrectly.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/bdb320c515c2466d. Report an issue: GitHub.