jackwener/OpenCLI · error · ArgumentError

archive search limit must be a positive integer

Error message

archive search limit must be a positive integer

What it means

This ArgumentError is thrown before any network call by `archive search` when `limit` is not a positive integer. The value is coerced with Number() and must pass Number.isInteger and be > 0, so NaN, floats, zero, negatives, and non-numeric strings all fail. It is a client-side input validation error.

Source

Thrown at clis/archive/search.js:40

    args: [
        { name: 'query', positional: true, required: true, help: 'Full-text query (matches title, description, creator, subject).' },
        { name: 'mediatype', type: 'string', required: false, help: `Restrict to mediatype: ${MEDIATYPES.join(', ')}` },
        { name: 'sort', type: 'string', default: 'downloads', help: `Sort key: ${SORT_OPTIONS.join(', ')}` },
        { name: 'limit', type: 'int', default: 20, help: 'Max items (max 100; one API page).' },
    ],
    columns: ['rank', 'identifier', 'title', 'creator', 'date', 'mediatype', 'downloads', 'url'],
    func: async (args) => {
        const sortRaw = String(args.sort ?? 'downloads').toLowerCase();
        const sort = SORT_ALIAS[sortRaw] ?? sortRaw;
        if (!SORT_OPTIONS.includes(sort)) {
            throw new ArgumentError(`archive search sort must be one of ${SORT_OPTIONS.join(', ')}`);
        }
        if (args.mediatype && !MEDIATYPES.includes(String(args.mediatype))) {
            throw new ArgumentError(`archive search mediatype must be one of ${MEDIATYPES.join(', ')}`);
        }
        const limit = Number(args.limit ?? 20);
        if (!Number.isInteger(limit) || limit <= 0) {
            throw new ArgumentError('archive search limit must be a positive integer');
        }
        if (limit > 100) {
            throw new ArgumentError('archive search limit must be <= 100');
        }

        const query = String(args.query ?? '').trim();
        if (!query) {
            throw new ArgumentError('archive search query must not be empty');
        }

        const fullQuery = args.mediatype
            ? `(${query}) AND mediatype:${args.mediatype}`
            : query;

        const url = new URL('https://archive.org/advancedsearch.php');
        url.searchParams.set('q', fullQuery);
        url.searchParams.set('output', 'json');
        url.searchParams.set('rows', String(limit));

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a whole number >= 1, e.g. --limit 20
  2. Remove the --limit flag to use the default of 20
  3. Check the calling script for empty/unset variables feeding --limit

Example fix

// before
opencli archive search "ada lovelace" --limit "${LIMIT}"  # LIMIT unset -> NaN
// after
LIMIT="${LIMIT:-20}"
opencli archive search "ada lovelace" --limit "$LIMIT"
Defensive patterns

Strategy: validation

Validate before calling

const limit = Number(rawLimit ?? 20);
if (!Number.isInteger(limit) || limit <= 0) {
    throw new Error(`limit must be a positive integer, got ${rawLimit}`);
}

Type guard

const isValidLimit = (n) => Number.isInteger(n) && n > 0;

Try / catch

try {
    rows = await run(['archive', 'search', query, '--limit', String(limit)]);
} catch (err) {
    if (err instanceof ArgumentError && err.message.includes('limit must be a positive integer')) {
        rows = await run(['archive', 'search', query]); // default limit 20
    } else { throw err; }
}

Prevention

When it happens

Trigger: Calling `opencli archive search <query> --limit 0`, --limit -5, --limit 12.5, --limit abc, or --limit with an empty value that coerces to NaN.

Common situations: Passing a shell variable that is unset/empty so limit becomes NaN; assuming 0 means 'unlimited'; locale-formatted numbers like '1,000' or '1.000' that fail integer parsing; copying fractional page sizes from other APIs.

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