jackwener/OpenCLI · error · ArgumentError
--${name} is required
Error message
--${name} is required What it means
requireString validates that a required CLI string option is present and non-blank, trimming and returning it. If the value is not a non-empty string it throws ArgumentError with '--<name> is required', naming the flag the user must supply. It is the standard gate for mandatory openfda query parameters.
Source
Thrown at clis/openfda/utils.js:13
// openFDA shared helpers — FDA drug labels + food recall enforcement (no auth, public).
//
// Free public tier with anonymous rate limit (~240 req/min, 1000 req/day per IP).
// API key bumps that to 240 req/min × ~120000 req/day, but is not required for
// modest read traffic.
import { ArgumentError, EmptyResultError, CommandExecutionError } from '@jackwener/opencli/errors';
export const OPENFDA_BASE = 'https://api.fda.gov';
const UA = 'opencli-openfda/1.0';
export function requireString(value, name) {
if (typeof value !== 'string' || !value.trim()) {
throw new ArgumentError(`--${name} is required`);
}
return value.trim();
}
export function requireBoundedInt(value, def, max, name = 'limit') {
const n = value == null || value === '' ? def : Number(value);
if (!Number.isInteger(n) || n < 1 || n > max) {
throw new ArgumentError(`--${name} must be an integer between 1 and ${max}`);
}
return n;
}
export async function openfdaFetch(url, label) {
let resp;
try {
resp = await fetch(url, { headers: { 'User-Agent': UA, accept: 'application/json' } });
} catch (err) {
throw new CommandExecutionError(`${label} request failed: ${err.message}`);View on GitHub (pinned to 49907e53dc)
Solutions
- Supply the missing flag, e.g. --query "ibuprofen".
- In scripts, guard with ${VAR:?message} so unset variables fail early with a clear message.
- In code, check the value before calling the API wrapper.
- Run the command with --help to see which flags are mandatory.
Example fix
// before QUERY="" ; opencli openfda drug-label --query "$QUERY" # -> --query is required // after QUERY="ibuprofen" ; opencli openfda drug-label --query "$QUERY"
Defensive patterns
Strategy: validation
Validate before calling
function ensureFlag(value, name) {
if (typeof value !== 'string' || !value.trim()) {
throw new Error(`--${name} is required`);
}
return value.trim();
}
// usage: const query = ensureFlag(process.env.QUERY, 'query'); Type guard
function isNonEmptyString(v) {
return typeof v === 'string' && v.trim().length > 0;
} Try / catch
try {
const rows = await openfdaQuery({ query });
} catch (e) {
if (e instanceof ArgumentError && /is required/.test(e.message)) {
console.error(`Usage: supply the missing flag. ${e.message}`);
process.exitCode = 2;
} else throw e;
} Prevention
- List required flags in --help output and enforce them up front.
- In shell scripts use ${QUERY:?"QUERY is required"} to fail fast on unset vars.
- Validate inputs before constructing API URLs.
- Never pass raw environment variables without an emptiness check.
When it happens
Trigger: Calling an openfda command (e.g. query) without the required flag, with an empty value (--query ""), or with only whitespace; programmatically passing null/undefined/number into the wrapper that forwards to requireString.
Common situations: User omits the flag because the command synopsis wasn't read; shell variable holding the query is unset so the flag expands to nothing; a script passes an empty string after trimming.
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
- keyword must not be empty
- <from> station must not be empty
- <to> station must not be empty
- who 不能为空
- key is required
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/d3db975e9fae5c7e.
Report an issue: GitHub.