jackwener/OpenCLI · error · ArgumentError
paragraphs must be a non-negative integer (0 = full article)
Error message
paragraphs must be a non-negative integer (0 = full article)
What it means
The optional paragraphs argument caps the article extract to its first N paragraphs; 0 means full article. The value is coerced with Number() and must be a non-negative integer — anything else (negative, fractional, NaN, non-numeric string) throws this ArgumentError.
Source
Thrown at clis/wikipedia/page.js:38
browser: false,
args: [
{ name: 'title', positional: true, required: true, type: 'string', help: 'Article title (e.g. "Transformer (machine learning model)")' },
{ name: 'lang', type: 'string', default: 'en', help: 'Language code (en, zh, ja, de, ...).' },
{ name: 'paragraphs', type: 'int', default: 0, help: 'Cap to first N paragraphs (0 = full article).' },
],
columns: ['title', 'description', 'pageId', 'paragraphs', 'extract', 'url'],
func: async (args) => {
const title = String(args.title ?? '').trim();
if (!title) {
throw new ArgumentError('wikipedia page title cannot be empty');
}
const lang = String(args.lang ?? 'en').trim().toLowerCase();
if (!/^[a-z]{2,3}(?:-[a-z0-9]+)?$/.test(lang)) {
throw new ArgumentError(`wikipedia lang must be a language code like en, zh, ja (got "${args.lang}")`);
}
const paragraphsCap = Number(args.paragraphs ?? 0);
if (!Number.isInteger(paragraphsCap) || paragraphsCap < 0) {
throw new ArgumentError('paragraphs must be a non-negative integer (0 = full article)');
}
const url = new URL(`https://${lang}.wikipedia.org/w/api.php`);
url.searchParams.set('action', 'query');
url.searchParams.set('format', 'json');
url.searchParams.set('formatversion', '2');
url.searchParams.set('prop', 'extracts|info|description');
url.searchParams.set('inprop', 'url');
url.searchParams.set('explaintext', '1');
url.searchParams.set('redirects', '1');
url.searchParams.set('titles', title);
let resp;
try {
resp = await fetch(url, {
headers: {
'User-Agent': 'opencli/1.0 (+https://github.com/jackwener/opencli)',
'Accept': 'application/json',View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a non-negative integer (0 for the full article)
- Fix shell/config values so they are numeric (e.g. '5', '0')
- Drop the flag entirely — the default is 0 (full article)
Example fix
// before opencli wikipedia page "Transformer" --paragraphs -1 // after opencli wikipedia page "Transformer" --paragraphs 0 # 0 = full article
Defensive patterns
Strategy: validation
Validate before calling
const cap = Number(args.paragraphs ?? 0);
if (!Number.isInteger(cap) || cap < 0) throw new Error('paragraphs must be a non-negative integer (0 = full)'); Type guard
function isParagraphCap(v) { const n = Number(v ?? 0); return Number.isInteger(n) && n >= 0; } Try / catch
try {
return await pageCommand(args);
} catch (err) {
if (err instanceof ArgumentError && err.message.includes('paragraphs')) {
return await pageCommand({ ...args, paragraphs: 0 }); // fall back to full article
}
throw err;
} Prevention
- Remember 0 means 'no cap' — never use -1 for unlimited
- Coerce and validate the value before passing to the CLI
- Sanitize config-driven values to integers
- Omit the flag when you want the default (full article)
When it happens
Trigger: Passing --paragraphs -1 or a negative number to disable capping (0 is the correct 'no cap' value); passing a float like 2.5; passing a non-numeric string like 'all' or '10x'; leaving a NaN-producing value such as an empty string that Number() coerces from an invalid input.
Common situations: Scripts substituting '-1' or 'none' for 'unlimited'; users copying flags from other tools that use -1 as 'all'; config files storing the cap as "" or null-adjacent strings.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- wikipedia page title cannot be empty
- series_id must be a non-empty value
- Invalid Chess.com username "${value}" Usernames are 3-25 cha
- coingecko derivatives limit must be a positive integer
- limit must be a positive integer
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/13f572cb20debc64.
Report an issue: GitHub.