jackwener/OpenCLI · error · ArgumentError

--limit must be an integer between 1 and 100, got ${JSON.str

Error message

--limit must be an integer between 1 and 100, got ${JSON.stringify(raw)}

What it means

parseCollectionLimit validates the --limit option for Xiaohongshu collection commands. It throws ArgumentError when the value is not a finite integer (e.g. 'abc', '3.5', '[1,2]', objects) — the message echoes the raw JSON of what was passed. Range violations (outside 1–100) get a separate message.

Source

Thrown at clis/xiaohongshu/collection-helpers.js:36

function toCleanString(value) {
    return typeof value === 'string' ? value.trim() : value == null ? '' : String(value).trim();
}

function isObject(value) {
    return value && typeof value === 'object' && !Array.isArray(value);
}

export function unwrapBrowserResult(payload) {
    if (isObject(payload) && 'session' in payload && 'data' in payload) {
        return payload.data;
    }
    return payload;
}

export function parseCollectionLimit(raw) {
    const parsed = Number(raw ?? 20);
    if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {
        throw new ArgumentError(`--limit must be an integer between 1 and 100, got ${JSON.stringify(raw)}`);
    }
    if (parsed < 1 || parsed > 100) {
        throw new ArgumentError(`--limit must be between 1 and 100, got ${parsed}`);
    }
    return parsed;
}

export function readSelfUserIdFromState(state) {
    const unwrapped = unwrapBrowserResult(state);
    const user = unwrapped?.user?.userInfo;
    const info = user?._value ?? user ?? {};
    return toCleanString(info.user_id ?? info.userId ?? info.userID ?? '');
}

export function mapCollectionNote(entry, options = {}) {
    if (!isObject(entry))
        return null;
    const noteCard = entry.note_card ?? entry.noteCard ?? entry;

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass an integer between 1 and 100, e.g. --limit 20.
  2. Coerce user input with Number() and Number.isInteger before calling the command.
  3. Trim/strip non-numeric characters from strings prior to parsing.
  4. Use the default by omitting --limit entirely (defaults to 20).
  5. Validate CLI/config values at startup and fail fast with your own message.

Example fix

// before
await cli.run(['xiaohongshu','collection','--limit', userLimit]); // userLimit = '2.5'
// after
const n = Number(userLimit);
if (!Number.isInteger(n) || n < 1 || n > 100) {
  throw new Error(`--limit must be an integer 1-100, got ${userLimit}`);
}
await cli.run(['xiaohongshu','collection','--limit', String(n)]);
Defensive patterns

Strategy: validation

Validate before calling

function isValidLimit(v) {
  const n = Number(v ?? 20);
  return Number.isInteger(n) && n >= 1 && n <= 100;
}
// call only if isValidLimit(userLimit)

Type guard

function isIntLimit(v) {
  return typeof v === 'number' && Number.isInteger(v) && v >= 1 && v <= 100;
}

Try / catch

try {
  return await collectionCommand({ limit: rawLimit });
} catch (e) {
  if (e instanceof ArgumentError && /--limit/.test(e.message)) {
    return collectionCommand({ limit: 20 }); // fall back to default
  }
  throw e;
}

Prevention

When it happens

Trigger: parseCollectionLimit(raw) receives NaN, a float, a numeric string with decimals, or a non-numeric value such that Number.isFinite(parsed) or Number.isInteger(parsed) fails; raw ?? 20 defaults undefined to 20.

Common situations: Passing --limit=abc or --limit=2.5 on the CLI; programmatically passing a string with whitespace/suffix like '20 notes'; an array/object from config files; forgetting the value ('--limit' alone) so the shell hands an odd token.

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/07c28d4f0a1100ca. Report an issue: GitHub.