jackwener/OpenCLI · error · ArgumentError
packagist ${label} must be a positive integer
Error message
packagist ${label} must be a positive integer What it means
requireBoundedInt coerces its input with Number() and throws this ArgumentError when the result is not an integer or is <= 0. The adapter uses it to validate the `limit` option so only sensible positive page sizes are sent to Packagist. Values like 0, -1, NaN, 'abc', or 2.5 all land here.
Source
Thrown at clis/packagist/utils.js:24
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
export const PACKAGIST_BASE = 'https://packagist.org';
const UA = 'opencli-packagist-adapter (+https://github.com/jackwener/opencli)';
// Each segment of a Composer package name (`vendor` and `package`).
const SEGMENT = /^[a-z0-9]([_.-]?[a-z0-9]+)*$/;
export function requireString(value, label) {
const s = String(value ?? '').trim();
if (!s) throw new ArgumentError(`packagist ${label} cannot be empty`);
return s;
}
export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
const raw = value ?? defaultValue;
const n = typeof raw === 'number' ? raw : Number(raw);
if (!Number.isInteger(n) || n <= 0) {
throw new ArgumentError(`packagist ${label} must be a positive integer`);
}
if (n > maxValue) {
throw new ArgumentError(`packagist ${label} must be <= ${maxValue}`);
}
return n;
}
export function requirePackageName(value) {
const raw = String(value ?? '').trim().toLowerCase();
if (!raw) {
throw new ArgumentError('packagist package name is required (e.g. "symfony/console", "monolog/monolog")');
}
const slash = raw.indexOf('/');
if (slash <= 0 || slash === raw.length - 1) {
throw new ArgumentError(
`packagist package "${value}" must be "<vendor>/<package>"`,
'Both segments are required (Composer convention).',
);View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a positive integer (>= 1) for the labeled option.
- If the value is a string, parse it with parseInt/Number and verify Number.isInteger before calling.
- Clamp user-supplied values: Math.max(1, Math.floor(n)).
- Catch ArgumentError and fall back to a sane default limit.
Example fix
// before
run({ limit: opts.limit }); // '20x' -> NaN -> throws
// after
const n = Number.parseInt(opts.limit ?? '10', 10);
run({ limit: Number.isInteger(n) && n > 0 ? n : 10 }); Defensive patterns
Strategy: validation
Validate before calling
function toPositiveInt(v, dflt = 10) {
if (v == null) return dflt;
const n = typeof v === 'number' ? v : Number(v);
return Number.isInteger(n) && n > 0 ? n : dflt;
} Type guard
const isPositiveInt = (v) => typeof v === 'number' && Number.isInteger(v) && v > 0;
Try / catch
try {
await run({ limit });
} catch (e) {
if (e instanceof ArgumentError && /positive integer/.test(e.message)) {
return run({ limit: 10 });
}
throw e;
} Prevention
- Coerce CLI flags with parseInt and validate with Number.isInteger before use.
- Reject NaN early — Number('') and Number('abc') are the usual culprits.
- Clamp defaults: Math.max(1, Math.floor(raw)).
- Document valid ranges in --help output.
When it happens
Trigger: Calling limit (which calls requireBoundedInt) with limit=0, a negative number, a non-numeric string like 'ten', a float like 1.5, or a string like '' that coerces to NaN.
Common situations: User passes --limit 0 or --limit abc on the CLI; a config file stores limit as a string that fails Number() parsing; code computes a limit via arithmetic that yields NaN (e.g. parseInt of malformed input).
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- ${flagLabel} must be a positive integer
- ${flagLabel} must be a non-negative integer
- juejin ${label} must be a positive decimal integer
- juejin ${label} must be a positive integer
- juejin cursor must be a non-negative decimal integer
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/469974eae6016c82.
Report an issue: GitHub.