jackwener/OpenCLI · error · ArgumentError

pubmed ${label} cannot be empty

Error message

pubmed ${label} cannot be empty

What it means

requireText is the shared validator for text arguments (name, query, journal, term, pmid, terms). It stringifies, trims, and throws ArgumentError 'pubmed <label> cannot be empty' when nothing remains, failing fast instead of sending a blank parameter to NCBI.

Source

Thrown at clis/pubmed/utils.js:13

import { ArgumentError, CommandExecutionError } from '@jackwener/opencli/errors';

export const EUTILS_BASE = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils';
export const SEARCH_COLUMNS = ['rank', 'pmid', 'title', 'authors', 'journal', 'year', 'article_type', 'doi', 'url'];
export const LINK_COLUMNS = ['rank', 'pmid', 'title', 'authors', 'journal', 'year', 'article_type', 'doi', 'url'];
export const RELATED_COLUMNS = ['rank', 'pmid', 'title', 'authors', 'journal', 'year', 'article_type', 'score', 'doi', 'url'];

let lastRequestAt = 0;

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

export function requirePmid(value, label = 'pmid') {
    const pmid = requireText(value, label);
    if (!/^\d+$/.test(pmid)) {
        throw new ArgumentError(`pubmed ${label} must be a numeric PMID`, 'Example: 37780221');
    }
    return pmid;
}

export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
    const raw = value ?? defaultValue;
    const text = String(raw).trim();
    if (!/^\d+$/.test(text)) {
        throw new ArgumentError(`pubmed ${label} must be a positive integer`);
    }

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Provide a non-empty value for the labeled argument
  2. Echo the shell variable or config field feeding the argument to confirm it is populated
  3. Guard in calling scripts: abort before invoking when the value trims to empty

Example fix

// before
const q = process.env.QUERY; // may be undefined
await pubmedSearch(q);
// after
const q = (process.env.QUERY ?? '').trim();
if (!q) throw new Error('QUERY env var is required');
await pubmedSearch(q);
Defensive patterns

Strategy: validation

Validate before calling

function assertNonEmpty(value, label) {
  const t = String(value ?? '').trim();
  if (!t) throw new Error(`pubmed ${label} cannot be empty`);
  return t;
}
assertNonEmpty(query, 'query'); // call before invoking the command

Type guard

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

Try / catch

try {
  await cli.parse(['pubmed', 'search', query]);
} catch (err) {
  if (err instanceof ArgumentError && /cannot be empty/.test(err.message)) {
    console.error(`Missing value: ${err.message}`);
    process.exitCode = 2;
  } else { throw err; }
}

Prevention

When it happens

Trigger: Any pubmed command invoked with an empty, whitespace-only, null, or undefined text argument, e.g. `pubmed search ""` or `pubmed journal --name ' '`.

Common situations: Unset shell variables expanding to empty string, forgotten positional argument, empty config field, programmatic callers passing null/undefined.

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