jackwener/OpenCLI · warning · ArgumentError

weibo favorites ${name} must be a positive integer

Error message

weibo favorites ${name} must be a positive integer

What it means

clis/weibo/favorites.js:12 — parsePositiveInt throws ArgumentError(`weibo favorites ${name} must be a positive integer`) when a numeric option (e.g. limit, called via the limit option handler) is not an integer or is <= 0. Defaults apply only when the value is nullish; any provided value must be a strictly positive integer. MAX_LIMIT is 50.

Source

Thrown at clis/weibo/favorites.js:12

import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { getSelfUid, requireArrayEvaluateResult, unwrapEvaluateResult } from './utils.js';

const DEFAULT_LIMIT = 20;
const MAX_LIMIT = 50;

function parsePositiveInt(value, name, defaultValue) {
  const raw = value ?? defaultValue;
  const number = Number(raw);
  if (!Number.isInteger(number) || number <= 0) {
    throw new ArgumentError(`weibo favorites ${name} must be a positive integer`);
  }
  if (number > MAX_LIMIT) {
    throw new ArgumentError(`weibo favorites ${name} must be <= ${MAX_LIMIT}`);
  }
  return number;
}

function parseFavoriteCard(card, favUrl) {
  const raw = String(card?.text ?? '');
  const lines = raw.split('\n');

  let author = '';
  let time = '';
  let source = '';
  let content = '';
  let likes = '0';
  let comments = '0';
  let reposts = '0';

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a positive integer, e.g. limit: 20 (omit the option entirely to use the default)
  2. Coerce and validate user/config input before the call: Number(value) and Number.isInteger check
  3. Trim numeric strings; empty string is not a valid value — drop it so the default applies
  4. Remember the hard cap is 50; larger values need the <=50 error (see next entry)

Example fix

// before
await cli.run('weibo favorites', { limit: '20 ' }); // ArgumentError
// after
const n = Number(String(cfg.limit ?? '').trim());
await cli.run('weibo favorites', { limit: Number.isInteger(n) && n > 0 ? n : undefined });
Defensive patterns

Strategy: validation

Validate before calling

function toPositiveInt(v, fallback = 20) {
  if (v == null || v === '') return fallback;
  const n = Number(v);
  return Number.isInteger(n) && n > 0 ? n : fallback;
}

Type guard

function isPositiveInt(v) { return typeof v === 'number' && Number.isInteger(v) && v > 0; }

Try / catch

try {
  await cli.run('weibo favorites', { limit });
} catch (e) {
  if (e instanceof Error && /must be a positive integer/.test(e.message)) {
    console.error(`limit must be a positive integer, got: ${JSON.stringify(limit)}`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling `weibo favorites` with limit=0, a negative number, a non-integer like 2.5, or a non-numeric string such as limit="abc" or "" (Number('') is 0).

Common situations: Users passing 0 expecting 'unlimited'; config/env strings like '20 ' or 'twenty' not being pre-parsed; scripts interpolating empty variables into limit; fractional values from dividing counts.

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/9c0cdb0e115644f1. Report an issue: GitHub.