jackwener/OpenCLI · error · ArgumentError
nowcoder --${name} must be an integer from 1 to ${maximum}
Error message
nowcoder --${name} must be an integer from 1 to ${maximum} What it means
requirePositiveInt validates CLI option values such as --page-number and --limit. The value must be an integer (after Number coercion) between 1 and the provided maximum inclusive. Non-integers, values below 1, values above the cap, or non-numeric strings throw this ArgumentError naming the offending flag and its valid range.
Source
Thrown at clis/nowcoder/posts.js:234
uuid,
entity_id: entityId,
url: isContent
? `https://www.nowcoder.com/discuss/${id}`
: `https://www.nowcoder.com/feed/main/detail/${uuid}`,
title: optionalText(data.title, `${target.post_type} title`) || '(untitled)',
...authorFields(data.userBrief, expectedAuthorId, target.post_type),
content: body,
likes: metric(data.frequencyData, 'likeCnt'),
comments: metric(data.frequencyData, 'commentCnt'),
views: metric(data.frequencyData, 'viewCnt'),
time: isoTime(isContent ? data.createTime : data.createdAt, `${target.post_type} timestamp`),
location: optionalText(data.ip4Location, `${target.post_type} location`),
};
}
export function requirePositiveInt(value, name, maximum) {
const number = Number(value);
if (!Number.isInteger(number) || number < 1 || number > maximum) throw new ArgumentError(`nowcoder --${name} must be an integer from 1 to ${maximum}`);
return number;
}
export async function fetchNowcoderData(page, url, options, label) {
let payload;
try {
await page.goto('https://www.nowcoder.com');
payload = await page.fetchJson(url, options);
}
catch (error) {
const detail = String(error?.message ?? error);
if (/HTTP\s+(401|403)|need login|not logged in/i.test(detail)) {
throw new AuthRequiredError('nowcoder.com', `${label} requires a logged-in Nowcoder session`);
}
throw new CommandExecutionError(`${label} failed: ${detail}`);
}
if (!isRecord(payload) || typeof payload.success !== 'boolean' || !Number.isSafeInteger(payload.code)) throw new CommandExecutionError(`${label} returned a malformed envelope`);
const message = typeof payload.msg === 'string' ? payload.msg : 'unknown error';View on GitHub (pinned to 49907e53dc)
Solutions
- Pass an integer >= 1 and <= the documented maximum, e.g. --limit 50 --page-number 1.
- Check the CLI help output (nowcoder --help) for the exact per-flag maximum.
- Quote values carefully in your shell so the flag value isn't dropped or mangled.
- To page through more results, increment --page-number instead of raising --limit beyond the cap.
Example fix
// before nowcoder posts list --limit 0 nowcoder posts list --limit "" // after nowcoder posts list --limit 20 --page-number 1
Defensive patterns
Strategy: validation
Validate before calling
function validatePositiveInt(value, name, max) {
const n = Number(value);
if (!Number.isInteger(n) || n < 1 || n > max) {
throw new RangeError(`--${name} must be an integer from 1 to ${max}, got ${JSON.stringify(value)}`);
}
return n;
}
// validatePositiveInt(process.env.LIMIT, 'limit', 100) before invoking the CLI Type guard
function isValidPageOption(value, max = 100) {
const n = Number(value);
return Number.isInteger(n) && n >= 1 && n <= max;
} Try / catch
try {
await nowcoderPostsList({ limit, pageNumber });
} catch (err) {
if (err instanceof ArgumentError && /must be an integer from 1 to/.test(err.message)) {
console.error(err.message); process.exitCode = 2;
} else throw err;
} Prevention
- Always pass page-number >= 1 (pagination is 1-based).
- Clamp limit to the documented maximum in wrapper scripts.
- Quote flag values in shell to avoid empty/mangled values.
- Use the CLI help to check each flag's valid range before scripting.
When it happens
Trigger: Running nowcoder commands with --limit 0, --limit 200 when the max is lower, --page-number abc, --limit 10.5, or empty/whitespace values for these flags.
Common situations: Copy-pasting '--limit ' with a trailing value lost in shell quoting; assuming pagination starts at 0; trying to fetch more results per page than the CLI's configured maximum; using float or scientific notation values.
Understand the failure class
Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.
Related errors
- ${label} must be a non-negative integer, got ${JSON.stringif
- limit must be a positive integer
- archive snapshots url cannot be empty
- archive snapshots limit must be a positive integer
- archive snapshots limit must be <= 1000
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/4951016c595700c1.
Report an issue: GitHub.