jackwener/OpenCLI · error · ArgumentError
--${name} is required
Error message
--${name} is required What it means
This ArgumentError from requireString is thrown when a required CLI argument is missing, not a string, or an empty/whitespace-only string. It fails fast before any network call, and the message names the flag (prefixed with --) that must be supplied.
Source
Thrown at clis/wttr/utils.js:13
// wttr.in shared helpers — global weather (no auth, terminal-friendly JSON via ?format=j1).
//
// Coverage: worldwide. Unlike NWS (US-only), wttr.in geocodes any city/airport
// code/lat,lon string and serves a 3-day forecast + current conditions in one
// payload.
import { ArgumentError, EmptyResultError, CommandExecutionError } from '@jackwener/opencli/errors';
export const WTTR_BASE = 'https://wttr.in';
const UA = 'opencli-wttr/1.0';
export function requireString(value, name) {
if (typeof value !== 'string' || !value.trim()) {
throw new ArgumentError(`--${name} is required`);
}
return value.trim();
}
export async function wttrFetch(location, label) {
// wttr.in path-encodes the location. Spaces → %20 is fine; commas survive.
const url = `${WTTR_BASE}/${encodeURIComponent(location)}?format=j1`;
let resp;
try {
resp = await fetch(url, { headers: { 'User-Agent': UA, accept: 'application/json' } });
} catch (err) {
throw new CommandExecutionError(`${label} request failed: ${err.message}`);
}
if (resp.status === 404) {
throw new EmptyResultError(label, `${label} could not find location "${location}".`);
}
if (!resp.ok) {
throw new CommandExecutionError(`${label} returned HTTP ${resp.status}.`);View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a non-empty location, e.g. --location 'New York'
- Check that the variable feeding the argument is defined and non-blank
- Trim or validate user input before passing it
Example fix
// before
await query({ location: process.env.CITY }); // CITY unset -> undefined
// after
const city = process.env.CITY?.trim();
if (!city) throw new Error('Set CITY env var');
await query({ location: city }); Defensive patterns
Strategy: validation
Validate before calling
function requireString(value, name) {
if (typeof value !== 'string' || !value.trim()) {
throw new Error(`--${name} is required`);
}
return value.trim();
}
const location = requireString(args.location, 'location'); Type guard
function isNonEmptyString(v) {
return typeof v === 'string' && v.trim().length > 0;
} Try / catch
try {
result = await query({ location: args.location });
} catch (err) {
if (err instanceof ArgumentError && err.message.includes('is required')) {
console.error('Usage: --location <city|lat,lon>');
} else throw err;
} Prevention
- Always validate required CLI args at entry point before calling the library
- Coerce and trim shell/env input; check for undefined and empty strings
- Provide sensible defaults or interactive prompts for missing flags
When it happens
Trigger: Calling query (or any requireString caller) without args.location, with location: undefined/null, or with location: '' or ' '.
Common situations: Forgetting the --location flag on the command line; a script variable that is undefined because an env var was unset; empty string after shell quoting mistakes.
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/9e559520b3a98f43.
Report an issue: GitHub.