jackwener/OpenCLI · warning · ArgumentError
--category must be one of ${RECOMMEND_CATEGORIES.join(', ')}
Error message
--category must be one of ${RECOMMEND_CATEGORIES.join(', ')}, got ${JSON.stringify(raw)} What it means
parseRecommendCategory validates the `--category` option of the toutiao recommend command against the fixed allowlist RECOMMEND_CATEGORIES (__all__, news_tech, news_finance, news_world, news_sports, news_entertainment, news_military). Any trimmed string not in that list is rejected as an ArgumentError because the upstream API only accepts these category slugs.
Source
Thrown at clis/toutiao/utils.js:41
}
export function parseRecommendLimit(raw, fallback = 20) {
if (raw === undefined || raw === null || raw === '') return fallback;
const parsed = Number(raw);
if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {
throw new ArgumentError(`--limit must be an integer between ${RECOMMEND_MIN_LIMIT} and ${RECOMMEND_MAX_LIMIT}, got ${JSON.stringify(raw)}`);
}
if (parsed < RECOMMEND_MIN_LIMIT || parsed > RECOMMEND_MAX_LIMIT) {
throw new ArgumentError(`--limit must be between ${RECOMMEND_MIN_LIMIT} and ${RECOMMEND_MAX_LIMIT}, got ${parsed}`);
}
return parsed;
}
export function parseRecommendCategory(raw, fallback = '__all__') {
if (raw === undefined || raw === null || raw === '') return fallback;
const value = String(raw).trim();
if (!RECOMMEND_CATEGORIES.includes(value)) {
throw new ArgumentError(`--category must be one of ${RECOMMEND_CATEGORIES.join(', ')}, got ${JSON.stringify(raw)}`);
}
return value;
}
export function parseHotLimit(raw, fallback = 30) {
if (raw === undefined || raw === null || raw === '') return fallback;
const parsed = Number(raw);
if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {
throw new ArgumentError(`--limit must be an integer between ${HOT_MIN_LIMIT} and ${HOT_MAX_LIMIT}, got ${JSON.stringify(raw)}`);
}
if (parsed < HOT_MIN_LIMIT || parsed > HOT_MAX_LIMIT) {
throw new ArgumentError(`--limit must be between ${HOT_MIN_LIMIT} and ${HOT_MAX_LIMIT}, got ${parsed}`);
}
return parsed;
}
const NON_TITLE_LINES = new Set([
'展现', '阅读', '点赞', '评论',View on GitHub (pinned to 49907e53dc)
Solutions
- Use one of the exact slugs: __all__, news_tech, news_finance, news_world, news_sports, news_entertainment, news_military.
- Trim whitespace and keep lowercase exact match — matching is case-sensitive.
- Omit --category to fall back to '__all__'.
- Call RECOMMEND_CATEGORIES from clis/toutiao/utils.js to build the choice list programmatically instead of hardcoding.
Example fix
// before toutiao recommend --category Tech // after toutiao recommend --category news_tech
Defensive patterns
Strategy: validation
Validate before calling
const ALLOWED = ['__all__','news_tech','news_finance','news_world','news_sports','news_entertainment','news_military'];
if (raw !== undefined && !ALLOWED.includes(String(raw).trim())) throw new RangeError(`--category must be one of ${ALLOWED.join(', ')}`); Type guard
const isRecommendCategory = (v) => typeof v === 'string' && RECOMMEND_CATEGORIES.includes(v.trim());
Try / catch
try {
parseRecommendCategory(raw);
} catch (e) {
if (e instanceof ArgumentError) { console.error(`${e.message}\nValid: ${RECOMMEND_CATEGORIES.join(', ')}`); process.exitCode = 2; }
else throw e;
} Prevention
- Import RECOMMEND_CATEGORIES and populate CLI choices/completion from it.
- Normalize input with .trim().toLowerCase() — matching is exact and case-sensitive.
- Omit --category for the '__all__' default.
When it happens
Trigger: Passing --category with a free-form value like 'tech', 'News_Tech' (case-sensitive), 'sports ', a typo like 'news_sport', or a localized name like '科技' instead of the exact slug.
Common situations: Users guessing category slugs instead of listing them; passing human-readable category names from another source; case/space mismatch after copying from docs; older scripts using slugs that were renamed.
Understand the failure class
Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.
Related errors
- --limit must be between ${RECOMMEND_MIN_LIMIT} and ${RECOMME
- --limit must be an integer between ${HOT_MIN_LIMIT} and ${HO
- --limit must be between ${HOT_MIN_LIMIT} and ${HOT_MAX_LIMIT
- <train-no> "${trainNo}" does not look like a 12306 internal
- --from station must not be empty
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/460ee1ff7e7bc0da.
Report an issue: GitHub.