jackwener/OpenCLI · error · ArgumentError

bilibili comments limit must be an integer between 1 and ${M

Error message

bilibili comments limit must be an integer between 1 and ${MAX_LIMIT}

What it means

ArgumentError thrown by parseLimit in clis/bilibili/comments.js when the --limit option fails validation. The value must be an integer between 1 and MAX_LIMIT (50); the default is 20 when omitted. Values like 0, negative numbers, floats, or numbers above 50 are rejected before any API call is made.

Source

Thrown at clis/bilibili/comments.js:20

 * Bilibili comments — fetches comments via the official API.
 * Top-level and pinned comments come from /x/v2/reply/main (WBI-signed); with
 * --parent, replies nested under a given comment come from /x/v2/reply/reply.
 */
import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { apiGet, resolveBvid } from './utils.js';

const MAX_LIMIT = 50;

function isAuthLikeBilibiliError(code, message) {
    return code === -101 || code === -403 || /登录|账号|权限|forbidden|permission|login/i.test(String(message ?? ''));
}

function parseLimit(value) {
    const raw = value == null ? 20 : value;
    const limit = Number(raw);
    if (!Number.isInteger(limit) || limit <= 0 || limit > MAX_LIMIT) {
        throw new ArgumentError(`bilibili comments limit must be an integer between 1 and ${MAX_LIMIT}`);
    }
    return limit;
}

function parseParent(value) {
    if (value == null) {
        return null;
    }
    const parent = Number(value);
    if (!Number.isInteger(parent) || parent <= 0) {
        throw new ArgumentError('bilibili comments parent must be a positive integer rpid');
    }
    return parent;
}

function requireOkPayload(payload, label) {
    if (!payload || typeof payload !== 'object' || Array.isArray(payload) || !Object.hasOwn(payload, 'code')) {
        throw new CommandExecutionError(`Bilibili ${label} API returned a malformed payload`);

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Set --limit to an integer between 1 and 50, or omit it to use the default of 20
  2. If more than 50 comments are needed, fetch multiple pages instead of raising the limit
  3. Sanitize the value before passing: Math.trunc(Number(value)) and clamp to [1, 50]

Example fix

// before
bilibili comments BV1WtAGzYEBm --limit 100
// after
bilibili comments BV1WtAGzYEBm --limit 50
Defensive patterns

Strategy: validation

Validate before calling

function isValidLimit(v) { const n = Number(v); return Number.isInteger(n) && n >= 1 && n <= 50; }
if (!isValidLimit(limit)) limit = 20;

Type guard

function isFiniteInt(v) { return typeof v === 'number' && Number.isInteger(v); }

Try / catch

try { const rows = await run(['bilibili','comments',bvid,'--limit',String(limit)]); } catch (e) { if (/limit must be an integer/.test(e.message)) { console.warn('invalid limit, using default 20'); } else throw e; }

Prevention

When it happens

Trigger: Calling `bilibili comments <bvid> --limit 0`, `--limit -5`, `--limit 51`, `--limit 12.5`, or passing a non-numeric string (Number() coerces it to NaN) to the limit kwarg.

Common situations: Scripts parameterizing the limit from user input or env vars with invalid values; assuming the API allows more than 50 comments per page; passing a string like '20 ' or 'all' that fails Number() coercion.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/8d29ad8f58e3b2b5. Report an issue: GitHub.