{"record":{"id":"bdb320c515c2466d","repo":"grafana/k6","slug":"accepts-d-arg-s-received-d-s","errorCode":null,"errorMessage":"accepts %d arg(s), received %d: %s","messagePattern":"accepts (.+?) arg\\(s\\), received (.+?): (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/common.go","lineNumber":74,"sourceCode":"\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn types.NullDuration{Duration: types.Duration(v), Valid: flags.Changed(key)}\n}\n\nfunc getNullString(flags *pflag.FlagSet, key string) null.String {\n\tv, err := flags.GetString(key)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn null.NewString(v, flags.Changed(key))\n}\n\n//nolint:unparam // this is only currently used with 1 as its first argument\nfunc exactArgsWithMsg(n int, msg string) cobra.PositionalArgs {\n\treturn func(_ *cobra.Command, args []string) error {\n\t\tif len(args) != n {\n\t\t\treturn fmt.Errorf(\"accepts %d arg(s), received %d: %s\", n, len(args), msg)\n\t\t}\n\t\treturn nil\n\t}\n}\n\nfunc printToStdout(gs *state.GlobalState, s string) {\n\tif _, err := fmt.Fprint(gs.Stdout, s); err != nil {\n\t\tgs.Logger.Errorf(\"could not print '%s' to stdout: %s\", s, err.Error())\n\t}\n}\n\nfunc getExampleText(gs *state.GlobalState, tpl string) string {\n\tvar exampleText bytes.Buffer\n\texampleTemplate := template.Must(template.New(\"\").Parse(tpl))\n\n\tif err := exampleTemplate.Execute(&exampleText, gs.BinaryName); err != nil {\n\t\tgs.Logger.WithError(err).Error(\"Error during help example generation\")\n\t}","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/common.go#L56-L92","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass exactly one script path (or '-' for stdin): `k6 run script.js`","Quote paths containing spaces: `k6 run \"my script.js\"`","Check the custom msg in the error output — it states the accepted argument for that specific command","If reading from stdin in a pipeline, use `cat script.js | k6 run -`"],"exampleFix":"# before\nk6 run my script.js\n# error: accepts 1 arg(s), received 2: arg should either be \"-\", if reading script from stdin, or a path to a script file\n\n# after\nk6 run \"my script.js\"","handlingStrategy":"validation","validationCode":"# Validate argv before invoking k6: exactly one non-flag positional for run/cloud/cloud upload/deps\nargs=()\nfor a in \"$@\"; do case \"$a\" in -*) ;; *) args+=(\"$a\");; esac; done\n[ \"${#args[@]}\" -eq 1 ] || { echo \"usage: $0 <script.js|->\"; exit 2; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always quote script paths: k6 run \"my script.js\"","In CI, print the fully expanded k6 command line before executing it","Use '-' explicitly when piping scripts via stdin","Remember these commands accept exactly one positional argument, not a glob"],"tags":["cli","arguments","validation","cobra"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}