jackwener/OpenCLI · error · ArgumentError
--page must be an integer between ${ARTICLES_MIN_PAGE} and $
Error message
--page must be an integer between ${ARTICLES_MIN_PAGE} and ${ARTICLES_MAX_PAGE}, got ${JSON.stringify(raw)} What it means
This ArgumentError is thrown by parseArticlesPage when the --page value is provided but is not a finite integer (e.g. 'abc', '1.5', 'NaN'). The error distinguishes type failures (this check) from range failures (the next check), and includes the raw JSON-stringified input for diagnosis.
Source
Thrown at clis/toutiao/utils.js:17
/**
* Shared helpers for the toutiao adapter.
*/
import { ArgumentError, CommandExecutionError } from '@jackwener/opencli/errors';
const ARTICLES_MIN_PAGE = 1;
const ARTICLES_MAX_PAGE = 4;
const HOT_MIN_LIMIT = 1;
const HOT_MAX_LIMIT = 50;
const RECOMMEND_MIN_LIMIT = 1;
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;
}View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a plain integer, e.g. `--page 2`.
- Remove stray whitespace/characters from the value.
- In scripts, coerce with Math.trunc(Number(x)) after validating with Number.isInteger before calling.
- Omit --page entirely to use the default page (fallback 1).
Example fix
// before toutiao articles --page 1.5 // after toutiao articles --page 2
Defensive patterns
Strategy: validation
Validate before calling
function validatePage(raw) {
if (raw === undefined || raw === null || raw === '') return; // falls back to default
const n = Number(raw);
if (!Number.isInteger(n)) throw new TypeError(`--page must be an integer, got ${JSON.stringify(raw)}`);
} Type guard
function isIntegerPage(v) {
return typeof v === 'number' && Number.isInteger(v);
} Try / catch
try {
await articles({ page: rawPage });
} catch (e) {
if (e instanceof ArgumentError && e.message.startsWith('--page must be an integer')) {
console.error(`Invalid --page value; use e.g. --page 1`);
process.exitCode = 2;
return;
}
throw e;
} Prevention
- Always pass bare integers to --page (no units, decimals, or separators).
- Validate user input with Number.isInteger before invoking commands from scripts.
- Omit --page to accept the default rather than guessing a value.
- Quote shell arguments to avoid stray characters being appended.
When it happens
Trigger: Running an article command with `--page abc`, `--page 1.5`, `--page ''` is fine (falls back) but `--page 0x`, or any non-numeric/decimal string passed programmatically to parseArticlesPage.
Common situations: Typo in the CLI flag value ('page 2a'); copy-pasting a value with stray characters; scripting errors passing a float or string instead of an integer; locale-formatted numbers ('2' full-width).
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
- tid must be a numeric thread id
- --from and --to must differ (got ${fromCity})
- --from and --to must differ (got ${fromCode})
- --from and --to must differ (got ${fromName})
- Unknown --since "${sinceKey}". Valid: ${Object.keys(SINCE).j
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/b07416ed894c2bd8.
Report an issue: GitHub.