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

  1. Use one of the exact slugs: __all__, news_tech, news_finance, news_world, news_sports, news_entertainment, news_military.
  2. Trim whitespace and keep lowercase exact match — matching is case-sensitive.
  3. Omit --category to fall back to '__all__'.
  4. 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

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


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/460ee1ff7e7bc0da. Report an issue: GitHub.