{"record":{"id":"d3db975e9fae5c7e","repo":"jackwener/OpenCLI","slug":"name-is-required","errorCode":null,"errorMessage":"--${name} is required","messagePattern":"--(.+?) is required","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/openfda/utils.js","lineNumber":13,"sourceCode":"// openFDA shared helpers — FDA drug labels + food recall enforcement (no auth, public).\n//\n// Free public tier with anonymous rate limit (~240 req/min, 1000 req/day per IP).\n// API key bumps that to 240 req/min × ~120000 req/day, but is not required for\n// modest read traffic.\nimport { ArgumentError, EmptyResultError, CommandExecutionError } from '@jackwener/opencli/errors';\n\nexport const OPENFDA_BASE = 'https://api.fda.gov';\nconst UA = 'opencli-openfda/1.0';\n\nexport function requireString(value, name) {\n    if (typeof value !== 'string' || !value.trim()) {\n        throw new ArgumentError(`--${name} is required`);\n    }\n    return value.trim();\n}\n\nexport function requireBoundedInt(value, def, max, name = 'limit') {\n    const n = value == null || value === '' ? def : Number(value);\n    if (!Number.isInteger(n) || n < 1 || n > max) {\n        throw new ArgumentError(`--${name} must be an integer between 1 and ${max}`);\n    }\n    return n;\n}\n\nexport async function openfdaFetch(url, label) {\n    let resp;\n    try {\n        resp = await fetch(url, { headers: { 'User-Agent': UA, accept: 'application/json' } });\n    } catch (err) {\n        throw new CommandExecutionError(`${label} request failed: ${err.message}`);","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/openfda/utils.js#L1-L31","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nQUERY=\"\" ; opencli openfda drug-label --query \"$QUERY\"  # -> --query is required\n// after\nQUERY=\"ibuprofen\" ; opencli openfda drug-label --query \"$QUERY\"","handlingStrategy":"validation","validationCode":"function ensureFlag(value, name) {\n  if (typeof value !== 'string' || !value.trim()) {\n    throw new Error(`--${name} is required`);\n  }\n  return value.trim();\n}\n// usage: const query = ensureFlag(process.env.QUERY, 'query');","typeGuard":"function isNonEmptyString(v) {\n  return typeof v === 'string' && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  const rows = await openfdaQuery({ query });\n} catch (e) {\n  if (e instanceof ArgumentError && /is required/.test(e.message)) {\n    console.error(`Usage: supply the missing flag. ${e.message}`);\n    process.exitCode = 2;\n  } else throw e;\n}","preventionTips":["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."],"tags":["argument-validation","cli","missing-argument","openfda"],"backgroundTag":"missing-required-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}