jackwener/OpenCLI · error · ArgumentError

weread-official: ${label} is required

Error message

weread-official: ${label} is required

What it means

requirePositiveInt validates that an option/argument is present and is a positive integer. When the value is undefined, null, or an empty string and no defaultValue was supplied, it throws this ArgumentError naming the missing parameter via ${label}. It is a fail-fast guard so commands never issue API requests with invalid paging/count arguments.

Source

Thrown at clis/weread-official/utils.js:262

export function requireText(value, label) {
    const text = String(value ?? '').trim();
    if (!text) throw new ArgumentError(`weread-official: ${label} cannot be empty`);
    return text;
}

export function requireBookId(value, label = 'bookId') {
    const text = requireText(value, label);
    if (!/^[A-Za-z0-9_-]+$/.test(text)) {
        throw new ArgumentError(`weread-official: ${label} contains invalid characters`, 'Pass a bookId from `weread-official search`.');
    }
    return text;
}

export function requirePositiveInt(value, label, { defaultValue, max } = {}) {
    if (value === undefined || value === null || value === '') {
        if (defaultValue === undefined) {
            throw new ArgumentError(`weread-official: ${label} is required`);
        }
        return defaultValue;
    }
    const text = String(value).trim();
    if (!/^\d+$/.test(text)) {
        throw new ArgumentError(`weread-official: ${label} must be a positive integer`);
    }
    const n = Number(text);
    if (!Number.isSafeInteger(n) || n < 1) {
        throw new ArgumentError(`weread-official: ${label} must be a positive integer`);
    }
    if (max !== undefined && n > max) {
        throw new ArgumentError(`weread-official: ${label} must be <= ${max}`);
    }
    return n;
}

export function requireChoice(value, choices, label, defaultValue) {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass the required option, e.g. --count 10 / --limit 5, matching the label in the message.
  2. If the caller should have a default, pass { defaultValue: N } to requirePositiveInt at the call site.
  3. Check the command's --help output for the exact flag name; the flag may have been renamed.
  4. In scripts, guard shell variables: use ${COUNT:-10} or exit early with a clear message before invoking the CLI.

Example fix

// before
runListNotebooks({});
// after
runListNotebooks({ limit: 10 }); // or requirePositiveInt(opts.limit, 'limit', { defaultValue: 20 })
Defensive patterns

Strategy: validation

Validate before calling

const raw = process.env.NOTEBOOK_COUNT ?? opts.count;
if (raw === undefined || raw === null || raw === '') {
  throw new Error('count/limit option is required before calling this command');
}

Type guard

function isProvided(v) { return v !== undefined && v !== null && v !== ''; }

Try / catch

try {
  await cli.run(['listNotebooks', '--count', count]);
} catch (e) {
  if (e instanceof ArgumentError && /is required$/.test(e.message)) {
    console.error('Missing option:', e.message); process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Calling requirePositiveInt(value, label) from commands like count or listNotebooks without passing a defaultValue, while the user omitted the flag (e.g. --count not provided, or explicitly empty).

Common situations: Users running the CLI without the required option; scripts passing empty shell variables (FOO="" cli ...); programmatic callers omitting an options object field; a changed CLI flag name in a newer version.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/41709c3894f1cfbd. Report an issue: GitHub.