microsoft/playwright · error · Error
error: too many arguments: expected ${argNames.length}, rece
Error message
error: too many arguments: expected ${argNames.length}, received ${argv.length} What it means
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.
Source
Thrown at packages/playwright-core/src/tools/cli-daemon/command.ts:67
if (schema instanceof z.ZodOptional)
return isVariadicArg(schema.unwrap() as zodType.ZodTypeAny);
if (schema instanceof z.ZodPipe)
return isVariadicArg(schema.in as zodType.ZodTypeAny);
return false;
}
export function parseCommand(command: AnyCommandSchema, args: Record<string, string> & { _: string[] }): { toolName: string, toolParams: any } {
const optionsObject = { ...args } as Record<string, string>;
delete optionsObject['_'];
const optionsSchema = (command.options ?? kEmptyOptions).strict();
const options: Record<string, string> = zodParse(optionsSchema, optionsObject, 'option');
const argsSchema = (command.args ?? kEmptyArgs).strict();
const argNames = [...Object.keys(argsSchema.shape)];
const argv = args['_'].slice(1);
const variadic = argNames.length > 0 && isVariadicArg(argsSchema.shape[argNames[argNames.length - 1]]);
if (argv.length > argNames.length && !variadic)
throw new Error(`error: too many arguments: expected ${argNames.length}, received ${argv.length}`);
const argsObject: Record<string, string | string[] | undefined> = {};
argNames.forEach((name, index) => {
if (variadic && index === argNames.length - 1)
argsObject[name] = index < argv.length ? argv.slice(index) : undefined;
else
argsObject[name] = argv[index];
});
const parsedArgsObject: Record<string, string | string[]> = zodParse(argsSchema, argsObject, 'argument');
const toolName = typeof command.toolName === 'function' ? command.toolName({ ...parsedArgsObject, ...options }) : command.toolName;
const toolParams = command.toolParams({ ...parsedArgsObject, ...options });
return { toolName, toolParams };
}
function zodParse(schema: zodType.ZodAny, data: unknown, type: 'option' | 'argument'): any {
try {
return schema.parse(data);
} catch (e) {View on GitHub (pinned to c8fc3bf8d3)
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.
Example fix
# before - command expects 1 positional playwright-cli screenshot png full # 2 positional -> throws # after playwright-cli screenshot png # 'png' binds to the single arg
Defensive patterns
Strategy: validation
Validate before calling
function expectedArity(command): number {
const shape = command.args?.shape ?? {};
return Object.keys(shape).length;
}
function isVariadic(command): boolean {
const names = Object.keys(command.args?.shape ?? {});
if (!names.length) return false;
return isVariadicArg(command.args.shape[names[names.length - 1]]);
}
function assertArityOk(command, argv: string[]) {
if (argv.length > expectedArity(command) && !isVariadic(command))
throw new Error(`Too many positional args: expected ${expectedArity(command)}`);
} Type guard
function isTooManyArgsError(e: unknown): boolean {
return e instanceof Error && e.message.startsWith('error: too many arguments');
} Try / catch
// Validation is strongly preferred; the parser runs in a controlled CLI boundary.
// If you must handle it:
try {
parseCommand(command, args);
} catch (e) {
if (isTooManyArgsError(e)) {
// trim extras or surface --help
} else throw e;
} Prevention
- 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.
When it happens
Trigger: 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`.
Common situations: 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.
Related errors
- boolean option '--${key}' should not be passed with '=value'
- --data must be in "mime/type=value" format, got: ${entry}
- Command is required
- Invalid viewport size format: use "width,height", for exampl
- Invalid geolocation format, should be "lat,long". For exampl
AI-assisted analysis of microsoft/playwright@c8fc3bf8d3 (2026-08-12).
Data as JSON: /api/errors/5bda85e4e47b5d8a.
Report an issue: GitHub.