jackwener/OpenCLI · error · ArgumentError

weixin search --${name} must be a positive integer

Error message

weixin search --${name} must be a positive integer

What it means

`normalizePositiveInteger` in the weixin search command validates --page and --limit inputs. If the value is not undefined/null and fails the /^\d+$/ whole-number test (empty string, non-numeric text, decimals like '1.5', or negatives like '-1'), it throws ArgumentError with message 'weixin search --<name> must be a positive integer'. ArgumentError carries code ARGUMENT and Unix exit code 2 (usage error).

Source

Thrown at clis/weixin/search.js:14

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');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass --page and --limit as plain positive integers, e.g. --page 1 --limit 10
  2. Remove thousands separators, signs, and decimals from the value before invoking the CLI
  3. In scripts, validate/coerce the value to an integer string before calling the command

Example fix

// before
opencli weixin search "golang" --limit 1,000   // ArgumentError
// after
opencli weixin search "golang" --limit 1000
Defensive patterns

Strategy: validation

Validate before calling

function isValidPositiveInt(v) {
  if (v === undefined || v === null) return true; // defaults apply
  return /^\d+$/.test(String(v).trim());
}
if (!isValidPositiveInt(page)) throw new Error('--page must be a positive integer');
if (!isValidPositiveInt(limit)) throw new Error('--limit must be a positive integer');

Type guard

function isPositiveIntString(v) {
  return typeof v === 'string' || typeof v === 'number'
    ? /^\d+$/.test(String(v).trim())
    : false;
}

Try / catch

try {
  await run(['weixin', 'search', q, '--page', String(page), '--limit', String(limit)]);
} catch (e) {
  if (e instanceof CliError && e.code === 'ARGUMENT') {
    console.error(`Bad flag value: ${e.message} (${e.hint})`); process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `opencli weixin search` with --page or --limit set to a non-integer string, e.g. `--page abc`, `--limit 1.5`, `--page -3`, `--limit ''`, or a value with whitespace/symbols like '1,000'.

Common situations: Scripts passing unquoted or malformed variables into the CLI flags; users copying '1,000' or '10 times' into --limit; locale-formatted numbers; a wrapper passing empty string when a value is unset.

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