jackwener/OpenCLI · error · ArgumentError

${label} cannot be empty

Error message

${label} cannot be empty

What it means

ArgumentError from requireString in clis/stackoverflow/utils.js when a required string argument is missing, null, or whitespace-only after String() coercion and trim(). Used by commands like `stackoverflow tag` (label 'tag') to reject empty required inputs before any network call.

Source

Thrown at clis/stackoverflow/utils.js:34

const UA = 'opencli-stackoverflow (+https://github.com/jackwener/opencli)';

/** Validate `limit` per typed-fail-fast convention (no silent clamp). */
export function normalizeLimit(value, defaultValue, maxValue, label = 'limit') {
    const raw = value ?? defaultValue;
    const limit = Number(raw);
    if (!Number.isInteger(limit) || limit <= 0) {
        throw new ArgumentError(`${label} must be a positive integer`);
    }
    if (limit > maxValue) {
        throw new ArgumentError(`${label} must be <= ${maxValue}`);
    }
    return limit;
}

export function requireString(value, label) {
    const raw = String(value ?? '').trim();
    if (!raw) {
        throw new ArgumentError(`${label} cannot be empty`);
    }
    return raw;
}

/** Fetch a Stack Exchange API endpoint and return parsed JSON envelope. */
export async function seFetch(path, { searchParams } = {}) {
    const url = new URL(path.startsWith('http') ? path : `${SE_API}${path.startsWith('/') ? '' : '/'}${path}`);
    if (searchParams) {
        for (const [k, v] of Object.entries(searchParams)) {
            if (v == null || v === '') continue;
            url.searchParams.set(k, String(v));
        }
    }
    if (!url.searchParams.has('site')) url.searchParams.set('site', SE_SITE);

    let resp;
    try {
        resp = await fetch(url, {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Supply a non-empty tag value, e.g. `stackoverflow tag javascript`.
  2. Trim and validate in scripts before calling: if (!tag?.trim()) skip.
  3. Check that the shell variable actually holds a value (`echo "$TAG"`).

Example fix

// before
stackoverflow tag "$TAG"   // TAG empty
// after
[ -n "$TAG" ] && stackoverflow tag "$TAG"
Defensive patterns

Strategy: validation

Validate before calling

function requireNonEmpty(value, label) {
  const s = String(value ?? '').trim();
  if (!s) throw new TypeError(`${label} is required and cannot be empty`);
  return s;
}

Type guard

function isNonEmptyString(v) { return typeof v === 'string' && v.trim().length > 0; }

Try / catch

try {
  await byTag(tag);
} catch (e) {
  if (e.name === 'ArgumentError' && e.message.includes('cannot be empty')) {
    console.error(`usage: stackoverflow tag <non-empty tag>`);
    process.exitCode = 2;
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: `stackoverflow tag` with no value, `stackoverflow tag " "`, or a script passing an unset variable so the tag resolves to '' or undefined.

Common situations: Shell variables that expand to empty (TAG=""); piping data where the field is blank; forgetting the positional argument; a tag consisting only of whitespace due to copy/paste.

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