jackwener/OpenCLI · error · ArgumentError

pubmed ${label} must be one of: ${choices.join(', ')}

Error message

pubmed ${label} must be one of: ${choices.join(', ')}

What it means

requireChoice validates enum-like options (position, sort, direction) against an allowed list, after applying the default. Values outside the list throw ArgumentError 'pubmed <label> must be one of: <choice1, choice2, ...>'.

Source

Thrown at clis/pubmed/utils.js:56

    }
    return n;
}

export function requireYear(value, label) {
    if (value === undefined || value === null || value === '') {
        return undefined;
    }
    const year = requireBoundedInt(value, 1900, 3000, label);
    if (year < 1800) {
        throw new ArgumentError(`pubmed ${label} must be >= 1800`);
    }
    return year;
}

export function requireChoice(value, choices, label, defaultValue) {
    const text = String(value ?? defaultValue).trim();
    if (!choices.includes(text)) {
        throw new ArgumentError(`pubmed ${label} must be one of: ${choices.join(', ')}`);
    }
    return text;
}

export function buildEutilsUrl(tool, params = {}) {
    const searchParams = new URLSearchParams();
    searchParams.set('db', 'pubmed');
    if (!params.retmode) {
        searchParams.set('retmode', 'json');
    }
    if (process.env.NCBI_API_KEY) {
        searchParams.set('api_key', process.env.NCBI_API_KEY);
    }
    if (process.env.NCBI_EMAIL) {
        searchParams.set('email', process.env.NCBI_EMAIL);
    }
    for (const [key, value] of Object.entries(params)) {
        if (value !== undefined && value !== null && value !== '') {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use exactly one of the values listed in the error message (they are comma-joined)
  2. Run the command with --help to see documented choices
  3. Match the exact casing of the allowed values

Example fix

// before
--direction ascending
// after
--direction asc   // or whichever value the error lists
Defensive patterns

Strategy: validation

Validate before calling

const ALLOWED_SORT = ['relevance', 'pub_date']; // see --help
if (!ALLOWED_SORT.includes(sort)) {
  throw new Error(`sort must be one of: ${ALLOWED_SORT.join(', ')}`);
}

Type guard

function isOneOf(v, choices) {
  return choices.includes(String(v ?? '').trim());
}

Try / catch

try {
  await pubmedSearch(query, { sort });
} catch (err) {
  const m = /must be one of: (.+)/.exec(err.message ?? '');
  if (err instanceof ArgumentError && m) {
    console.error(`Invalid sort '${sort}'. Allowed: ${m[1]}`);
    process.exitCode = 2;
  } else { throw err; }
}

Prevention

When it happens

Trigger: `--sort newest` when only 'relevance'/'pub_date' style values are accepted, `--direction ascending` instead of 'asc', misspelled position values.

Common situations: Guessing option names without reading --help, copying flags from a different CLI, case mismatches ('ASC' vs 'asc').

Related errors


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