{"record":{"id":"460ee1ff7e7bc0da","repo":"jackwener/OpenCLI","slug":"category-must-be-one-of-recommend-categories-j","errorCode":null,"errorMessage":"--category must be one of ${RECOMMEND_CATEGORIES.join(', ')}, got ${JSON.stringify(raw)}","messagePattern":"--category must be one of (.+?), got (.+?)","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"warning","filePath":"clis/toutiao/utils.js","lineNumber":41,"sourceCode":"}\n\nexport function parseRecommendLimit(raw, fallback = 20) {\n    if (raw === undefined || raw === null || raw === '') return fallback;\n    const parsed = Number(raw);\n    if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {\n        throw new ArgumentError(`--limit must be an integer between ${RECOMMEND_MIN_LIMIT} and ${RECOMMEND_MAX_LIMIT}, got ${JSON.stringify(raw)}`);\n    }\n    if (parsed < RECOMMEND_MIN_LIMIT || parsed > RECOMMEND_MAX_LIMIT) {\n        throw new ArgumentError(`--limit must be between ${RECOMMEND_MIN_LIMIT} and ${RECOMMEND_MAX_LIMIT}, got ${parsed}`);\n    }\n    return parsed;\n}\n\nexport function parseRecommendCategory(raw, fallback = '__all__') {\n    if (raw === undefined || raw === null || raw === '') return fallback;\n    const value = String(raw).trim();\n    if (!RECOMMEND_CATEGORIES.includes(value)) {\n        throw new ArgumentError(`--category must be one of ${RECOMMEND_CATEGORIES.join(', ')}, got ${JSON.stringify(raw)}`);\n    }\n    return value;\n}\n\nexport function parseHotLimit(raw, fallback = 30) {\n    if (raw === undefined || raw === null || raw === '') return fallback;\n    const parsed = Number(raw);\n    if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {\n        throw new ArgumentError(`--limit must be an integer between ${HOT_MIN_LIMIT} and ${HOT_MAX_LIMIT}, got ${JSON.stringify(raw)}`);\n    }\n    if (parsed < HOT_MIN_LIMIT || parsed > HOT_MAX_LIMIT) {\n        throw new ArgumentError(`--limit must be between ${HOT_MIN_LIMIT} and ${HOT_MAX_LIMIT}, got ${parsed}`);\n    }\n    return parsed;\n}\n\nconst NON_TITLE_LINES = new Set([\n    '展现', '阅读', '点赞', '评论',","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/toutiao/utils.js#L23-L59","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ntoutiao recommend --category Tech\n// after\ntoutiao recommend --category news_tech","handlingStrategy":"validation","validationCode":"const ALLOWED = ['__all__','news_tech','news_finance','news_world','news_sports','news_entertainment','news_military'];\nif (raw !== undefined && !ALLOWED.includes(String(raw).trim())) throw new RangeError(`--category must be one of ${ALLOWED.join(', ')}`);","typeGuard":"const isRecommendCategory = (v) => typeof v === 'string' && RECOMMEND_CATEGORIES.includes(v.trim());","tryCatchPattern":"try {\n  parseRecommendCategory(raw);\n} catch (e) {\n  if (e instanceof ArgumentError) { console.error(`${e.message}\\nValid: ${RECOMMEND_CATEGORIES.join(', ')}`); process.exitCode = 2; }\n  else throw e;\n}","preventionTips":["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."],"tags":["cli","argument-validation","enum-value","toutiao"],"backgroundTag":"invalid-enum-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}