{"record":{"id":"5bda85e4e47b5d8a","repo":"microsoft/playwright","slug":"error-too-many-arguments-expected-argnames-len","errorCode":null,"errorMessage":"error: too many arguments: expected ${argNames.length}, received ${argv.length}","messagePattern":"error: too many arguments: expected (.+?), received (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/tools/cli-daemon/command.ts","lineNumber":67,"sourceCode":"  if (schema instanceof z.ZodOptional)\n    return isVariadicArg(schema.unwrap() as zodType.ZodTypeAny);\n  if (schema instanceof z.ZodPipe)\n    return isVariadicArg(schema.in as zodType.ZodTypeAny);\n  return false;\n}\n\nexport function parseCommand(command: AnyCommandSchema, args: Record<string, string> & { _: string[] }): { toolName: string, toolParams: any } {\n  const optionsObject = { ...args } as Record<string, string>;\n  delete optionsObject['_'];\n  const optionsSchema = (command.options ?? kEmptyOptions).strict();\n  const options: Record<string, string> = zodParse(optionsSchema, optionsObject, 'option');\n\n  const argsSchema = (command.args ?? kEmptyArgs).strict();\n  const argNames = [...Object.keys(argsSchema.shape)];\n  const argv = args['_'].slice(1);\n  const variadic = argNames.length > 0 && isVariadicArg(argsSchema.shape[argNames[argNames.length - 1]]);\n  if (argv.length > argNames.length && !variadic)\n    throw new Error(`error: too many arguments: expected ${argNames.length}, received ${argv.length}`);\n  const argsObject: Record<string, string | string[] | undefined> = {};\n  argNames.forEach((name, index) => {\n    if (variadic && index === argNames.length - 1)\n      argsObject[name] = index < argv.length ? argv.slice(index) : undefined;\n    else\n      argsObject[name] = argv[index];\n  });\n  const parsedArgsObject: Record<string, string | string[]> = zodParse(argsSchema, argsObject, 'argument');\n\n  const toolName = typeof command.toolName === 'function' ? command.toolName({ ...parsedArgsObject, ...options }) : command.toolName;\n  const toolParams = command.toolParams({ ...parsedArgsObject, ...options });\n  return { toolName, toolParams };\n}\n\nfunction zodParse(schema: zodType.ZodAny, data: unknown, type: 'option' | 'argument'): any {\n  try {\n    return schema.parse(data);\n  } catch (e) {","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/tools/cli-daemon/command.ts#L49-L85","documentation":"Thrown by parseCommand when the count of positional argv tokens exceeds the number of args declared in the command schema and the last declared arg is not variadic. Each command fixes a positional arity; extras are rejected to surface user mistakes.","triggerScenarios":"Running a CLI command with more positional arguments than its schema defines, e.g. a 1-arg command invoked as `playwright-cli cmd a b c`.","commonSituations":"User assuming variadic behavior that the command does not declare; copy-paste adding stray tokens; quoting bug that splits one arg into several; deprecated alias with fewer args than the user expects.","solutions":["Check the command's definition (args schema) for its expected positional count and pass exactly that many.","If a trailing list is genuinely needed, the command must declare its last arg as variadic (z.array); file a feature request if so.","Re-run with --help (or inspect the command schema) to see the accepted positional names."],"exampleFix":"# before - command expects 1 positional\nplaywright-cli screenshot png full   # 2 positional -> throws\n\n# after\nplaywright-cli screenshot png        # 'png' binds to the single arg","handlingStrategy":"validation","validationCode":"function expectedArity(command): number {\n  const shape = command.args?.shape ?? {};\n  return Object.keys(shape).length;\n}\nfunction isVariadic(command): boolean {\n  const names = Object.keys(command.args?.shape ?? {});\n  if (!names.length) return false;\n  return isVariadicArg(command.args.shape[names[names.length - 1]]);\n}\nfunction assertArityOk(command, argv: string[]) {\n  if (argv.length > expectedArity(command) && !isVariadic(command))\n    throw new Error(`Too many positional args: expected ${expectedArity(command)}`);\n}","typeGuard":"function isTooManyArgsError(e: unknown): boolean {\n  return e instanceof Error && e.message.startsWith('error: too many arguments');\n}","tryCatchPattern":"// Validation is strongly preferred; the parser runs in a controlled CLI boundary.\n// If you must handle it:\ntry {\n  parseCommand(command, args);\n} catch (e) {\n  if (isTooManyArgsError(e)) {\n    // trim extras or surface --help\n  } else throw e;\n}","preventionTips":["Inspect the command schema's args before constructing the CLI invocation.","Quote multi-word positional values so the shell does not split them into extras.","Surface --help text on this error so users learn the accepted positional names."],"tags":["cli","args-parsing","input-validation","command-schema"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}