jackwener/OpenCLI · error · ArgumentError
--max-content must be a non-negative integer (0 = no cap, fu
Error message
--max-content must be a non-negative integer (0 = no cap, full content)
What it means
This ArgumentError is thrown during argument validation of the zhihu answer-detail command when the --max-content option is provided but is not a non-negative integer. 0 means no cap (full stripped answer is returned); any positive value is an explicit user cap on content length, mirroring the wikipedia `page` pattern so the tool never silently truncates. Non-integers (e.g. 'abc', 1.5) and negative numbers are rejected outright.
Source
Thrown at clis/zhihu/answer-detail.js:54
],
columns: ['id', 'author', 'votes', 'comments', 'question_id', 'question_title', 'url', 'created_at', 'updated_at', 'content'],
func: async (page, kwargs) => {
const target = parseAnswerTarget(kwargs.id);
if (!target) {
throw new ArgumentError(
'Answer ID must be a numeric id, a Zhihu answer URL, or answer:<qid>:<aid>',
'Example: opencli zhihu answer-detail 1937205528846655537',
);
}
const { answerId } = target;
// `--max-content 0` (the default) means "no cap, return the
// full stripped answer". Any positive value is an opt-in user
// cap, mirroring the wikipedia `page` pattern — we never
// silently truncate behind the user's back.
const rawMaxContent = kwargs['max-content'];
const maxContent = rawMaxContent == null ? 0 : Number(rawMaxContent);
if (!Number.isInteger(maxContent) || maxContent < 0) {
throw new ArgumentError(
'--max-content must be a non-negative integer (0 = no cap, full content)',
'Example: --max-content 2000',
);
}
// Navigate to the answer page itself: this both seeds the
// cookie/anti-bot context and works even when the caller did
// not supply the parent question id (Zhihu redirects from
// `/answer/<aid>` to the canonical `/question/<qid>/answer/<aid>`).
try {
await page.goto(`https://www.zhihu.com/answer/${answerId}`);
} catch (err) {
throw new CommandExecutionError(
`Failed to open Zhihu answer ${answerId}: ${err instanceof Error ? err.message : String(err)}`,
'Open the answer URL in Chrome and retry after the page is reachable.',
);
}
const currentQuestionId = page.getCurrentUrl
? extractQuestionIdFromAnswerUrl(await page.getCurrentUrl().catch(() => ''))View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a plain non-negative integer, e.g. --max-content 2000, or omit the flag entirely for full content (0 = no cap).
- Validate/normalize the value before invoking: Math.trunc a float and clamp negatives to 0.
- If the value comes from a script/env var, ensure it is an unquoted digit string without units or sign.
Example fix
// before clis zhihu answer-detail --max-content -1 // after clis zhihu answer-detail --max-content 2000
Defensive patterns
Strategy: validation
Validate before calling
const raw = process.argv.maxContent;
const n = raw == null ? 0 : Number(raw);
if (!Number.isInteger(n) || n < 0) throw new Error('--max-content must be a non-negative integer (0 = no cap)'); Type guard
function isValidMaxContent(v) { const n = v == null ? 0 : Number(v); return Number.isInteger(n) && n >= 0; } Try / catch
try {
await answerDetail({ 'max-content': raw });
} catch (err) {
if (err instanceof ArgumentError) {
console.error(`Bad --max-content: ${err.message}`);
process.exitCode = 2;
} else throw err;
} Prevention
- Omit the flag when you want full content instead of passing 0 or a sentinel.
- Clamp/normalize values in wrapper scripts: Math.max(0, Math.trunc(Number(v))).
- Never pass floats, negatives, or strings with unit suffixes.
When it happens
Trigger: Running `clis zhihu answer-detail --max-content -1` (negative), `--max-content 2.5` (non-integer), or `--max-content abc` (non-numeric string), i.e. any value where Number.isInteger(maxContent) is false or maxContent < 0 after Number(rawMaxContent) coercion.
Common situations: Typo in the CLI flag value, copy-pasting a value with a units suffix like '2000ch', shell scripts interpolating an empty or negative default (e.g. MAX=-1), or assuming the flag accepts floats for partial caps.
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
- bilibili comment ${label} must be a positive integer
- bilibili comment message cannot be empty
- bilibili unfollow target cannot be empty
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/45781f9c147ef255.
Report an issue: GitHub.