jackwener/OpenCLI · error · ArgumentError

${label} must be a positive integer

Error message

${label} must be a positive integer

What it means

normalizePositiveInteger validates numeric CLI options (like --min-count) before use, coercing via Number() and requiring a positive integer result. It throws ArgumentError when the value is missing-but-no-default, non-numeric, zero, negative, or fractional. This ensures downstream loops that fetch N images never run with nonsensical counts.

Source

Thrown at clis/grok/image.js:18

import * as fs from 'node:fs';
import * as path from 'node:path';
import * as crypto from 'node:crypto';
import { cli, Strategy } from '@jackwener/opencli/registry';
import { ArgumentError, CommandExecutionError, TimeoutError } from '@jackwener/opencli/errors';
import { GROK_URL, isOnGrok, normalizeBooleanFlag } from './utils.js';

const SESSION_HINT = 'Likely login/auth/challenge/session issue in the existing grok.com browser session.';

/**
 * Validate a positive-integer arg without silently flooring/clamping.
 * Throws ArgumentError on `0`, negatives, non-integers, or non-numeric input.
 */
function normalizePositiveInteger(value, defaultValue, label) {
  const raw = value ?? defaultValue;
  const n = Number(raw);
  if (!Number.isInteger(n) || n <= 0) {
    throw new ArgumentError(`${label} must be a positive integer`);
  }
  return n;
}

function dedupeBySrc(images) {
  const seen = new Set();
  const out = [];
  for (const img of images) {
    if (!img.src || seen.has(img.src)) continue;
    seen.add(img.src);
    out.push(img);
  }
  return out;
}

function imagesSignature(images) {
  return images.map(i => i.src).sort().join('|');
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a positive whole number (>= 1) for the option, or omit it to use the default.
  2. If 'no minimum' is intended, drop the flag rather than passing 0.
  3. Quote values in the shell so empty/whitespace values don't slip through as ''.

Example fix

// before
node cli.js image --min-count 0
// after
node cli.js image --min-count 3   # or omit the flag entirely
Defensive patterns

Strategy: validation

Validate before calling

function isValidPositiveInteger(v) {
  const n = Number(v);
  return Number.isInteger(n) && n > 0;
}
const minCount = process.env.MIN_COUNT;
if (minCount !== undefined && !isValidPositiveInteger(minCount)) {
  throw new Error(`min-count must be a positive integer, got: ${minCount}`);
}

Type guard

function isPositiveInteger(v) {
  return typeof v === 'number' && Number.isInteger(v) && v > 0;
}

Try / catch

try {
  await cli.image({ minCount });
} catch (e) {
  if (e instanceof ArgumentError && /positive integer/.test(e.message)) {
    console.error(`Bad --min-count value: ${JSON.stringify(minCount)}`);
    process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Calling minCount with --min-count=0, --min-count=-3, --min-count=2.5, --min-count=abc, or an empty string with no default configured.

Common situations: Typo in the CLI flag value, shell passing an empty string for an unset variable, or a user trying to say 'no minimum' with 0 instead of omitting the flag.

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