jackwener/OpenCLI · error · ArgumentError
zhihu search --limit must be a positive integer no greater t
Error message
zhihu search --limit must be a positive integer no greater than ${MAX_LIMIT} What it means
The --limit option passed to `zhihu search` failed parseLimit's validation: it must be a positive integer no greater than MAX_LIMIT. The CLI rejects non-integers, zero/negative values, and oversized limits to avoid slow requests and tripping Zhihu risk controls. This is thrown before any network request is made.
Source
Thrown at clis/zhihu/search.js:49
return `https://www.zhihu.com/api/v4/search_v3${parsed.search}`;
}
if (parsed.hostname === 'www.zhihu.com' && parsed.pathname === '/api/v4/search_v3') {
return parsed.toString();
}
} catch {
return '';
}
return '';
}
const MAX_LIMIT = 1000;
const PAGE_SIZE = 20;
const TYPES = ['all', 'answer', 'article', 'question'];
function parseLimit(value) {
const limit = Number(value ?? 10);
if (!Number.isInteger(limit) || limit <= 0 || limit > MAX_LIMIT) {
throw new ArgumentError(`zhihu search --limit must be a positive integer no greater than ${MAX_LIMIT}`, 'Use a normal-sized limit to avoid slow requests or Zhihu risk controls');
}
return limit;
}
function requireQuery(value) {
const query = String(value || '').trim();
if (!query) {
throw new ArgumentError('zhihu search query must not be empty', 'Example: opencli zhihu search codex');
}
return query;
}
function requireType(value) {
const type = String(value || 'all');
if (!TYPES.includes(type)) {
throw new ArgumentError(`zhihu search --type must be one of: ${TYPES.join(', ')}`, 'Example: opencli zhihu search codex --type answer');
}
return type;View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a positive integer, e.g. --limit 20
- Check the CLI's MAX_LIMIT constant in clis/zhihu/search.js and use a value at or below it
- Quote or trim the value if it comes from a shell variable to avoid stray whitespace
- Omit --limit entirely to use the default of 10
Example fix
// before opencli zhihu search codex --limit 500 // after opencli zhihu search codex --limit 20
Defensive patterns
Strategy: validation
Validate before calling
function safeLimit(v) {
const n = Number(v ?? 10);
if (!Number.isInteger(n) || n <= 0 || n > MAX_LIMIT) throw new Error(`--limit must be an integer 1..${MAX_LIMIT}`);
return n;
} Type guard
function isValidLimit(v) {
return Number.isInteger(v) && v > 0 && v <= MAX_LIMIT;
} Try / catch
try {
await runSearch({ limit: userLimit });
} catch (err) {
if (err instanceof ArgumentError && err.message.includes('--limit')) {
console.error('Invalid --limit; using default 10.');
return runSearch({ limit: 10 });
}
throw err;
} Prevention
- Always pass explicit small integer limits (10–20) when scripting
- Clamp user-supplied limits with Math.min/Math.floor before invoking the CLI
- Quote shell variables to prevent stray whitespace or empty values
- Check MAX_LIMIT in clis/zhihu/search.js when increasing limits
When it happens
Trigger: Running `opencli zhihu search <query> --limit` with e.g. --limit 0, --limit -5, --limit abc, --limit 2.5, or any integer above MAX_LIMIT; also any non-numeric string (Number(value) yields NaN).
Common situations: Typo or stray characters in the flag value; copying a default like 100 from docs when MAX_LIMIT is smaller; shell interpolation producing an empty or malformed value; passing '10 ' with whitespace from a script.
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
- zhihu search query must not be empty
- 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})
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/27a33aeb4614cea3.
Report an issue: GitHub.