jackwener/OpenCLI · error · ArgumentError
crates crate name is required (e.g. "serde", "tokio")
Error message
crates crate name is required (e.g. "serde", "tokio")
What it means
requireCrateName enforces that a crate name is present before validating its format; empty values get this dedicated message with examples ('serde', 'tokio'). It exists so the common 'forgot the name' case produces a more helpful error than the format-failure message.
Source
Thrown at clis/crates/utils.js:18
// Shared helpers for the crates.io adapters.
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
export const CRATES_BASE = 'https://crates.io';
const UA = 'opencli-crates-adapter (+https://github.com/jackwener/opencli)';
// crates.io crate names: 1-64 chars, ascii letters/digits/-_, must start with a letter.
const CRATE_NAME = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
export function requireString(value, label) {
const s = String(value ?? '').trim();
if (!s) throw new ArgumentError(`crates ${label} cannot be empty`);
return s;
}
export function requireCrateName(value) {
const s = String(value ?? '').trim();
if (!s) throw new ArgumentError('crates crate name is required (e.g. "serde", "tokio")');
if (!CRATE_NAME.test(s)) {
throw new ArgumentError(
`crates crate name "${value}" is not a valid crates.io name`,
'Names start with an ASCII letter, then 0-63 chars of letters / digits / "_-".',
);
}
return s;
}
export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
const raw = value ?? defaultValue;
const n = typeof raw === 'number' ? raw : Number(raw);
if (!Number.isInteger(n) || n <= 0) {
throw new ArgumentError(`crates ${label} must be a positive integer`);
}
if (n > maxValue) {
throw new ArgumentError(`crates ${label} must be <= ${maxValue}`);
}View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a crate name, e.g. `crates crate serde`.
- In scripts, default or assert the variable before calling: if (!name) fail fast.
- Use `crates search <keyword>` first if you do not know the exact name.
- Wrap calls in a try/catch on ArgumentError to surface usage help.
Example fix
// before
await cli.crates.crate({ name: undefined });
// after
await cli.crates.crate({ name: 'tokio' }); Defensive patterns
Strategy: validation
Validate before calling
function requireName(value) {
const s = String(value ?? '').trim();
if (!s) throw new Error('crate name is required (e.g. "serde", "tokio")');
return s;
} Type guard
function hasCrateName(v) {
return typeof v === 'string' && v.trim().length > 0;
} Try / catch
try {
await cli.crates.crate({ name });
} catch (e) {
if (e instanceof ArgumentError && e.message.includes('crate name is required')) {
console.error('Usage: crates crate <name>');
} else throw e;
} Prevention
- Include the crate name in scripted invocations; never rely on unset variables.
- Default interactive prompts to well-known examples to avoid empty submits.
- Validate arguments at the CLI boundary before dispatching.
When it happens
Trigger: Calling `crates crate` (or anything that calls requireCrateName) with name === '', ' ', null, or undefined — typically when the positional/flag argument was omitted entirely.
Common situations: Running the CLI interactively and forgetting the crate argument, scripting with an unset variable, or a wrapper passing undefined when a lookup fails.
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
- <train-no> must not be empty
- keyword must not be empty
- <from> station must not be empty
- <to> station must not be empty
- who 不能为空
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/2a712c5ebc5f4c3e.
Report an issue: GitHub.