jackwener/OpenCLI · error · ArgumentError

zhihu collection --${name} must be a positive integer

Error message

zhihu collection --${name} must be a positive integer

What it means

The zhihu collection command validates numeric options with validatePositiveInt, which throws ArgumentError when a value passed as a positive-integer option (e.g. --limit) is not an integer or is <= 0. This is input validation before any API call — the collection is never fetched.

Source

Thrown at clis/zhihu/collection.js:9

import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { log } from '@jackwener/opencli/logger';
import { stripHtml } from './text.js';

function validatePositiveInt(value, name) {
  const n = Number(value);
  if (!Number.isInteger(n) || n <= 0) {
    throw new ArgumentError(`zhihu collection --${name} must be a positive integer`, 'Example: opencli zhihu collection 83283292 --limit 20');
  }
  return n;
}

function validateNonNegativeInt(value, name) {
  const n = Number(value);
  if (!Number.isInteger(n) || n < 0) {
    throw new ArgumentError(`zhihu collection --${name} must be a non-negative integer`, 'Example: opencli zhihu collection 83283292 --offset 0');
  }
  return n;
}

async function fetchCollectionPage(page, collectionId, offset, limit) {
  const url = `https://www.zhihu.com/api/v4/collections/${collectionId}/items?offset=${offset}&limit=${limit}`;
  const data = await page.evaluate(`
    (async () => {
      const r = await fetch(${JSON.stringify(url)}, { credentials: 'include' });
      if (!r.ok) return { __httpError: r.status };

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a positive integer, e.g. `--limit 20`.
  2. Check shell variables for emptiness/typos before interpolation; use defaults like `${COUNT:-20}`.
  3. Use the suggested example from the error: `opencli zhihu collection 83283292 --limit 20`.
  4. Catch ArgumentError in wrappers and validate/normalize the option first (parseInt, check > 0).

Example fix

// before
opencli zhihu collection 83283292 --limit 0
// ArgumentError: --limit must be a positive integer
// after
opencli zhihu collection 83283292 --limit 20
Defensive patterns

Strategy: validation

Validate before calling

function toPositiveInt(v) { const n = Number(v); if (!Number.isInteger(n) || n <= 0) throw new Error(`--limit must be a positive integer, got: ${JSON.stringify(v)}`); return n; }

Type guard

function isPositiveInt(v) { return Number.isInteger(Number(v)) && Number(v) > 0; }

Try / catch

try { await collection(args); } catch (e) { if (/must be a positive integer/.test(e.message)) { console.error('Usage: opencli zhihu collection <id> --limit 20'); } throw e; }

Prevention

When it happens

Trigger: Passing `--limit 0`, `--limit -5`, `--limit abc`, `--limit 2.5`, or an empty string to `opencli zhihu collection <id>`; requestedLimit() calls validatePositiveInt on the option value.

Common situations: Shell variable interpolating empty or malformed values (`--limit "${COUNT}"` where COUNT is unset); copy-paste with decimals or thousands separators like `--limit 1,000`; scripting mistakes passing a float from JSON config.

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


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/45f85d8552a055b0. Report an issue: GitHub.