jackwener/OpenCLI · error · ArgumentError

youtube search limit must be a positive integer

Error message

youtube search limit must be a positive integer

What it means

ArgumentError from normalizeLimit rejecting a --limit that is not a positive integer. The value is coerced with Number(value ?? 20); non-numeric strings become NaN and decimals fail the Number.isInteger check, so anything <= 0, NaN, or Infinity throws before the request is made.

Source

Thrown at clis/youtube/search.js:50

const SORT_FILTERS = {
    relevance: '',
    date: 'CAI%3D',
    views: 'CAM%3D',
    rating: 'CAE%3D',
};

function normalizeChoice(value, choices, label) {
    const normalized = String(value || '').trim();
    if (normalized && !Object.hasOwn(choices, normalized)) {
        throw new ArgumentError(`youtube search ${label} must be one of: ${Object.keys(choices).join(', ')}`);
    }
    return normalized;
}

function normalizeLimit(value) {
    const limit = Number(value ?? DEFAULT_LIMIT);
    if (!Number.isInteger(limit) || limit <= 0) {
        throw new ArgumentError('youtube search limit must be a positive integer');
    }
    if (limit > MAX_LIMIT) {
        throw new ArgumentError(`youtube search limit must be <= ${MAX_LIMIT}`);
    }
    return limit;
}

cli({
    site: 'youtube',
    name: 'search',
    access: 'read',
    description: 'Search YouTube videos, Shorts, channels, and playlists',
    domain: 'www.youtube.com',
    strategy: Strategy.COOKIE,
    args: [
        { name: 'query', required: true, positional: true, help: 'Search query' },
        { name: 'limit', type: 'int', default: DEFAULT_LIMIT, help: 'Max results (max 50)' },
        { name: 'type', default: '', help: 'Filter type: shorts, video, channel, playlist' },

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass an integer between 1 and 50, e.g. --limit 20
  2. Sanitize shell/config values: trim, Number() them, and check Number.isInteger(n) && n > 0 before calling
  3. Omit --limit to use the default of 20

Example fix

// before
opencli youtube search "cats" --limit "${COUNT}"  // COUNT empty -> 0
// after
LIMIT=${COUNT:-20}; opencli youtube search "cats" --limit "$LIMIT"
Defensive patterns

Strategy: validation

Validate before calling

function normalizeLimit(value, { DEFAULT = 20, MAX = 50 } = {}) {
  const n = Number(value ?? DEFAULT);
  if (!Number.isInteger(n) || n <= 0) throw new Error(`limit must be a positive integer, got: ${JSON.stringify(value)}`);
  return Math.min(n, MAX);
}

Type guard

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

Try / catch

try {
  await run('youtube search', [query, '--limit', String(limit)]);
} catch (e) {
  if (/limit must be a positive integer/i.test(e.message)) {
    console.error(`Invalid --limit ${limit}; using default 20`);
    return run('youtube search', [query]);
  }
  throw e;
}

Prevention

When it happens

Trigger: `--limit 0`, `--limit -5`, `--limit abc`, `--limit 10.5`, `--limit ""` (empty string coerces to 0), or a script passing null/undefined in a way that coerces to NaN.

Common situations: Off-by-one loops generating 0, shell variables that are empty or contain whitespace/newlines, config files with string limits like "25 items", or float division results passed straight through.

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