jackwener/OpenCLI · error · ArgumentError

weixin search --${name} is out of range

Error message

weixin search --${name} is out of range

What it means

After passing the digit-format test, `normalizePositiveInteger` checks that the parsed value is a safe integer, >= 1, and (when a maxValue is given) <= maxValue. If any check fails it throws ArgumentError with message 'weixin search --<name> is out of range', hinting the accepted range. This prevents nonsensical pages (0 or negative overflow like 00000) and limits beyond the backend's maximum. Exit code is 2 (usage error).

Source

Thrown at clis/weixin/search.js:18

import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { cli, Strategy } from '@jackwener/opencli/registry';

const SOGOU_WEIXIN_DOMAIN = 'weixin.sogou.com';
const DEFAULT_PAGE = 1;
const DEFAULT_LIMIT = 10;
const MAX_LIMIT = 10;

function normalizePositiveInteger(value, name, defaultValue, maxValue) {
    if (value === undefined || value === null)
        return defaultValue;
    const text = String(value).trim();
    if (!/^\d+$/.test(text)) {
        throw new ArgumentError(`weixin search --${name} must be a positive integer`, `Pass --${name} as a whole number${maxValue ? ` from 1 to ${maxValue}` : ' greater than 0'}.`);
    }
    const parsed = Number(text);
    if (!Number.isSafeInteger(parsed) || parsed < 1 || (maxValue && parsed > maxValue)) {
        throw new ArgumentError(`weixin search --${name} is out of range`, `Pass --${name} as a whole number${maxValue ? ` from 1 to ${maxValue}` : ' greater than 0'}.`);
    }
    return parsed;
}

function normalizePage(page) {
    return normalizePositiveInteger(page, 'page', DEFAULT_PAGE);
}

function normalizeLimit(limit) {
    return normalizePositiveInteger(limit, 'limit', DEFAULT_LIMIT, MAX_LIMIT);
}

function buildSearchUrl(query, pageNo) {
    const searchUrl = new URL('https://weixin.sogou.com/weixin');
    searchUrl.searchParams.set('query', query);
    searchUrl.searchParams.set('type', '2');
    searchUrl.searchParams.set('page', String(pageNo));
    searchUrl.searchParams.set('ie', 'utf8');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use --page >= 1 and --limit >= 1 within the command's documented maximum
  2. Clamp or cap the value in your script before invoking the CLI
  3. If you want 'as many as possible', omit --limit to get the built-in default instead of a huge number

Example fix

// before
opencli weixin search "golang" --limit 999999999999999999999  // out of range
// after
const limit = Math.min(Math.max(1, Number(userLimit) || 10), 50);
opencli weixin search "golang" --limit limit
Defensive patterns

Strategy: validation

Validate before calling

function clampInt(v, min, max, fallback) {
  if (v === undefined || v === null) return fallback;
  const n = Number(String(v).trim());
  if (!Number.isSafeInteger(n)) return fallback;
  return Math.min(Math.max(n, min), max);
}
const page = clampInt(rawPage, 1, undefined, 1);
const limit = clampInt(rawLimit, 1, 50, 10);

Type guard

function isInRangePositiveInt(v, maxValue) {
  if (!/^\d+$/.test(String(v).trim())) return false;
  const n = Number(v);
  return Number.isSafeInteger(n) && n >= 1 && (!maxValue || n <= maxValue);
}

Try / catch

try {
  await run(['weixin', 'search', q, '--page', String(page)]);
} catch (e) {
  if (e instanceof CliError && e.code === 'ARGUMENT' && e.message.includes('out of range')) {
    // retry with clamped values
    await run(['weixin', 'search', q, '--page', '1', '--limit', '10']);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `opencli weixin search` with --page 0, --limit 0, an astronomically large value exceeding Number.isSafeInteger bounds (e.g. '99999999999999999999'), or --limit above the maxValue configured for the command.

Common situations: Users trying --page 0 expecting 1-indexed-vs-0-indexed confusion; scripts computing limits that overflow; passing sentinel values like 999999 to mean 'everything'; copy-pasted oversized numbers.

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


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