jackwener/OpenCLI · error · ArgumentError

--limit must be an integer between ${RECOMMEND_MIN_LIMIT} an

Error message

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

What it means

This ArgumentError is thrown by parseRecommendLimit when the --limit value is provided but is not a finite integer. It is the type-check branch (before the range check) and includes the JSON-stringified raw input to make the offending value obvious.

Source

Thrown at clis/toutiao/utils.js:29

const RECOMMEND_MAX_LIMIT = 50;

export function parseArticlesPage(raw, fallback = 1) {
    if (raw === undefined || raw === null || raw === '') return fallback;
    const parsed = Number(raw);
    if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {
        throw new ArgumentError(`--page must be an integer between ${ARTICLES_MIN_PAGE} and ${ARTICLES_MAX_PAGE}, got ${JSON.stringify(raw)}`);
    }
    if (parsed < ARTICLES_MIN_PAGE || parsed > ARTICLES_MAX_PAGE) {
        throw new ArgumentError(`--page must be between ${ARTICLES_MIN_PAGE} and ${ARTICLES_MAX_PAGE}, got ${parsed}`);
    }
    return parsed;
}

export function parseRecommendLimit(raw, fallback = 20) {
    if (raw === undefined || raw === null || raw === '') return fallback;
    const parsed = Number(raw);
    if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {
        throw new ArgumentError(`--limit must be an integer between ${RECOMMEND_MIN_LIMIT} and ${RECOMMEND_MAX_LIMIT}, got ${JSON.stringify(raw)}`);
    }
    if (parsed < RECOMMEND_MIN_LIMIT || parsed > RECOMMEND_MAX_LIMIT) {
        throw new ArgumentError(`--limit must be between ${RECOMMEND_MIN_LIMIT} and ${RECOMMEND_MAX_LIMIT}, got ${parsed}`);
    }
    return parsed;
}

export function parseRecommendCategory(raw, fallback = '__all__') {
    if (raw === undefined || raw === null || raw === '') return fallback;
    const value = String(raw).trim();
    if (!RECOMMEND_CATEGORIES.includes(value)) {
        throw new ArgumentError(`--category must be one of ${RECOMMEND_CATEGORIES.join(', ')}, got ${JSON.stringify(raw)}`);
    }
    return value;
}

export function parseHotLimit(raw, fallback = 30) {
    if (raw === undefined || raw === null || raw === '') return fallback;

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a plain integer within [RECOMMEND_MIN_LIMIT, RECOMMEND_MAX_LIMIT], e.g. `--limit 30`.
  2. Strip formatting characters (commas, spaces) from the value.
  3. In scripts, validate with Number.isInteger(Number(x)) before calling.
  4. Omit --limit to use the default of 20.

Example fix

// before
toutiao recommend --limit 1,000
// after
toutiao recommend --limit 30
Defensive patterns

Strategy: validation

Validate before calling

function validateLimit(raw) {
  if (raw === undefined || raw === null || raw === '') return 20; // default
  const n = Number(String(raw).replace(/[,\s]/g, ''));
  if (!Number.isInteger(n)) throw new TypeError(`--limit must be an integer, got ${JSON.stringify(raw)}`);
  return n;
}

Type guard

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

Try / catch

try {
  await recommend({ limit: rawLimit });
} catch (e) {
  if (e instanceof ArgumentError && e.message.startsWith('--limit must be an integer')) {
    console.error(`Invalid --limit value; pass a plain integer like --limit 20`);
    process.exitCode = 2;
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Running the recommend command with `--limit twenty`, `--limit 12.5`, `--limit 1,000`, or programmatically passing a non-integer to parseRecommendLimit. Empty/undefined/null values do NOT trigger it (fallback 20 applies).

Common situations: Entering formatted numbers with thousands separators ('1,000'); decimal limits ('20.5'); shell variables containing garbage; confusing --limit with a page number.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


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